下面程序的运行结果是: #include <iostream> using namespace std; class BaseClass { public: ~BaseClass(){cout<<"~BaseClass() is called!"<<endl;} }; class DerivedClass:public BaseClass { public: ~DerivedClass(){cout<<"~DerivedClass() is called!"<<endl;} }; int main() { DerivedClass d; return 0; }
时间: 2024-03-24 13:39:19 浏览: 66
使用include <iostream>时一定要加using namespace std
下面是程序的输出结果:
```
~DerivedClass() is called!
~BaseClass() is called!
```
程序中,类DerivedClass是公共继承自BaseClass的派生类。在main函数中,声明了一个DerivedClass对象d。当程序运行结束时,程序会自动调用d的析构函数,并按照继承顺序先调用DerivedClass的析构函数,再调用BaseClass的析构函数。因此,输出结果为~DerivedClass() is called!和~BaseClass() is called!。
阅读全文