• 【C++】公有_保护_私有_继承—代码学习记录


    1. //继承
    2. #include
    3. #include
    4. using namespace std;
    5. // 基类people
    6. class people {
    7. private:
    8. int number;
    9. string name;
    10. int age;
    11. protected:
    12. void fun();
    13. public:
    14. people(int num, string n, int a);
    15. ~people() { cout << "people类被析构" << endl; }
    16. void getpeople();
    17. };
    18. people::people(int num, string n, int a) {
    19. this->number = num;
    20. this->name = n;
    21. this->age = a;
    22. }
    23. void people::getpeople() {
    24. cout << "number:" << this->number << "name:" << this->name << "age:" << this->age << endl;
    25. }
    26. void people::fun() {
    27. cout << "This is people protected" << endl;
    28. }
    29. // 公有继承
    30. // 基类公有和保护成员的访问属性在派生类中不变
    31. class student : public people{
    32. private:
    33. int grade;
    34. protected:
    35. void fun2();
    36. public:
    37. student(int grade, int num, string name, int a);
    38. ~student();
    39. void getstudent();
    40. };
    41. void student::fun2() {
    42. this->fun(); // 访问基类保护类成员
    43. cout << "This is student protected" << endl;
    44. }
    45. student::student(int grade, int num, string name, int a) : grade(grade), people(num, name, a) {}
    46. student::~student() {
    47. cout << "student 类被析构" << endl;
    48. //不用手动调用基类析构函数,系统会自己调用
    49. }
    50. void student::getstudent() {
    51. this->getpeople(); // 访问基类公有成员
    52. cout << "grade:" << this->grade << endl;
    53. }
    54. // 保护继承
    55. // 基类公有成员和保护成员都以保护成员身份出现在派生类中,基类私有成员不可直接被派生类访问
    56. // 派生类可以访问保护类,外部使用者无法访问
    57. class teacher :protected people {
    58. private:
    59. int year;
    60. protected:
    61. void fun3() { cout << "This is teacher protected"; };
    62. public:
    63. teacher(int year, int num, string name, int a) :year(year), people(num, name, a) {}
    64. ~teacher() { cout << "teacher类被析构" << endl;; }
    65. void getteacher();
    66. };
    67. void teacher::getteacher() {
    68. this->getpeople(); //调用基类继承而来保护类
    69. // this->fun(); 可以
    70. cout << "year:" << year << endl;
    71. }
    72. //私有继承
    73. //基类公有和保护成员以派生类私有成员身份出现,只能被派生类访问
    74. //与保护继承的异同
    75. //同:都只能被派生类访问,不能被外部成员访问
    76. //异:派生类作为基类继续派生时,私有成员会被隐藏,不能被派生类的派生类访问,但是公有成员可以
    77. class college :private people {
    78. private:
    79. int score;
    80. protected:
    81. int fun4() { cout << "This is college protected" << endl; }
    82. public:
    83. college(int score, int num, string name, int a) :score(score), people(num, name, a) {}
    84. ~college() { cout << "college 类被析构" << endl; }
    85. void getcollege();
    86. };
    87. void college::getcollege() {
    88. this->getpeople(); //访问继承来的私有成员
    89. cout << "score:" << score << endl;
    90. }
    91. int main() {
    92. student zhang(3, 1, "xiaozhang", 18);
    93. zhang.getpeople(); //外部访问由基类公有继承而来的派生类公有成员
    94. zhang.getstudent(); //访问派生类的公有成员
    95. // zhang.fun(); 不可访问保护类成员(由基类公有继承而来)
    96. // zhang.fun2(); 类外不能访问保护类成员
    97. teacher yi(10, 2, "xiaoyi", 22);
    98. yi.getteacher(); //派生类公有
    99. // yi.fun3(); 不能调用派生类保护成员
    100. // yi.getpeople(); 不能调用基类保护继承来的保护成员
    101. college xing(100, 3, "xiaoxing", 19);
    102. xing.getcollege(); //自己的公有成员
    103. // xing.getpeople(); //不能访问继承来的私有成员
    104. return 0;
    105. }

  • 相关阅读:
    【Java】BIO,NIO,AIO聊天室示例
    VOLTE呼叫流程介绍
    遍历链。遍历链。
    数据结构学习笔记(第八章 排序-内部排序)
    JAVA基础算法(11)----- 最大二叉树
    Docker容器+DPDK+virtio网卡的使用与测试
    Linux系统编程——进程间通信的学习
    两个有序数组的合并(python)
    Linux CentOS7 vim宏操作
    使用Python进行Web开发:从基础到实战
  • 原文地址:https://blog.csdn.net/2302_79596028/article/details/138209834