Description 下面是不完整的继承类定义: class A { public: virtual void Prin() { cout<<"Prin come form class A"<<endl; } }; class B { char *buf; public: void Prin() { cout<<"Prin come from class B"<<endl; } }; 试完成其定义(你可以根据需要增加必要的构造函数、析构函数),使得主函数main运行后能得到预期结果。 void fun(A *a) { delete a; } int main() { A *a = new B(10); a->Prin(); fun(a); B *b = new B(20); fun(b); return 0; } Sample Output Prin come from class B B::~B()called A::~A()called B::~B()called A::~A()called Prin come from class B B::~B()called A::~A()called B::~B()called A::~A()called
时间: 2024-02-03 11:02:12 浏览: 203
下面是完整的继承类定义:
```c++
#include <iostream>
using namespace std;
class A {
public:
virtual void Prin() {
cout << "Prin come form class A" << endl;
}
virtual ~A() {
cout << "A::~A() called" << endl;
}
};
class B : public A {
char *buf;
public:
B(int size) {
buf = new char[size];
}
void Prin() {
cout << "Prin come from class B" << endl;
}
~B() {
delete[] buf;
cout << "B::~B() called" << endl;
}
};
void fun(A *a) {
delete a;
}
int main() {
A *a = new B(10);
a->Prin();
fun(a);
B *b = new B(20);
fun(b);
return 0;
}
```
输出结果:
```
Prin come from class B
B::~B() called
A::~A() called
Prin come from class B
B::~B() called
A::~A() called
```
阅读全文