#include <iostream>
//在继承体系下:
//创建对象时:先调父类构造,再调子类构造
//销毁对象时:先调子类析构,再调父类析构
class A
{
int* p;
public:
A()
{
p = new int[10];
std::cout<<"父类构造\n";
}
~A()
{
delete [] p;
std::cout<<"父类析构\n";
}
};
class B : public A
{
int* b;
public:
B()
{
b = new int[20];
std::cout<<"子类构造\n";
}
~B()
{
delete [] b;
std::cout<<"子类析构\n";
}
};
class C : public B
{
int b;
public:
C()
{
std::cout<<"C构造\n";
}
~C()
{
std::cout<<"C析构\n";
}
};
void main()
{
{
A a;
//父类构造
}
//父类析构
std::cout<<std::endl;
{
B b;
//父类构造
//子类构造
}
//子类析构
//父类析构
std::cout<<std::endl;
{
C c;
//父类构造
//子类构造
//C构造
}
//C析构
//子类析构
//父类析构
system("pause");
}