By default
- The constructor of the base class is not inherited
- Derived classes need to define their own constructors
C++11 (not recommended)
- The using statement can inherit the base class constructor;
- Only members inherited from a base class can be initialized.
- Syntax form: using B::B;
proposal
- If the derived class has its own newly added members and needs to be initialized through the constructor, the derived class needs to customize the constructor.
If you do not inherit the constructor of the base class
- New member of derived class: derived class defines constructor initialization;
- Inherited members: automatically call the base class constructor for initialization;
- The constructor of the derived class needs to pass parameters to the constructor of the base class.
Single inheritance
When a derived class has only one direct base class, it is single inheritance. In single inheritance, the constructor of a derived class only needs to pass parameters to a direct base class constructor.
Definition syntax of constructor in single inheritance
Derived class name:: derived class name (formal parameters required by the base class and members of this class):
Base class name (parameter table), initialization list of members of this class{
// Other initialization;
};
Constructor example for single inheritance
void print() const is a constant function that does not change the state of the object?
#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() { //Main function C obj(5,6); obj.print(); return 0; }
Multiple inheritance
Definition syntax of constructor in multi inheritance
Constructors for derived and base classes
When the base class has a default constructor
- Derived class constructors may not pass arguments to base class constructors.
When constructing an object of a derived class, the default constructor of the base class is called.
- To execute the constructor with parameters in the base class the derived class constructor should provide parameters for the base class constructor.
Class composition and inheritance
Constructor definition syntax derived when there are multiple inheritance and object members
Execution order of constructors
1. Call the base class constructor.
- The order is in the order they were declared when they were inherited (from left to right).
2. Initialize the members in the initialization list.
- The order is in the order they are defined in the class.
- When an object member initializes, the constructor of its class is automatically called. The parameters are provided by the initialization list.
3. Execute the contents of the constructor body of the derived class.
Example 7-4 example of derived class constructor
#include <iostream> using namespace std; class Base1{ public: Base1(int i){ cout<< "Constructing Base1 " << i << endl; }; }; class Base2{ public: Base2(int j){ cout << "Constructing Base2 " << j << endl; }; }; class Base3{ public: Base3(){ cout <<"Constructing Base3* " << endl; }; }; class Derived:public Base2,public Base1,public Base3{ //Derive a new class Derived. Pay attention to the order of base class names public: Derived(int a,int b,int c,int d):Base1(a),member2(d),member1(c),Base2(b){ //The order here is independent of the execution order of the constructor //Pay attention to the number and order of base class names and member object names }; private: Base1 member1; Base2 member2; Base3 member3; }; int main() { //Main function Derived obj(1,2,3,4); return 0; }
Derived class copy constructor
The case where the derived class does not define a copy constructor
-
The compiler generates an implicit copy constructor when needed;
-
First call the copy constructor of the base class;
-
Then copy the new members of the derived class.
A derived class defines the case of a copy constructor
-
Generally, you pass parameters to the copy constructor of the base class.
-
The copy constructor can only accept one parameter, which is used to initialize the members of the derived class definition and will also be passed to the copy constructor of the base class.
-
The copy constructor parameter type of the base class is a reference to the base class object, and the argument can be a reference to a derived class object
-
For example: C:: C (const C & C1): B (C1) {...}