派生類的析構函數的功能是在該對象消亡之前進行一些必要的清理工作,析構函數沒有類型,也沒有參數。析構函數的執行順序與構造函數相反。
代碼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");
}