C++ Advanced (7)-Inheritance and Derivation 5

Construction and destruction of derived classes

Constructor of derived class

  • Default

    • The constructor of the base class is not inherited;
    • Derived classes need to define their own constructor.
  • C++11 regulations

    • The using statement can be used to inherit the base class constructor.
    • But only the members inherited from the base class can be initialized.
      • New members of the derived class can be initialized with the initial value in the class.
    • Grammatical form:
      • using B::B;

Suggest

  • If the derived class has its own new members and needs to be initialized by the constructor, the derived class must customize the constructor.

If you do not inherit the constructor of the base class

  • New members of the derived class: initialize the derived class definition constructor;
  • Inherited members: automatically call the base class constructor to initialize;
  • The constructor of the derived class needs to pass parameters to the constructor of the base class.

Single inheritance

  • The case where a derived class has only one direct base class is single inheritance. In single inheritance, the constructor of the derived class only needs to pass parameters to a direct base class constructor.

The definition syntax of the constructor in single inheritance

派生类名::派生类名(基类所需的形参,本类成员所需的形参):
基类名(参数表), 本类成员初始化列表
{
	//其他初始化;
};

Examples of constructors in single inheritance

#include<iostream>
using namespace std;
class B {
public:
    B();
    B(int i);
    ~B();
    void print() const;
private:
    int b;
};

B::B() {
    b=0;
    cout << "B's default constructor called." << endl;
}
B::B(int i) {
    b=i;
    cout << "B's constructor called." << endl;
}
B::~B() {
    cout << "B's destructor called." << endl;
}
void B::print() const {
    cout << b << endl;
}

class C: public B {
public:
    C();
    C(int i, int j);
    ~C();
    void print() const;
private:
    int c;
};
C::C() {
    c = 0;
    cout << "C's default constructor called." << endl;
}
C::C(int i,int j): B(i), c(j){
    cout << "C's constructor called." << endl;
}

C::~C() {
    cout << "C's destructor called." << endl;
}
void C::print() const {
    B::print();
    cout << c << endl;
}

int main() {
    C obj(5, 6);
    obj.print();
    return 0;
}

Multiple inheritance

  • In the case of multiple inheritance, there are multiple direct base classes. If the constructor of the base class is not inherited, the derived class constructor needs to pass parameters to all base class constructors. Let's take a look at the grammar rules

The definition syntax of the constructor in multiple inheritance

派生类名::派生类名(参数表) : 
基类名1(基类1初始化参数表), 
基类名2(基类2初始化参数表), 
...
基类名n(基类n初始化参数表), 
本类成员初始化列表
{
        //其他初始化;
};

Derived class and base class constructor

  • When the base class has a default constructor
    • The derived class constructor may not pass parameters to the base class constructor.
    • When constructing an object of a derived class, the default constructor of the base class will be called.
  • To execute the constructor with parameters in the base class
    • The derived class constructor should provide parameters for the base class constructor.

Multi-inheritance and object members derived constructor definition syntax

派生类名::派生类名(形参表):
基类名1(参数), 基类名2(参数), ..., 基类名n(参数), 
本类成员(含对象成员)初始化列表
{
        //其他初始化
};

Execution order of constructor

  1. Call the base class constructor.
    • The order is in the order in which they were declared when they were inherited (from left to right).
  2. Initialize the members in the initialization list.
    • The order follows the order in which they are defined in the class.
    • When an object member is initialized, the constructor of the class to which it belongs is automatically called. The parameters are provided by the initialization list.
  3. Execute the content in the constructor body of the derived class.

Examples of derived class constructors

#include <iostream>
using namespace std;
class Base1 {//基类Base1,构造函数有参数
public:
    Base1(int i) 
  { cout << "Constructing Base1 " << i << endl; }
};
class Base2 {//基类Base2,构造函数有参数
public:
    Base2(int j) 
  { cout << "Constructing Base2 " << j << endl; }
};
class Base3 {//基类Base3,构造函数无参数
public:
    Base3() 
  { cout << "Constructing Base3 *" << endl; }
};

class Derived: public Base2, public Base1, public Base3 {
public: 
    Derived(int a, int b, int c, int d): Base1(a), member2(d), member1(c), Base2(b)
  //此处的次序与构造函数的执行次序无关
    { }
private:
    Base1 member1;
    Base2 member2;
    Base3 member3;
};

int main() {
    Derived obj(1, 2, 3, 4);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_41023026/article/details/108568906