C++ inheritance and derivation (personal summary notes)

1. Inheritance and Derivation

1. The concept and syntax of C++ inheritance

  • Inheritance is the relationship between classes, is a very simple and intuitive concept, similar to inheritance in the real world, such as a son inheriting his father's property.

  • InheritanceIt can be understood as a process in which a class obtains member variables and member functions from another class. For example, class B inherits from class A, then B has member variables and member functions of A.Inherited classes are called parent or base classes, and inherited classes are called subclasses or derived classes.

  • Inheritance methods include public (public), private (private) and protected (protected). This option is optional. If not written, the default is private.

  • demo syntax

class derived class name: [inheritance] base class name { newly added members of the derived class };

//基类 Pelple
class People{
    
    

}
//派生类 Student
class Student: public People{
    
    

}

The Student class inherits the member member functions of the People class. These inherited members can be accessed through subclass objects, just like their own.

2. C++ inheritance permissions and inheritance methods

public, protected, private specify inheritance methods.
Different inheritance methods will affect the access rights of base class members in derived classes.

  1. public inheritance
  • All public members in the base class are public properties in the derived class;
  • All protected members in the base class are protected attributes in the derived class;
  • All private members in the base class cannot be used in the derived class.
  1. protected inheritance
  • All public members in the base class are protected properties in the derived class;
  • All protected members in the base class are protected properties in the derived class;
  • All private members in the base class cannot be used in the derived class.
  1. private inheritance
  • All public members in the base class are private properties in the derived class;
  • All protected members in the base class are private properties in the derived class;
  • All private members in the base class cannot be used in the derived class.
    insert image description here
  • Since the private and protected inheritance methods will change the access rights of the base class members in the derived class, the inheritance relationship is complicated.So in actual development, we generally use public.
  • Changing access rights
    The using keyword can change the access rights of base class members in derived classes, such as changing public to private and protected to public.

3. Name shadowing in C++ inheritance

If the members of the derived class (including member variables and member functions) have the same name as the members of the base class, the members inherited from the base class will be shadowed. The so-called shadowing means that when the member is used in a derived class (including the use when defining the derived class, and also accessing the member through the derived class object), it actually uses the new member of the derived class instead of inheriting from the base class. come.

  • Base class member functions and derived class member functions do not constitute overloading
    When the names of base class members and derived class members are the same, it will cause shadowing. This sentence is easy to understand for member variables, but attention should be paid to member functions.Regardless of the parameters of the function, as long as the name is the same, it will cause shadowing. In other words, the base class member function and the derived class member function do not constitute overloading. If the derived class has a function of the same name, it will shadow all functions of the same name in the base class, regardless of whether their parameters are the same.

4. C++ derived class constructor

  • Member functions of the base class can be inherited and accessed through objects of the derived class;
  • Class constructors cannot be inherited.
  • Call the base class's constructor in the derived class's constructor.
Student::Student(char *name, int age, float score): People("小明", 16), m_score(score){
    
     }
  • The constructor of a derived class can only call the constructor of the direct base class, not the indirect base class.
  • When creating an object through a derived class, the constructor of the base class must be called, which is a syntax requirement.In other words, it is better to specify the base class constructor when defining the derived class constructor; if not specified, the base class default constructor (constructor without parameters) is called; if there is no default constructor, the compilation fails.

5. Destructor of derived class

  • Like constructors, destructors cannot be inherited. (The programmer is not required to display the call, the compiler is responsible)
  • The execution order of outer destructors and constructors are also reversed.
    • When creating a derived class object, the execution order of the constructors is the same as the inheritance order, that is, the base class constructor is executed first, and then the derived class constructor is executed.
    • while destroying the derived class object, the execution order of the destructor is opposite to the inheritance order, that is, the derived class destructor is executed first, and then the base class destructor is executed.

6. C++ virtual inheritance and virtual base classes

  • Multiple inheritance (Multiple Inheritance) refers to the ability to generate derived classes from multiple direct base classes. Multiple inheritance derived classes inherit the members of all parent classes. (Problem-prone, naming conflicts.)
  • Virtual Inheritance (Virtual Inheritance)
    In order to solve the problem of naming conflicts and redundant data in multiple inheritance, C++ proposes virtual inheritance, so that only a member of the indirect base class is retained in the derived class.Adding the virtual keyword in front of the inheritance method is virtual inheritance
//间接基类A
class A{
    
    
protected:
    int m_a;
};
//直接基类B
class B: virtual public A{
    
      //虚继承
protected:
    int m_b;
};
  • The purpose of virtual inheritance is for a class to make a declaration that it is willing to share its base classes. Among them, this shared base class is calledVirtual Base Class

7. Constructor in C++ virtual inheritance

  • virtual inheritance, the virtual base class is defined byfinal derived classInitialized, in other words, the constructor of the final derived class must call the constructor of the virtual base class. For the final derived class, a virtual base class is an indirect base class, not a direct base class.
  • This is different from ordinary inheritance, inOrdinary inheritancemiddle,Derived class constructors can only call direct base class constructors, cannot call indirect base classes.
D::D(int a, int b, int c, int d): A(a), B(90, b), C(100, c), m_d(d){
    
     }
void D::display(){
    
    
    cout<<"m_a="<<m_a<<", m_b="<<m_b<<", m_c="<<m_c<<", m_d="<<m_d<<endl;
}
  • example:
    • In the constructor of the final derived class D, in addition to calling the constructors of B and C, the constructor of A is also called, which means that D is not only responsible for initializing the direct base classes B and C, but also the indirect base class A.
    • In the past ordinary inheritance, the constructor of the derived class is only responsible for initializing its direct base class, and then the indirect base class is initialized by the constructor of the direct base class

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324129305&siteId=291194637