编程要求 编写程序声明一个基类 BaseClass,构造其派生类DerivedClass,在构造函数和析构函数中用 cout 输出提示信息,观察构造函数和析构函数的执行情况。根据提示,在右侧编辑器补充代码,并输出正确的值。 测试说明 平台会对你编写的代码进行测试: 测试输入: 预期输出: Construction. Number = 1 Destruction. Number = 0
时间: 2024-03-25 22:37:07 浏览: 73
虚函数被类的构造析构函数和成员函数调用虚函数的执行过程
下面是代码实现:
```c++
#include <iostream>
using namespace std;
class BaseClass{
public:
static int number;
BaseClass(){
number++;
cout << "Construction. Number = " << number << endl;
}
~BaseClass(){
number--;
cout << "Destruction. Number = " << number << endl;
}
};
int BaseClass::number = 0;
class DerivedClass : public BaseClass{
public:
DerivedClass(){
cout << "DerivedClass Construction." << endl;
}
~DerivedClass(){
cout << "DerivedClass Destruction." << endl;
}
};
int main(){
DerivedClass derivedClass;
return 0;
}
```
输出结果为:
```
Construction. Number = 1
DerivedClass Construction.
DerivedClass Destruction.
Destruction. Number = 0
```
阅读全文