鱼C论坛

 找回密码
 立即注册
查看: 1167|回复: 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
这段代码可以统计学生的分数
  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. class Exam {
  4.         public:
  5.                 string student_name;
  6.                 int score;
  7.                
  8.                 Exam(string aname, int ascore)
  9.                 {
  10.                         student_name = aname;
  11.                         score = ascore;
  12.                 }
  13. };

  14. int main()
  15. {
  16.         Exam first("A", 98);
  17.         
  18.         cout << first.student_name << "考了" << first.score << "分";
  19. }
复制代码

但是也会有人乱改分数比如:
  1. Exam first("A", 300);
复制代码
  300分……

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


先说说 private 关键字

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

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

  3. class Exam {
  4.         public:
  5.                 string student_name;
  6.                
  7.                 Exam(string aname, int ascore)
  8.                 {
  9.                         student_name = aname;
  10.                         score = ascore;
  11.                 }
  12.                
  13.         private:
  14.                 int score;       
  15. };

  16. int main()
  17. {
  18.         Exam first("A", 98);
  19.        
  20.         cout << first.student_name << "考了" << first.score << "分";
  21. }
复制代码

结果是:
  1. 报错#%@
复制代码


getter & setter 代码

乱设置分数的,最后都会被强制设置为 0 分

  1. #include<iostream>
  2. using namespace std;

  3. class Students {
  4.         private:
  5.                 int score;
  6.         public:
  7.                 string name;
  8.                
  9.                 Students(string aname, int ascore)
  10.                 {
  11.                         name = aname;
  12.                         setscore(ascore);
  13.                 }
  14.                
  15.                 bool ispass()
  16.                 {
  17.                         if(score<60)
  18.                         {
  19.                                 return false;
  20.                         }
  21.                         return true;
  22.                 }
  23.                
  24.                 void setscore(int ascore)
  25.                 {
  26.                         if(ascore<0 || ascore>100)
  27.                         {
  28.                                 score = 0;
  29.                         }
  30.                         else
  31.                         {
  32.                                 score = ascore;
  33.                         }
  34.                 }
  35.                
  36.                 int getscore()
  37.                 {
  38.                         return score;
  39.                 }
  40. };

  41. int main()
  42. {
  43.         Students Andy("^_^",300);
  44.         cout<<Andy.getscore();
  45.        
  46.         return 0;
  47. }
复制代码


下一篇:? ? ?

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> 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-5-1 01:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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