C++学习日记---第13天(类和对象---封装)

[复制链接]
发表于 2025-12-19 02:25:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

×
条记复习

1.类和对象

c++面向对象的三大特性为:封装,继续,多态
c++以为万事万物都皆为对象,对象上有其属性和举动
具有类似性子的对象,我们可以抽象为称为类
2.封装

作用:将属性和举动作为一个团体,体现生存中的事物,具有类似性子的对象,我们可以抽象为类。
语法:class 类名{访问权限(也可不写,默以为private):属性/举动};
示例:
  1. class circle{
  2. public:
  3.     int m_r;
  4.     double calculator(){
  5.         return 2*pi*m_r;
  6.     }
  7. }
复制代码
封装案例:盘算一个圆的周长
  1. #include<iostream>
  2. using namespace std;
  3. const double pi = 3.14;
  4. class circle {
  5. public:
  6.         //属性
  7.         //半径
  8.         int m_r;
  9.         //行为
  10.         //获取圆的周长
  11.         double calculatezc() {
  12.                 return 2 * pi * m_r;
  13.         }
  14. };
  15. int main() {
  16. 通过圆类创建具体的圆(对象)
  17.         circle c1;
  18. 给圆对象的属性进行赋值操作
  19.         c1.m_r = 10;
  20.         cout << "圆的周长为" << c1.calculatezc() << endl;
  21.         system("pause");
  22.         return 0;
  23. }
复制代码
对比结构体我们不难发现,类与结构体可以说千篇一律,但现实上类与结构体的区别很大,不外难度过高,我们这里不做深入探究,下面我们来学习类的访问权限
3.类的访问权限

1)分类

访问权限一共有一下三种:
a.public         公共权限                                            此中的成员类内可以访问,类外也可以访问
b.protected    掩护权限                                            此中的成员类内可以访问,类外不可以访问
对于protected权限,特定职员有权访问
c.private         私有权限                                           此中的成员类内可以访问,类外不可以访问
对于private权限,除本人外其他人无权访问
现实上在结构体struct中也有权限,不外与类class的区别是,struct的默认权限是公共权限,class的默认权限是私有权限
2)访问权限的作用

在现实的编程项目中,我们通常将类的对象的属性设置为私有,而类中的函数则设置为公有。
比方:
  1. class student{
  2. public:
  3.     int let_you_see_it(){
  4.         return age;
  5. }
  6. private:
  7.     int age=18;
  8. }
复制代码
利用这一利用,我们可以本身设置类对象属性的只读,只写,读写利用,代码如下:
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. class person {//创建一个person结构体
  5. public:
  6.         void set_name(string name) {//定义一个函数来接收数据,然后将数据赋给私有权限中的数据
  7.                 m_name = name;
  8.                 //这个函数无返回值,故要返回字符串需要创建一个string函数
  9.         }
  10.         string get_name() {
  11.                 return m_name;
  12.         }
  13.         int get_age() {
  14.                 return m_age;//类内可以访问私有成员
  15.         }
  16.         void set_idol(string idol) {
  17.                 m_idol = idol;//没有返回值,只写
  18.         }
  19. private:
  20.         string m_name; //可读可写
  21.         int m_age=18;//只读
  22.         string m_idol;//只写
  23. };
  24. int main() {
  25.         person p;
  26.     //创建一个person中的p对象
  27.         p.set_name("张三");
  28.         cout << "姓名:" << p.get_name() << endl;
  29.         cout << "年龄:" << p.get_age() << endl;
  30.         p.set_idol("小明");
  31.         system("pause");
  32.         return 0;
  33. }
复制代码
4.类案例:长方体类

案例要求:
1)创创建方体类
2)计划属性
3)计划举动,获取立方面子积和体积
4)分别利用全局函数和成员函数判定两个立方体是否相当
我们仍旧利用上面的方法,将属性设置为私有,并通过成员函数来修改和获取属性的值
我们将代码实现分为三个部门,第一部门是设置属性,完成面积体积的盘算,第二部门是用全局函数判定两个立方体是否相当,第三部门是用成员函数判定,下面给出第一部门的代码:
  1. #include<iostream>
  2. using namespace std;
  3. class cube {
  4. public:
  5.         void set_l(int l) {
  6.                 m_l = l;
  7.         }
  8.         int get_l() {
  9.                 return m_l;
  10.         }
  11.         void set_w(int w) {
  12.                 m_w = w;
  13.         }
  14.         int get_w() {
  15.                 return m_w;
  16.         }
  17.         void set_h(int h) {
  18.                 m_h = h;
  19.         }
  20.         int get_h() {
  21.                 return m_h;
  22.         }
  23.         int calculateS() {
  24.                 return 2 * m_l * m_w + 2 * m_w * m_h + 2 * m_l * m_h;
  25.         }
  26.         int calculateV() {
  27.                 return m_l * m_w * m_h;
  28.         }
  29. private:
  30.         int m_l;
  31.         int m_w;
  32.         int m_h;
  33. };
  34. int main() {
  35.         cube c1;
  36.         c1.set_l(10);
  37.         c1.set_w(20);
  38.         c1.set_h(30);
  39.         cout <<"c1的面积为"<< c1.calculateS() << endl;
  40.         cout <<"c1的体积为"<< c1.calculateV() << endl;
  41.         return 0;
  42. }
复制代码
第二部门代码为:
  1. bool isSame1(cube& c1, cube& c2) {
  2.         if (c1.get_l() == c2.get_l() && c1.get_w() == c2.get_w() && c1.get_h() == c2.get_h()) {
  3.                 return true;
  4.         }
  5.         return false;
  6. }
复制代码
第三部门代码为:
  1. bool isSame2(cube& c) {
  2.         //这里传递的参数一个就可以,因为我们是用已知的一个对象去调用未知的一个对象
  3.         if (m_l == c.get_l() && m_w == c.get_w() && m_h == c.get_h()) {
  4.                 return true;
  5.         }
  6.         return false;
  7. }
复制代码
完备代码如下:
  1. #include<iostream>
  2. using namespace std;
  3. class cube {
  4. public:
  5.         void set_l(int l) {
  6.                 m_l = l;
  7.         }
  8.         int get_l() {
  9.                 return m_l;
  10.         }
  11.         void set_w(int w) {
  12.                 m_w = w;
  13.         }
  14.         int get_w() {
  15.                 return m_w;
  16.         }
  17.         void set_h(int h) {
  18.                 m_h = h;
  19.         }
  20.         int get_h() {
  21.                 return m_h;
  22.         }
  23.         int calculateS() {
  24.                 return 2 * m_l * m_w + 2 * m_w * m_h + 2 * m_l * m_h;
  25.         }
  26.         int calculateV() {
  27.                 return m_l * m_w * m_h;
  28.         }
  29.         bool isSame2(cube& c) {
  30.                 //这里传递的参数一个就可以,因为我们是用已知的一个对象去调用未知的一个对象
  31.                 if (m_l == c.get_l() && m_w == c.get_w() && m_h == c.get_h()) {
  32.                         return true;
  33.                 }
  34.                 return false;
  35.         }
  36. private:
  37.         int m_l;
  38.         int m_w;
  39.         int m_h;
  40. };
  41. bool isSame(cube& c1, cube& c2) {
  42.         if (c1.get_l() == c2.get_l() && c1.get_w() == c2.get_w() && c1.get_h() == c2.get_h()) {
  43.                 return true;
  44.         }
  45.         return false;
  46. }
  47. int main() {
  48.         cube c1;
  49.         c1.set_l(10);
  50.         c1.set_w(20);
  51.         c1.set_h(30);
  52.         cout <<"c1的面积为"<< c1.calculateS() << endl;
  53.         cout <<"c1的体积为"<< c1.calculateV() << endl;
  54.         cube c2;
  55.         c2.set_l(10);
  56.         c2.set_w(10);
  57.         c2.set_h(30);
  58.         cout << "c2的面积为" << c2.calculateS() << endl;
  59.         cout << "c2的体积为" << c2.calculateV() << endl;
  60.         bool ret1 = isSame(c1,c2);//引用传递
  61.         if (ret1) {
  62.                 cout << "全局函数:两个立方体相等" << endl;
  63.         }
  64.         else {
  65.                 cout << "全局函数;两个立方体不相等" << endl;
  66.         }
  67.         bool ret2 = c1.isSame2(c2);
  68.         //这里如果是用c1作为对象,那么传入c2即可,如果是c2作为对象,那么传入c1
  69.         if (ret2) {
  70.                 cout << "成员函数:两个立方体相等" << endl;
  71.         }
  72.         else {
  73.                 cout << "成员函数;两个立方体不相等" << endl;
  74.         }
  75.         return 0;
  76. }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表