C++ Review Road 12 - The order of execution of constructors and destructors of class inheritance

Generally, when we define classes, we will define constructors and destructors. If we want to inherit these classes, we need to know the calling order of the constructors of the base class and the derived class, as well as the calling order of the destructors. (Take public inheritance as an example)

First, the calling sequence of the constructor

Let's first define two classes, the Child class inherits the Parent class through public.

class Parent
{
public:
	Parent(int a, int b);
	~Parent();
	void printP();
private:
	int m_a;
	int m_b;
};

class Child : public Parent
{
public:
	Child( int a, int b,  int c, int d);
	~Child();
	void printC();
private:
	int m_c;
	int m_d;
	int m_aa;
	int m_bb;
};

There are two private member variables in Parent class, m_a, m_b, and three common member functions, namely constructor, destructor, and a print function.

The Child class inherits the members of Parent through public methods, but does not inherit the constructor and destructor of the base class . In addition to the members of the inherited base class it also has some private member variables of its own. From the previous blog, we know that all properties of the base class remain unchanged after public inheritance.

We first declare a Child object.

Child c(1,2,3,4);

Because the base class defines a constructor with two parameters when we declare the base class, we must pass two parameters to the base class when we declare it. Since our Child inherits the Parent class, we are defining When the constructor of the Child class needs to call the constructor of the Parent class. So we need to define the constructor of the subclass like this.

Child::Child( int a, int b,  int c, int d):Parent(a, b)
{
	cout << "Constructor of Derived Class" << endl;
	m_c = c;
	m_d = d;
}

The Parent(a, b) after ":" is what we show to call the constructor of the Parent class , and then use a, and b to initialize the private member variables m_a, m_b of the Parent class respectively.

in conclusion:

To define an object of a derived class, the constructor of the base class is called first, and then the constructor of the derived class is called.

Second, the calling sequence of the destructor

When the function ends, the class's destructor is automatically called. The order of invocation of destructors in inheritance is as follows.

The destructor of the derived class is called first, and then the constructor of the base class is called.

We can execute the following code to verify:

#include <iostream>

using namespace std;

class Parent
{
public:
	Parent(int a, int b);
	~Parent();
	void printP();
	int geta ();
	int getb();
private:
	int m_a;
	int m_b;
};

class Child : public Parent
{
public:
	Child( int a, int b,  int c, int d);
	~Child();
	void printC();
private:
	int m_c;
	int m_d;
	int m_aa;
	int m_bb;
};

int Parent :: geta ()
{
	return m_a;
}

int Parent::getb()
{
	return m_b;
}

Parent::Parent(int a, int b)
{
	cout << "Base class constructor" << endl;
	m_a = a;
	m_b = b;
}

Parent::~Parent()
{
	cout << "base class destructor" << endl;
}

void Parent::printP()
{
	cout << "m_a = " << m_a << endl;
	cout << "m_b = " << m_b << endl;
}

Child::Child( int a, int b,  int c, int d):Parent(a, b)
{
	cout << "Constructor of Derived Class" << endl;
	m_aa = geta ();
	m_bb = getb();
	m_c = c;
	m_d = d;
}

Child::~Child()
{
	cout << "Destructor of Derived Class" << endl;
}

void Child::printC()
{
	
	 cout << "m_a = " << m_aa << endl;
	 cout << "m_b = " << m_bb << endl;
	cout << "m_c = " << m_c << endl;
	cout << "m_d = " << m_d << endl;
}
intmain()
{
	Child c(3,4,5,6);
	c.printC();
	return 0;
}

The result of executing this code is as follows.





Guess you like

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