20154:补足程序4
总时间限制:
1000ms
内存限制:
65536kB
// 在此处补充你的代码
描述
补足下面的程序,使之输出结果是:
destructor B
destructor A
#include <iostream> #include <algorithm> #include <string> using namespace std; class A { public:
}; class B:public A { public: ~B() { cout << "destructor B" << endl; } } ; int main() { A * p = new B(); delete p; return 0; }
输入
无
输出
destructor B
destructor A
样例输入
无
样例输出
destructor B destructor A
这里就是典序的一个多态
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
class A {
public:
// 在此处补充你的代码
virtual ~A() { cout << "destructor A" << endl; };
};
class B :public A
{
public:
~B() { cout << "destructor B" << endl; }
};
int main()
{
A * p = new B();
delete p;
system("pause");
return 0;
}