鱼C论坛

 找回密码
 立即注册
查看: 1220|回复: 3

[技术交流] 005 - 类和对象 ④ private & pubilc 关键字 + getter & setter

[复制链接]
发表于 2020-5-8 17:14:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 liuzhengyuan 于 2020-7-6 12:50 编辑

待更新

C++ 自学心得 |005 - 类和对象 ④ private & pubilc 关键字 + getter & setter





大概总结了一下

关键字含义
public成员可以在类外部被访问
private成员只可以在类内部被访问
protect可以在派生类(即子类)被访问



1,getter & setter
这段代码可以统计学生的分数
#include<bits/stdc++.h>
using namespace std;

class Exam {
        public:
                string student_name;
                int score;
                
                Exam(string aname, int ascore)
                {
                        student_name = aname;
                        score = ascore;
                }
};

int main()
{
        Exam first("A", 98);
        
        cout << first.student_name << "考了" << first.score << "分"; 
}
但是也会有人乱改分数比如:
Exam first("A", 300);
  300分……

防止乱改分数的人的最有效方法就是 getter and setter


先说说 private 关键字

不妨把刚刚的代码改成:
score 变量是 Exam 类私有的

所以主函数访问不了 score 变量,所以报错……
#include<bits/stdc++.h>
using namespace std;

class Exam {
        public:
                string student_name;
                
                Exam(string aname, int ascore)
                {
                        student_name = aname;
                        score = ascore;
                }
                
        private:
                int score;        
};

int main()
{
        Exam first("A", 98);
        
        cout << first.student_name << "考了" << first.score << "分"; 
}
结果是:
报错#%@


getter & setter 代码

乱设置分数的,最后都会被强制设置为 0 分
#include<iostream>
using namespace std;

class Students {
        private:
                int score;
        public:
                string name;
                
                Students(string aname, int ascore)
                {
                        name = aname;
                        setscore(ascore);
                }
                
                bool ispass()
                {
                        if(score<60)
                        {
                                return false;
                        }
                        return true;
                }
                
                void setscore(int ascore)
                {
                        if(ascore<0 || ascore>100)
                        {
                                score = 0;
                        }
                        else
                        {
                                score = ascore;
                        }
                }
                
                int getscore()
                {
                        return score;
                }
};

int main()
{
        Students Andy("^_^",300);
        cout<<Andy.getscore();
        
        return 0;
}

下一篇:? ? ?

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-8 17:17:43 | 显示全部楼层
protect:???我呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-8 17:35:49 | 显示全部楼层
明天再更新吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-8 17:36:33 | 显示全部楼层

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-16 14:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表