• 继承(个人学习笔记黑马学习)


    1、基本语法

    1. #include
    2. using namespace std;
    3. #include
    4. //普通实现页面
    5. //Java页面
    6. //class Java {
    7. //public:
    8. // void header() {
    9. // cout << "首页、公开课、登录、注册...(公共头部)" << endl;
    10. // }
    11. // void footer() {
    12. // cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
    13. // }
    14. // void left() {
    15. // cout << "Java、python、c++...(公共分类列表)" << endl;
    16. // }
    17. // void content() {
    18. // cout << "Java学科视频" << endl;
    19. // }
    20. //};
    21. //
    22. //
    23. Python页面
    24. //class Python {
    25. //public:
    26. // void header() {
    27. // cout << "首页、公开课、登录、注册...(公共头部)" << endl;
    28. // }
    29. // void footer() {
    30. // cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
    31. // }
    32. // void left() {
    33. // cout << "Java、python、c++...(公共分类列表)" << endl;
    34. // }
    35. // void content() {
    36. // cout << "Python学科视频" << endl;
    37. // }
    38. //};
    39. //
    40. C++页面
    41. //class CPP {
    42. //public:
    43. // void header() {
    44. // cout << "首页、公开课、登录、注册...(公共头部)" << endl;
    45. // }
    46. // void footer() {
    47. // cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
    48. // }
    49. // void left() {
    50. // cout << "Java、python、c++...(公共分类列表)" << endl;
    51. // }
    52. // void content() {
    53. // cout << "C++学科视频" << endl;
    54. // }
    55. //};
    56. //继承实现页面
    57. //公共页面
    58. class BasePage {
    59. public:
    60. void header() {
    61. cout << "首页、公开课、登录、注册...(公共头部)" << endl;
    62. }
    63. void footer() {
    64. cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
    65. }
    66. void left() {
    67. cout << "Java、python、c++...(公共分类列表)" << endl;
    68. }
    69. };
    70. //Java页面
    71. class Java :public BasePage {
    72. public:
    73. void content() {
    74. cout << "Java学科视频" << endl;
    75. }
    76. };
    77. //Python页面
    78. class Python :public BasePage {
    79. public:
    80. void content() {
    81. cout << "Python学科视频" << endl;
    82. }
    83. };
    84. //C++页面
    85. class CPP :public BasePage {
    86. public:
    87. void content() {
    88. cout << "C++学科视频" << endl;
    89. }
    90. };
    91. void test01() {
    92. cout << "Java下载视频页面如下:" << endl;
    93. Java ja;
    94. ja.header();
    95. ja.footer();
    96. ja.left();
    97. ja.content();
    98. cout << "---------------------------" << endl;
    99. cout << "Python下载视频页面如下:" << endl;
    100. Python py;
    101. py.header();
    102. py.footer();
    103. py.left();
    104. py.content();
    105. cout << "---------------------------" << endl;
    106. cout << "C++下载视频页面如下:" << endl;
    107. CPP cpp;
    108. cpp.header();
    109. cpp.footer();
    110. cpp.left();
    111. cpp.content();
    112. }
    113. int main() {
    114. test01();
    115. system("pause");
    116. return 0;
    117. }


    2、继承方式

    继承方式一共有三种:

    • 公共继承
    • 保护继承
    • 私有继承
    1. #include
    2. using namespace std;
    3. #include
    4. //公共继承
    5. class Base1 {
    6. public:
    7. int m_A;
    8. protected:
    9. int m_B;
    10. private:
    11. int m_C;
    12. };
    13. class Son1 :public Base1 {
    14. void func() {
    15. m_A = 10;//父类中的公共权限成员 到子类中依然是公共权限
    16. m_B = 10;//父类中的保护权限成员 到子类中依然是保护权限
    17. //m_C = 10;//父类中的私有权限成员 子类访问不到
    18. }
    19. };
    20. void test01() {
    21. Son1 s1;
    22. s1.m_A = 100;
    23. //s1.m_B = 100;//到son1中 m_B是保护权限 类外访问不到
    24. }
    25. //保护继承
    26. class Base2 {
    27. public:
    28. int m_A;
    29. protected:
    30. int m_B;
    31. private:
    32. int m_C;
    33. };
    34. class Son2 :protected Base2 {
    35. public:
    36. void func() {
    37. m_A = 10;//父类中的公共权限成员 到子类中是保护权限
    38. m_B = 10;//父类中的保护权限成员 到子类中依然是保护权限
    39. //m_C = 10;//父类中的私有权限成员 子类访问不到
    40. }
    41. };
    42. void test02() {
    43. Son2 s1;
    44. //s1.m_A = 1000;//在son2中 m_A变为保护权限 因此类外访问不到
    45. //s1.m_B = 1000;//在son2中 m_B保护权限 不可以访问
    46. }
    47. //私有继承
    48. class Base3 {
    49. public:
    50. int m_A;
    51. protected:
    52. int m_B;
    53. private:
    54. int m_C;
    55. };
    56. class Son3 :private Base2 {
    57. public:
    58. void func() {
    59. m_A = 10;//父类中的公共权限成员 到子类中是私有权限
    60. m_B = 10;//父类中的保护权限成员 到子类中是私有权限
    61. //m_C = 10;//父类中的私有权限成员 子类访问不到
    62. }
    63. };
    64. class GrandSon3 :public Son3 {
    65. public:
    66. void func() {
    67. //m_A = 1000;//到了son3中 m_A变为私有 即使是儿子 也访问不到
    68. //m_B = 1000;//到了son3中 m_B变为私有 即使是儿子 也访问不到
    69. }
    70. };
    71. void test03() {
    72. Son3 s1;
    73. //s1.m_A = 1000;//在son3中 m_A变为私有权限 因此类外访问不到
    74. //s1.m_B = 1000;//在son3中 m_B私有权限 不可以访问
    75. }
    76. int main() {
    77. system("pause");
    78. return 0;
    79. }

    3、继承中的对象模型

    1. #include
    2. using namespace std;
    3. #include
    4. class Base {
    5. public:
    6. int m_A;
    7. protected:
    8. int m_B;
    9. private:
    10. int m_C;
    11. };
    12. class Son :public Base {
    13. public:
    14. int m_D;
    15. };
    16. //利用开发人员命令提示符工具查看对象模型
    17. //跳转盘符 F:
    18. //跳转文件路径 cd 具体路径下
    19. //查看命令
    20. // cl /d1 reportSingleClassLayout类名 文件名
    21. void test01() {
    22. //16
    23. //父类中所有非静态成员属性都会被子类继承下去
    24. //父类中私有成员属性 是被编译器给隐藏了 因此是访问不到,但是缺省被继承下去了
    25. cout << "size of Son = " << sizeof(Son) << endl;
    26. }
    27. int main() {
    28. system("pause");
    29. return 0;
    30. }

    4、继承中构造和析构顺序

    1. #include
    2. using namespace std;
    3. #include
    4. //继承中的构造和析构顺序
    5. class Base {
    6. public:
    7. Base() {
    8. cout << "Base的构造函数" << endl;
    9. }
    10. ~Base() {
    11. cout << "Base的析构函数" << endl;
    12. }
    13. };
    14. class Son :public Base {
    15. public:
    16. Son() {
    17. cout << "Son的构造函数" << endl;
    18. }
    19. ~Son() {
    20. cout << "Son的析构函数" << endl;
    21. }
    22. };
    23. void test01() {
    24. //继承的构造和析构顺序如下:
    25. //先构造父类 再构造子类,析构的顺序与构造的顺序相反
    26. Son s;
    27. }
    28. int main() {
    29. test01();
    30. system("pause");
    31. return 0;
    32. }

    5、同名成员处理

    1. #include
    2. using namespace std;
    3. #include
    4. class Base {
    5. public:
    6. Base() {
    7. m_A = 100;
    8. }
    9. void func() {
    10. cout << "Base - func()调用" << endl;
    11. }
    12. void func(int a) {
    13. cout << "Base - func(int a)调用" << endl;
    14. }
    15. int m_A;
    16. };
    17. class Son :public Base {
    18. public:
    19. Son() {
    20. m_A = 200;
    21. }
    22. void func() {
    23. cout << "Son - func()调用" << endl;
    24. }
    25. int m_A;
    26. };
    27. //同名成员属性
    28. void test01() {
    29. Son s;
    30. cout << "Son 下m_A = " << s.m_A << endl;
    31. //如果通过子类对象 访问到父类中同名成员 需要加作用域
    32. cout << "Base 下m_A = " << s.Base::m_A << endl;
    33. }
    34. //同名成员函数处理
    35. void test02() {
    36. Son s;
    37. s.func();//直接调用 调用是子类中的同名成员
    38. s.Base::func();//调用父类的
    39. //如果子类中出现和父类同名的成员函数,子类的同名成员会隐藏掉父类中所有同名的成员函数
    40. //如果想访问到父类中被隐藏的同名成员函数,需要加作用域
    41. //s.func(100); 错误
    42. s.Base::func(100);
    43. }
    44. int main() {
    45. //test01();
    46. test02();
    47. system("pause");
    48. return 0;
    49. }

    6、同名静态成员处理方法

    1. #include
    2. using namespace std;
    3. #include
    4. class Base {
    5. public:
    6. static int m_A;
    7. static void func() {
    8. cout << "Base - static void func()" << endl;
    9. }
    10. };
    11. int Base::m_A=100;
    12. class Son :public Base {
    13. public:
    14. static int m_A;
    15. static void func() {
    16. cout << "Son - static void func()" << endl;
    17. }
    18. };
    19. int Son::m_A = 200;
    20. //同名静态成员属性
    21. void test01() {
    22. //1、通过对象访问
    23. cout << "通过对象访问:" << endl;
    24. Son s;
    25. cout << " Son 下m_A = " << s.m_A << endl;
    26. cout << "Base 下m_A = " << s.Base::m_A << endl;
    27. //2、通过类名访问
    28. cout << "通过类名访问:" << endl;
    29. cout << "Son 下m_A = " << Son::m_A << endl;
    30. //第一个:代表通过类名方式访问 第二个:代表访问父类作用域下
    31. cout << "Base 下m_A = " << Son::Base::m_A << endl;
    32. }
    33. //同名静态成员函数
    34. void test02() {
    35. //1、通过对象访问
    36. cout << "通过对象访问:" << endl;
    37. Son s;
    38. s.func();
    39. s.Base::func();
    40. //2、通过类名访问
    41. cout << "通过类名访问:" << endl;
    42. Son::func();
    43. Son::Base::func();
    44. }
    45. int main() {
    46. //test01();
    47. test02();
    48. system("pause");
    49. return 0;
    50. }

    7、多继承语法

    1. #include
    2. using namespace std;
    3. #include
    4. class Base1 {
    5. public:
    6. Base1() {
    7. m_A = 100;
    8. }
    9. int m_A;
    10. };
    11. class Base2{
    12. public:
    13. Base2() {
    14. m_A = 200;
    15. }
    16. int m_A;
    17. };
    18. //子类 需要继承Base1 和Base2
    19. class Son :public Base1, public Base2 {
    20. public:
    21. Son() {
    22. m_C = 300;
    23. m_D = 400;
    24. }
    25. int m_C;
    26. int m_D;
    27. };
    28. void test01() {
    29. Son s;
    30. cout << "sizeof Son = " << sizeof(s) << endl;
    31. //当父类中出现同名成员,需要加作用域区分
    32. cout << "Base1::m_A = " << s.Base1::m_A << endl;
    33. cout << "Base2::m_A = " << s.Base2::m_A << endl;
    34. }
    35. int main() {
    36. test01();
    37. system("pause");
    38. return 0;
    39. }

    8、菱形继承

    菱形继承概念
    两个派生类继承同一个基类

    又有某个类同时继承者两个派生类

    这种继承被称为菱形继承,或者钻石继承

    1. #include
    2. using namespace std;
    3. #include
    4. //动物类
    5. class Animal {
    6. public:
    7. int m_Age;
    8. };
    9. //利用虚继承 解决菱形继承的问题
    10. //继承之前 加上关键字 virtual 变为虚继承
    11. // Animal类称为 虚基类
    12. //羊类
    13. class Sheep :virtual public Animal {
    14. };
    15. //驼类
    16. class Tuo :virtual public Animal {
    17. };
    18. //羊驼类
    19. class SheepTuo :public Sheep, public Tuo {
    20. };
    21. void test01() {
    22. SheepTuo st;
    23. st.Sheep::m_Age = 18;
    24. st.Tuo::m_Age = 28;
    25. //当菱形继承,两个父类拥有相同数据,需要加以作用域区分
    26. cout << "st.Sheep::m_Age = " << st.Sheep::m_Age << endl;
    27. cout << "st.Tuo::m_Age =" << st.Tuo::m_Age << endl;
    28. cout << "st.m_Age = " << st.m_Age << endl;
    29. //这份数据我们知道 只要有一份就可以,菱形继承导致数据有两份,资源浪费
    30. }
    31. int main() {
    32. test01();
    33. system("pause");
    34. return 0;
    35. }

  • 相关阅读:
    python基础教程
    XAF新手入门 - 类型子系统(Types Info Subsystem)
    SpringCloud - Spring Cloud Alibaba 之 SkyWalking 分布式链路跟踪;跨多服务追踪,集成日志(十五)
    人生的B计划:构建“一个人”的商业模式
    机器学习(西瓜书)第 7 章 贝叶斯分类器
    算法题:Find the closest common ancestor
    JSP总结
    Java中如何遍历Map中的key呢?
    Git学习笔记9
    VSCode 好用的插件分享
  • 原文地址:https://blog.csdn.net/m0_59848857/article/details/132676440