Destructors for C++ derived classes

The function of the destructor of the derived class is to perform some necessary cleaning work before the object of this class dies. A destructor has no type and no parameters.

In the derivation process, the destructor of the base class cannot be inherited. If destructor is required, a new destructor must be declared in the derived class. The declaration method of the derived class destructor is exactly the same as the declaration method of the destructor without the destructor in the inheritance relationship, as long as the function body is responsible for cleaning up the newly added non-object members of the derived class, it is enough. The system will call the destructor of the base class and object members to clean up the base class and object members. But her execution order is opposite to that of the constructor. First, execute the function body of the destructor, then clean up the member objects of the newly added class type of the derived class, and finally clean up all the members inherited from the base class. These cleaning tasks are to execute derived class destructor, call the destructor of the class where the derived class object member of the class type is located, and call the base class destructor. The order in which these destructors are called is the exact opposite of the order in which the constructors are called.

If the destructor of a certain class is not explicitly declared, in this case, the compilation system will automatically generate a default destructor for each class and call it automatically when the object lifetime ends. The function body of the automatically generated destructor is empty, but it does not do anything. It will implicitly call the destructor of the class where the derived class object member is located and call the destructor of the base class.

[Example] Derived class destructor example

class B1//基类B1,构造函数有参数
{
    
    
public:

	B1(int i)
	{
    
    
		cout << "基类B1的构造函数" << i << endl;
	}
	~B1()
	{
    
    
		cout << "基类B1的析构函数" << endl;
	}

};

class B2//基类B2,构造函数有参数
{
    
    
public:
	B2(int j)
	{
    
    
		cout << "基类B2的构造函数" << j << endl;
	}
	~B2()
	{
    
    
		cout << "基类B2的析构函数" << endl;
	}

};

class B3//基类B3,构造函数无参数
{
    
    
public:
	B3()
	{
    
    
		cout << "基类B3的构造函数" << endl;
	}
	~B3()
	{
    
    
		cout << "基类B3的析构函数" << endl;
	}
};

class D :public B2, public B1, public B3//派生类D,注意基类名的顺序
{
    
    
public://派生类的公有成员
	D(int a, int b, int c, int d) : b2(d), B2(b), B1(a), b1(c) {
    
    }//注意类名的个数与顺序,注意成员对象名的个数与顺序
private://派生类的私有成员对象
	B1 b1;
	B2 b2;
	B3 b3;
};

int main()
{
    
    
	D d(1, 2, 3, 4);
	return 0;
}

operation result:
insert image description here

analyze:

In the program, destructors are added to the three base classes, and the derived classes do not make any changes, and the default destructors provided by the system are still used. When the program is executed, the constructor of the derived class is first executed, and then the destructor of the derived class is executed. The default destructor of the derived class calls the destructor of the member object and the base class respectively, and the order is just opposite to that of the constructor.

[Example 2]

class B
{
    
    
public:
	B(int x = 0) :x(x)
	{
    
    
		cout << "基类B构造函数" << endl;
	}
	B(B& b)
	{
    
    
		cout << "基类B的拷贝构造函数" << endl;
		x = b.x;
	}
	~B()
	{
    
    
		cout << "基类B的析构函数" << endl;
	}
	void show1()
	{
    
    
		cout << "x的值为:" << x << endl;
	}
	int getX()
	{
    
    
		return x;
	}
private:
	int x;
};

class D :public B
{
    
    
public:
	D(int a, int b, int c, int d) :B(a), y(b), z(c), v(d)
	{
    
    
		cout << "派生类D的构造函数" << endl;
	}
	D(D& dd) :B(dd)
	{
    
    
		cout << "派生类D的拷贝构造函数" << endl;
		x = dd.x;
		y = dd.y;
		z = dd.z;
		v = dd.v;
	}

	~D()
	{
    
    
		cout << "基类D的析构函数" << endl;
	}

	void show2()
	{
    
    
		cout << "x的值为:" << getX() << endl;
		cout << "y的值为:" << y << endl;
		cout << "z的值为:" << z << endl;
		cout << "v的值为:" << v << endl;
	}
private:
	int x;
	int y;
	int z;
	int v;
};

int main()
{
    
    
	B b(5);
	b.show1();

	D d(1, 2, 3, 4);
	d.show2();

	D dd(d);
	dd.show2();
	return 0;
}

operation result:
insert image description here

Guess you like

Origin blog.csdn.net/NuYoaH502329/article/details/132133916