c++ - 关于派生类如何工作的问题
问题描述
这是我期末考试的模拟测试。我必须能够解释这些代码是如何工作的。但老实说,我不太了解这段代码。你们能解释一下我这是如何工作的吗?如果我能完全理解它,我会很高兴为我的决赛做好准备。谢谢你。
#include <iostream>
using namespace std;
class Wind {
int category;
public:
Wind(int cat = 3) {
category = cat;
cout << "1." << cat << endl;
}
virtual void increase(int amount) {
category += amount;
cout << "A. " << category << endl;
}
void operator++() {
++category;
cout << "B. " << category << endl;
}
virtual ~Wind() {
cout << "C. " << category << endl;
}
};
class Tornado : public Wind {
double velocity;
public:
Tornado(int cat, double vel) : Wind(cat) {
velocity = vel;
cout << "2. " << vel << endl;
}
virtual void increase(int value) {
velocity += value;
cout << "X. " << velocity << endl;
}
void operator++() {
Wind::operator++();
velocity += 20;
cout << "Y. " << endl;
}
~Tornado() {
cout << "Z. " << velocity << endl;
}
};
int main() {
Wind* wind_array[2];
wind_array[0] = new Tornado(7, 66.5);
wind_array[1] = new Wind(5);
for (int i = 0; i < 2; i++) {
wind_array[i]->increase(5);
++(*wind_array[i]);
}
for (int i = 0; i < 2; i++)
delete wind_array[i];
return 0;
}
这是输出。
1.7
2. 66.5
1.5
X. 71.5
B. 8
A. 10
B. 11
Z. 71.5
C. 8
C. 11
解决方案
推荐阅读
- python-3.x - 如何在 Flask Python 中的 localhost 上显示不同的 webapp
- sql - 如何在两个相同的分隔符 T-SQL 之间提取字符串?
- sql - 如何查找一个记录列值是否存在于另一个表中?
- sql - SQL 返回每个 id、日期和频道的第二个最大日期
- three.js - 在 Forge Viewer 中从对象的 fragId 获取 ThreeJS 网格的函数“getRenderProxy”无法正常工作
- next.js - 在 Nextjs 中构建后如何导入某个图像并获取确切的路径(图像名称)
- node.js - Http 流使用 Keep-alive 并在其创建时验证套接字
- javascript - 选择开始日期时如何设置结束日期?
- kubernetes - 使用 ELK 设置 zipkin 时发生错误
- npm - 私有 npm 注册表不适用于自签名证书