派生类的析构函数的功能是在该对象消亡之前进行一些必要的清理工作,析构函数没有类型,也没有参数。析构函数的执行顺序与构造函数相反。
代码1:
#include
using namespace std;
class A
{
public:
A(int i)
{
cout << "构建 A " << i << endl;
}
A()
{
cout << "构建 A "<< endl;
}
~A()
{
cout << "析构 A" << endl;
}
};
class B
{
public:
B(int i)
{
cout << "构建 B " << i << endl;
}
B()
{
cout << "构建 B " << endl;
}
~B()
{
cout << "析构 B" << endl;
}
};
class C
{
public:
C(int i)
{
cout << "构建 C " << i << endl;
}
C()
{
cout << "构建 C " << endl;
}
~C()
{
cout << "析构 C" << endl;
}
};
class D
{
public:
D()
{
cout << "构建 D "<< endl;
}
~D()
{
cout << "析构 D" << endl;
}
};
class E:public D,public C,public A,public B
{
private:
A n1;
B n2;
C n3;
D n4;
public:
E(int a, int b, int c, int d) :A(a), B(c), C(b)
{
cout << "构建 E " << d << endl;
}
~E()
{
cout << "析构 E" << endl;
}
};
void example()
{
E a(1, 2, 3, 4);
}
int main()
{
example();
system("pause");
}