#include<iostream>
using namespace std;
class A
{
public:
A()
{
p = new char[20];
strcpy(p,"obja");
printf("A()--构造\n");
}
virtual ~A()
{
delete[] p;
printf("A()--析构\n");
}
protected:
private:
char *p;
};
class B : public A
{
public:
B()
{
p = new char[20];
strcpy(p,"obja");
printf("B()--构造\n");
}
~B()
{
delete[] p;
printf("B()--析构\n");
}
protected:
private:
char *p;
};
class C : public B
{
public:
C()
{
p = new char[20];
strcpy(p,"obja");
printf("C()--构造\n");
}
~C()
{
delete[] p;
printf("C()--析构\n");
}
protected:
private:
char *p;
};
//只执行了父类的析构函数
//通过父类指针 把 所有的子类对象的析构函数 都执行一遍
//想通过父类指针析构所有资源要加virtual
void howtodelete(A *base)
{
delete base;//这句话不会表现成多态这种属性
}
int main()
{
C *myC = new C;
howtodelete(myC);
system("pause");
return 0;
}