#include <iostream> using namespace std; class A {public: A() {cout<<"A::A() called.In";} virtual ~A({cout<<"A::~A() called.In";} }; class B: public A {public: B(int i) { cout<<"B::B( called.In"; buf=new charlil;} virtual ~B0 § delete [buf; cout<<"B:~B()called.In";} private: char *buf;}; int main { A *a=new B(15); delete a; return 0;」的运行结果是什么
时间: 2024-01-30 11:02:58 浏览: 91
此代码存在一些语法错误。修正后的代码如下:
```
#include <iostream>
using namespace std;
class A {
public:
A() {cout<<"A::A() called.";}
virtual ~A() {cout<<"A::~A() called.";}
};
class B: public A {
public:
B(int i) {
cout<<"B::B() called.";
buf=new char[i];
}
virtual ~B() {
delete [] buf;
cout<<"B::~B() called.";
}
private:
char *buf;
};
int main() {
A *a = new B(15);
delete a;
return 0;
}
```
该程序的运行结果是:
```
A::A() called.
B::B() called.
A::~A() called.
B::~B() called.
```
程序首先创建了一个指向 `B` 类对象的 `A` 类指针 `a`,然后通过 `delete` 关键字删除了该指针,这将自动调用 `B` 类的析构函数和 `A` 类的虚析构函数。因此,程序输出了 `A::A() called.` 和 `B::B() called.`,然后是 `A::~A() called.` 和 `B::~B() called.`。输出结果表明程序成功地创建和销毁了 `B` 类对象,并正确地使用了虚析构函数。
阅读全文