鱼C论坛

 找回密码
 立即注册
查看: 1127|回复: 0

[技术交流] C++旅程第七站-----类的继承

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

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

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

x
本帖最后由 这是她 于 2020-5-15 22:22 编辑

Wanting to be someone else is a waste of the person you are.        --Kurt Cobain
                                                                                                                 类的继承
           继承的好处:减少重复代码
           语法:class 子类 : 继承方式  父类
                    子类 -> 派生类        父类 -> 基类
           派生类中的成员:
           1、从基类继承过来;(共性)            2、自己增加的成员;(特性)

          继承方式:公共继承  私有继承  保护继承
  1. #include<iostream>

  2. using namespace std;

  3. class Base1
  4. {
  5.         public:
  6.                 int m_A;
  7.                
  8.                 void Headline1()
  9.                 {
  10.                         cout << "文件的标题!!!!" << endl;
  11.                 }
  12.                
  13.                 void Time1()
  14.                 {
  15.                         cout << "文件创建的时间!!!!" << endl;;
  16.                 }
  17.                
  18.                 void Author1()
  19.                 {
  20.                         cout << "文件的作者!!!!" << endl;
  21.                 }
  22.                
  23.         private:
  24.                 int m_B;
  25.                
  26.         protected:
  27.                 int m_C;
  28. };
  29. //继承的方式有三种:公共继承    保护继承    私有继承
  30. //class 子类 : 继承方式 父类           

  31. //公共继承   public
  32. class File1 : public Base1
  33. {
  34.         public:
  35.                 void Content1()
  36.                 {
  37.                         cout << "文件的内容!!!!!!" << endl;
  38.                 }
  39. };

  40. //公共继承  public
  41. class File2 : public Base1
  42. {
  43.         public:
  44.                 void test1()
  45.                 {
  46.                         int m_A = 10;//父类中的公共权限成员----子类->公共权限
  47.                         int m_B = 10;//父类中的私有权限成员----子类->访问不到
  48.                         int m_C = 10;//父类中的保护权限成员----子类->保护权限
  49.                 }
  50. }

  51. //私有继承   private
  52. class File3 : private Base1
  53. {
  54.         public:
  55.                 void test2()
  56.                 {
  57.                         int m_A = 11;//父类中的公共权限成员----子类->私有权限
  58.                         int m_B = 11;//父类中的私有权限成员----子类->访问不到
  59.                         int m_C = 11;//父类中的保护权限成员----子类->私有权限
  60.                 }
  61. }

  62. //保护继承   protected
  63. class File4 : protected Base1
  64. {
  65.         public:
  66.                 void test3()
  67.                 {
  68.                         int m_A = 12;//父类中的公共权限成员----子类->保护权限
  69.                         int m_B = 12;//父类中的私有权限成员----子类->访问不到
  70.                         int m_C = 12;//父类中的保护权限成员---- 子类->保护权限
  71.                 }
  72. }

  73. int main()
  74. {
  75.         File1 f1;
  76.         f1.Content1();
  77.         f1.Headline1();
  78.         f1.Time1();
  79.         f1.Author1();
  80.         
  81.         File2 f2;
  82.         f2.m_A = 100;//公共权限---可以访问到
  83.         f2.m_C = 100;//保护权限---类外访问不到
  84.         
  85.         File3 f3;
  86.         f3.m_A = 110;//私有权限---访问不到
  87.         
  88.         File4 f4;
  89.         f4.m_A = 120;//保护权限---类外访问不到
  90.         
  91.         return 0;
  92. }
复制代码
         继承中的对象模型
  1. #include<iostream>

  2. using namespace std;

  3. class Base2
  4. {
  5.         public:
  6.                 int m_A;
  7.                
  8.                 Base2()
  9.                 {
  10.                         cout << "Base2 的构造函数!!!!" << endl;
  11.                 }
  12.                
  13.                 ~Base2()
  14.                 {
  15.                         cout << "Base2 的析构函数!!!!" << endl;
  16.                 }        
  17.         
  18.         private:
  19.                 int m_B;
  20.         protected:
  21.                 int m_C;
  22. };

  23. class Box : public Base2
  24. {
  25.         public:
  26.                 int m_D;
  27.                
  28.                 Box()
  29.                 {
  30.                         cout << "Box 的构造函数!!!!" << endl;
  31.                 }
  32.                
  33.                 ~Box()
  34.                 {
  35.                         cout << "Box 的析构函数!!!!" << endl;
  36.                 }
  37. };

  38. int main()
  39. {
  40.         //16
  41.         //父类中所有非静态成员属性都会被子类继承下去
  42.         //父类中私有成员属性--是被编译器给隐藏了,因此访问不到,但确实被继承下去了
  43.         cout << "size of Box : " << sizeof(Box) << endl;
  44.         
  45.         //继承中的构造和析构顺序如下:
  46.         //先构造父类,在构造子类,析构的顺序与构造的顺序相反
  47.         Base2 b1;
  48.         Box b2;
  49.         
  50.         return 0;
  51. }
复制代码
         同名成员
          访问子类同名成员-----直接访问                     访问父类同名成员-----需要加作用域
          静态同名成员访问方式:    通过对象    通过类名
  1. #include<iostream>

  2. using namespace std;

  3. class Base3
  4. {
  5.         public:
  6.                 int m_A;
  7.                 static int m_B;
  8.                
  9.                 Base3()
  10.                 {
  11.                         m_A = 10;
  12.                 }
  13.                
  14.                 void magic()
  15.                 {
  16.                         cout << "Base3 中的magic()!!!!" << endl;
  17.                 }
  18.                
  19.                 void magic(int a)
  20.                 {
  21.                         cout << "Base3 中的magic(int a)!!!!" << endl;
  22.                 }
  23.                
  24.                 static void cute()
  25.                 {
  26.                         cout << "Base3 中的静态cute()!!!!" << endl;
  27.                 }
  28. };

  29. int Base3::m_B = 20;

  30. class Contemptible : public Base3
  31. {
  32.         public:
  33.                 int m_A;
  34.                 static int m_B;
  35.                
  36.                 Contemptible()
  37.                 {
  38.                         m_A = 100;
  39.                 }
  40.                
  41.                 void magic()
  42.                 {
  43.                         cout << "Contemptible 中的magic()!!!!" << endl;
  44.                 }        
  45.                
  46.                 static void cute()
  47.                 {
  48.                         cout << "Contemptible 中的静态cute()!!!!" << endl;
  49.                 }
  50. };

  51. int Contemptible::m_B = 200;

  52. //同名成员属性
  53. void test1()
  54. {
  55.         //如果成员属性有重名
  56.         Contemptible p1;
  57.         cout <<"子类中的成员属性 : " << p1.m_A << endl;//直接调用
  58.         cout <<"父类中的成员属性 : " <<  p1.Base3::m_A << endl;
  59.         //如果通过子类对象访问到父类中同名成员,需要加作用域  ::
  60. }

  61. //同名成员函数
  62. void test2()
  63. {
  64.         //如果成员函数有重名
  65.         Contemptible p2;
  66.         p2.magic();//直接调用,调用的是子类中的同名成员
  67.         p2.Base3::magic();// 如果通过子类对象访问到父类中同名函数,需要加作用域  ::
  68.         //如果子类中出现和父类同名的成员函数,子类的同名成员会隐藏掉父类中所有同名成员函数
  69.         //如果想访问到父类中被隐藏的同名成员函数,需要加作用域::
  70.         p2.Base3::magic(1);
  71. }

  72. void test3()
  73. {
  74.         //1、通过对象访问
  75.         cout << "通过对象来访问!!!" << endl;
  76.         Contemptible p3;
  77.         cout <<"子类中的静态成员属性 : " << p3.m_B << endl;
  78.         cout <<"父类中的静态成员属性 : " <<  p3.Base3::m_B << endl;
  79.         
  80.         //2、通过类名来访问
  81.         cout << "通过类名来访问!!!!" << endl;
  82.         cout <<"子类中的成员属性 : " << Contemptible::m_B << endl;
  83.         //第一个::代表通过类名的方式访问  第二个::代表访问父类作用域下
  84.         cout <<"父类中的成员属性 : " <<  Contemptible::Base3::m_B << endl;
  85. }

  86. void test4()
  87. {
  88.         //如果子类中出现和父类同名的静态成员函数,子类的静态同名成员会隐藏掉父类中所有静态同名成员函数
  89.         //如果想访问到父类中被隐藏的静态同名成员函数,需要加作用域::
  90.         
  91.         //通过对象访问
  92.         Contemptible p4;
  93.         p4.cute();
  94.         p4.Base3::cute();
  95.         
  96.         //通过类名访问
  97.         Contemptible::cute();
  98.         Contemptible::Base3::cute();
  99. }

  100. int main()
  101. {
  102.         test1();
  103.         
  104.         test2();
  105.         
  106.         test3();
  107.         
  108.         test4();
  109.         
  110.         return 0;
  111. }
复制代码
        多继承
  1. #include<iostream>

  2. using namespace std;

  3. class Library1
  4. {
  5.         public:
  6.                 int m_A;
  7.                
  8.                 Library1()
  9.                 {
  10.                         m_A = 10;
  11.                 }
  12. };

  13. class Library2
  14. {
  15.         public:
  16.                 int m_B;
  17.                 int m_A;
  18.                
  19.                 Library2()
  20.                 {
  21.                         m_B = 20;
  22.                         m_A = 101;
  23.                 }
  24. };

  25. //子类----需要继承Library1和Library2
  26. //class 子类 : 继承方式 父类1,继承方式 父类2.......
  27. class Book : public Library1, public Library2
  28. {
  29.         public:
  30.                 int m_C;
  31.                 int m_D;
  32.                
  33.                 Book()
  34.                 {
  35.                         m_C = 30;
  36.                         m_D = 40;
  37.                 }
  38. };

  39. void test1()
  40. {
  41.         Book p1;
  42.        
  43.         cout << "size of p1 : " << sizeof(p1) << endl;
  44.        
  45.         //当父类中出现同名成员,需要加作用域区分
  46.         cout << "Library1::m_A : " << p1.Library1::m_A << endl;
  47.         cout << "Library2::m_A : " << p1.Library2::m_A << endl;
  48. }

  49. int main()
  50. {
  51.         test1();
  52.        
  53.         return 0;
  54. }
复制代码

                                                    渣渣一个望各位大佬指点指点











评分

参与人数 1鱼币 +3 收起 理由
永恒的蓝色梦想 + 3 谢谢楼主

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-1 07:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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