下面是不完整的继承类定义: 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; } }; 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 Hint Virtual destructor!(在析构函数前加一个virtual) Only need to submit class A and B. Please check your spelling carefully.
时间: 2024-01-09 08:05:44 浏览: 120
完整的继承类定义如下:
```
class A {
public:
virtual void Prin() {
cout << "Prin come form class A" << endl;
}
virtual ~A() {}
};
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;
}
};
```
在类A中增加了一个虚析构函数,并且将类B继承自类A,同时在类B的构造函数中增加了一个动态内存分配的操作,因为需要在析构函数中释放这个动态内存。在类B的析构函数中增加了一个输出语句,用来验证析构函数的调用。这样,主函数中的代码就能够正确运行并输出预期结果了。
相关问题
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
下面是完整的继承类定义:
```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
```
abstract class MyPoint { public abstract void show(); } //打印正方形 class MyPrintSquare extend MyPoint { @Override public void show() { for(int i=0;i<=5;++i){ for(int j=1;j<=5;++j){ if(j==1 && j==4) Systemout.print('*'); if(i==0 && i==4) System.out.print('*'); else Sytem.out.print(' '); } } } } //打印圆形 class MyPrintCircle extends MyPoint{ @Override public void show() { for (int y = 0; y <2 * 5; y += 2) { int x = (int)Math.round(5 - Math.sqrt(2 * 5 * y - y * y)); int len = 2 * (5 - x); for (int i = 0; i <= x; i++) { System.out.print(' '); } System.out.print('*'); for (int j = 0; j < len; j++) { System.out.prin(' '); System.out.println('*'); } } } } public class MyPointTest { public static void Show(MyPoint a){ a.Show(); } public static void main(String[] args){ MyPoint mp1 = new MyPrintSquare(); MyPoint mp2 = new MyPrintCircle(); myShow(mp1); myShow(mp2); } }
这段代码存在几个错误,可能是因为笔误或者拼写错误。下面是我发现的错误和建议的修改:
1. 在类 MyPrintSquare 的 extends 后应该是 MyPoint,而不是 extend MyPoint。
2. 在 MyPrintSquare 的 show 方法中,System.out.print('*') 和 System.out.prin(' ') 中间的字母 t 应该是少打了一个。
3. 在 MyPrintCircle 的 show 方法中,System.out.prin(' ') 应该是 System.out.print(' ')。
4. 在 MyPointTest 的 main 方法中,myShow 应该是 Show。
下面是修改后的代码:
```
abstract class MyPoint {
public abstract void show();
}
//打印正方形
class MyPrintSquare extends MyPoint {
@Override
public void show() {
for(int i=0;i<=5;++i){
for(int j=1;j<=5;++j){
if(j==1 || j==4)
System.out.print('*');
if(i==0 || i==4)
System.out.print('*');
else System.out.print(' ');
}
System.out.println();
}
}
}
//打印圆形
class MyPrintCircle extends MyPoint{
@Override
public void show() {
for (int y = 0; y <2 * 5; y += 2) {
int x = (int)Math.round(5 - Math.sqrt(2 * 5 * y - y * y));
int len = 2 * (5 - x);
for (int i = 0; i <= x; i++) {
System.out.print(' ');
}
System.out.print('*');
for (int j = 0; j < len; j++) {
System.out.print(' ');
System.out.print('*');
}
System.out.println();
}
}
}
public class MyPointTest {
public static void Show(MyPoint a){
a.show();
}
public static void main(String[] args){
MyPoint mp1 = new MyPrintSquare();
MyPoint mp2 = new MyPrintCircle();
Show(mp1);
Show(mp2);
}
}
```
阅读全文