• 深入浅出继承


    目录

    一、继承的概念

    二、继承的定义

    2.1 继承格式

    2.2 继承方式与访问限定符

    2.3 继承方式和访问限定符

    2.4 默认继承方式

    三、基类与派生类对象赋值转换

    四、继承中的作用域

    六、派生类默认成员函数

    七、继承与友元

    八、继承与静态成员


    一、继承的概念

    继承(inheritance)机制是面向对象程序设计使代码可以复用的重要的手段,其允许程序员在保持原有类特性的基础上进行扩展,增加功能。这样产生新的类被称为派生类
    继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程。之前接触的复用都是函数复用,而继承便是类设计层次的复用

    以下代码中Student类和Teacher类就继承了Person类:

    1. #include
    2. #include
    3. using namespace std;
    4. class Person
    5. {
    6. public:
    7. void Print() {
    8. cout << "name:" << _name << endl;
    9. cout << "age:" << _age << endl;
    10. }
    11. protected:
    12. string _name = "bjy";
    13. int _age = 18;
    14. };
    15. class Student : public Person
    16. {
    17. protected:
    18. int _stuid = 04;
    19. };
    20. class Teacher : public Person
    21. {
    22. protected:
    23. int _jobid = 05;
    24. };

    继承后,父类Person的成员(成员函数和成员变量),都会变成子类的一部分。即子类Student和Teacher复用了父类Person成员

    二、继承的定义

    2.1 继承格式

    在继承中,父类也称为基类,子类是由基类派生而来的,所以子类又称为派生类

    2.2 继承方式与访问限定符

    访问限定符有以下三种:

    1. public访问
    2. protected访问
    3. private访问

    继承的方式有三种:

    1. public继承
    2. protected继承
    3. private继承

    2.3 继承方式和访问限定符

    基类中被不同访问限定符修饰的成员,以不同的继承方式继承到派生类中后,该成员最终在派生类中的访问方式将会发生变化

    1. 在基类中的访问方式为public或protected的成员,在派生类中的访问方式变为:Min(成员在基类的访问方式,继承方式)
    2. 在基类中的访问方式为private的成员,在派生类中都是不可见的

    基类的private成员在派生类中不可见是什么意思?

    即无法在派生类中访问基类的private成员。如:虽然Student类继承了Person类,但无法在Student类中访问Person类中的private成员_name

    基类的private成员无论以什么方式继承,在派生类中都是不可见的,这里的不可见是指基类的私有成员虽然被继承到了派生类对象中,但是语法上限制派生类对象不管在类内还是类外都不能访问

    基类的private成员在派生类中是不能被访问的,若基类成员不想在类外直接被访问,但要在派生类中能访问,就需要定义为protected

    注意: 在实际运用中一般使用的都是public继承,几乎很少使用protected和private继承,也不提倡使用protected和private继承,因为使用protected和private继承下来的成员都只能在派生类的类里面使用,实际中扩展维护性不强

    2.4 默认继承方式

    在使用继承时可以不指定继承方式,使用关键字class时默认的继承方式是private,使用struct时默认的继承方式是public

    1. //基类
    2. class Person
    3. {
    4. public:
    5. string _name = "张三"; //姓名
    6. };
    7. //派生类
    8. class Student : Person //默认为private继承
    9. {
    10. protected:
    11. int _stuid; //学号
    12. };
    1. //基类
    2. class Person
    3. {
    4. public:
    5. string _name = "张三"; //姓名
    6. };
    7. //派生类
    8. struct Student : Person //默认为public继承
    9. {
    10. protected:
    11. int _stuid; //学号
    12. };

    三、基类与派生类对象赋值转换

    派生类对象可以赋值给基类的对象或引用,派生类的地址可以赋值给基类的指针。即赋值兼容规则,在这个过程中会发生基类和派生类对象间的赋值转换

    1. //基类
    2. class Person
    3. {
    4. protected:
    5. string _name; //姓名
    6. string _sex; //性别
    7. int _age; //年龄
    8. };
    9. //派生类
    10. class Student : public Person
    11. {
    12. protected:
    13. int _stuid; //学号
    14. };
    1. Student s;
    2. Person p = s; //派生类对象赋值给基类对象
    3. Person* ptr = &s; //派生类对象的地址赋值给基类指针
    4. Person& ref = s; //派生类对象赋值给基类引用

    这种做法被称为切片/切割,即为将派生类中基类那部分切来赋值过去

    派生类对象赋值给基类对象图示

    派生类对象赋值给基类指针图示

    派生类对象赋值给基类引用图示

    注意:基类对象不能赋值给派生类对象,基类的指针或引用可以通过强制类型转换赋值给派生类的指针或引用,但是基类的指针或引用必须是管理的派生类对象才是安全的

    四、继承中的作用域

    在继承体系中的基类和派生类都有独立的作用域。若子类和父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问。这种情况被称为隐藏,也称为重定义

    1. #include
    2. #include
    3. using namespace std;
    4. //父类
    5. class Person
    6. {
    7. protected:
    8. int _num = 111;
    9. };
    10. //子类
    11. class Student : public Person
    12. {
    13. public:
    14. void fun()
    15. {
    16. cout << _num << endl;
    17. }
    18. protected:
    19. int _num = 999;
    20. };
    21. int main()
    22. {
    23. Student s;
    24. s.fun(); //999
    25. return 0;
    26. }

    此时若要访问父类中的_num成员,可以使用作用域限定符进行指定访问

    1. void fun() {
    2. cout << Person::_num << endl; //指定访问父类作用域中的_num成员
    3. }

    若成员函数的隐藏,只需函数名相同就构成隐藏

    对于以下代码,调用成员函数fun时将直接调用子类中的fun,若想调用父类中的fun,则需使用作用域限定符指定类域

    1. #include
    2. #include
    3. using namespace std;
    4. //父类
    5. class Person
    6. {
    7. public:
    8. void fun(int x)
    9. {
    10. cout << x << endl;
    11. }
    12. };
    13. //子类
    14. class Student : public Person
    15. {
    16. public:
    17. void fun(double x)
    18. {
    19. cout << x << endl;
    20. }
    21. };
    22. int main()
    23. {
    24. Student s;
    25. s.fun(3.14); //直接调用子类中的成员函数fun
    26. s.Person::fun(20); //指定调用父类中的成员函数fun
    27. return 0;
    28. }

    注意:该代码中,父类中的fun和子类中的fun不构成函数重载,函数重载要求两个函数在同一作用域,而此时这两个fun函数并不在同一作用域。为了避免类似问题,实际在继承体系中最好不要定义同名的成员

    六、派生类默认成员函数

    默认成员函数,即程序员不写编译器也会自动生成的函数,类中的默认成员函数有以下六个

    以下面这个Person类为基类

    1. //基类
    2. class Person
    3. {
    4. public:
    5. //构造函数
    6. Person(const string& name = "peter") :_name(name)
    7. {
    8. cout << "Person()" << endl;
    9. }
    10. //拷贝构造函数
    11. Person(const Person& p) :_name(p._name)
    12. {
    13. cout << "Person(const Person& p)" << endl;
    14. }
    15. //赋值运算符重载函数
    16. Person& operator=(const Person& p)
    17. {
    18. cout << "Person& operator=(const Person& p)" << endl;
    19. if (this != &p) {
    20. _name = p._name;
    21. }
    22. return *this;
    23. }
    24. //析构函数
    25. ~Person() {
    26. cout << "~Person()" << endl;
    27. }
    28. private:
    29. string _name; //姓名
    30. };

    用Person基类派生出Student类,Student类中的默认成员函数的基本逻辑如下

    1. //派生类
    2. class Student : public Person
    3. {
    4. public:
    5. //构造函数
    6. Student(const string& name, int id)
    7. :Person(name) //调用基类的构造函数初始化基类的那一部分成员
    8. , _id(id) //初始化派生类的成员
    9. {
    10. cout << "Student()" << endl;
    11. }
    12. //拷贝构造函数
    13. Student(const Student& s)
    14. :Person(s) //调用基类的拷贝构造函数完成基类成员的拷贝构造
    15. , _id(s._id) //拷贝构造派生类的成员
    16. {
    17. cout << "Student(const Student& s)" << endl;
    18. }
    19. //赋值运算符重载函数
    20. Student& operator=(const Student& s)
    21. {
    22. cout << "Student& operator=(const Student& s)" << endl;
    23. if (this != &s) {
    24. Person::operator=(s); //调用基类的operator=完成基类成员的赋值
    25. _id = s._id; //完成派生类成员的赋值
    26. }
    27. return *this;
    28. }
    29. //析构函数
    30. ~Student() {
    31. cout << "~Student()" << endl;
    32. //派生类的析构函数会在被调用完成后自动调用基类的析构函数
    33. }
    34. private:
    35. int _id; //学号
    36. };

    派生类与普通类的默认成员函数的不同之处概括为以下几点:

    1. 派生类的构造函数被调用时,会自动调用基类的构造函数初始化基类的那一部分成员。若基类中没有默认构造函数,则须在派生类构造函数的初始化列表中显示调用基类构造函数
    2. 派生类的拷贝构造函数必须调用基类的拷贝构造函数完成基类成员的拷贝构造
    3. 派生类的赋值运算符重载函数必须调用基类的赋值运算符重载函数完成基类成员的赋值
    4. 派生类的析构函数会在被调用完成后自动调用基类的析构函数清理基类成员
    5. 派生类对象初始化时,会先调用基类的构造函数再调用派生类的构造函数
    6. 派生类对象在析构时,会先调用派生类的析构函数再调用基类的析构函数

    在编写派生类的默认成员函数时,需要注意以下几点:

    1. 派生类和基类的赋值运算符重载函数因为函数名相同构成隐藏,因此在派生类中调用基类的赋值运算符重载函数时,需使用作用域限定符进行指定调用
    2. 由于多态的某些原因,任何类的析构函数名都会被统一处理为destructor();。因此,派生类和基类的析构函数也会因为函数名相同构成隐藏,若需要在某处调用基类的析构函数,那么就要使用作用域限定符进行指定调用
    3. 在 派生类的拷贝构造函数和operator=函数 中调用 基类的拷贝构造函数和operator=函数 的传参方式是一个切片行为,都是将派生类对象直接赋值给基类的引用

    说明:

    • 基类的构造函数、拷贝构造函数、赋值运算符重载函数都可以在派生类当中自行进行调用,而基类的析构函数是当派生类的析构函数被调用后由编译器自动调用的,若是自行调用基类的构造函数就会导致基类被析构多次的问题
    • 创建派生类对象时是先创建的基类成员再创建的派生类成员,编译器为了保证析构时先析构派生类成员再析构基类成员的顺序,所以编译器会在派生类的析构函数被调用后自动调用基类的析构函数

    七、继承与友元

    友元关系不能继承,也就是说基类的友元可以访问基类的私有和保护成员,但是不能访问派生类的私有和保护成员(友元关系不可被继承)

    以下代码中Display函数是基类Person的友元,但是Display函数不是派生类Student的友元,即Display函数无法访问派生类Student中的私有和保护成员

    1. #include
    2. #include
    3. using namespace std;
    4. class Student;
    5. class Person
    6. {
    7. public:
    8. //声明Display是Person的友元
    9. friend void Display(const Person& p, const Student& s);
    10. protected:
    11. string _name; //姓名
    12. };
    13. class Student : public Person
    14. {
    15. protected:
    16. int _id; //学号
    17. };
    18. void Display(const Person& p, const Student& s)
    19. {
    20. cout << p._name << endl; //可以访问
    21. cout << s._id << endl; //无法访问
    22. }
    23. int main()
    24. {
    25. Person p;
    26. Student s;
    27. Display(p, s);
    28. return 0;
    29. }

    若想让Display函数也能够访问派生类Student的私有和保护成员,只能在派生类Student中进行友元声明

    1. class Student : public Person
    2. {
    3. public:
    4. //声明Display是Student的友元
    5. friend void Display(const Person& p, const Student& s);
    6. protected:
    7. int _id; //学号
    8. };

    八、继承与静态成员

    若基类中定义了一个static静态成员变量,则在整个继承体系里面只有一个该静态成员。无论派生出多少个子类,都只有一个static成员实例

    如:在基类Person中定义了静态成员变量_count,尽管Person又被派生类Student和Graduate继承,但在整个继承体系里面只有一个该静态成员
    若是在基类Person的构造函数和拷贝构造函数中设置_count进行自增,那么就可以随时通过_count来获取该时刻已经实例化的Person、Student以及Graduate对象的总个数

    1. #include
    2. #include
    3. using namespace std;
    4. //基类
    5. class Person
    6. {
    7. public:
    8. Person() { _count++; }
    9. Person(const Person& p) { _count++; }
    10. protected:
    11. string _name; //姓名
    12. public:
    13. static int _count; //统计人的个数
    14. };
    15. int Person::_count = 0; //静态成员变量在类外进行初始化
    16. //派生类
    17. class Student : public Person
    18. {
    19. protected:
    20. int _stuNum; //学号
    21. };
    22. //派生类
    23. class Graduate : public Person
    24. {
    25. protected:
    26. string _seminarCourse; //研究科目
    27. };
    28. int main()
    29. {
    30. Student s1;
    31. Student s2(s1);
    32. Student s3;
    33. Graduate s4;
    34. cout << Person::_count << endl; //4
    35. cout << Student::_count << endl; //4
    36. return 0;
    37. }

    也可以通过打印Person类和Student类中静态成员_count的地址来证明就是同一个变量

    1. cout << &Person::_count << endl; //00F1F320
    2. cout << &Student::_count << endl; //00F1F320

  • 相关阅读:
    Java的CompletableFuture,Java的多线程开发
    C. Salyg1n and the MEX Game Codeforces Round 897 (Div. 2)
    阶段六-Day02-Maven
    台湾飞凌FM8PB513B单片机提供单片机方案开发 产品设计
    CP AUTOSAR值Ethernet Test Module(TC8)
    【八股总结】至今为止遇到的八股(上半)
    阶段性工作总结
    深度学习第三章
    Android 沉浸式状态栏
    目标检测工程化最佳实践:Python 并行条件下YOLOv8的模型推理,线程安全的模型推理!
  • 原文地址:https://blog.csdn.net/GG_Bruse/article/details/134237137