#include <iostream>
using namespace std;
class BaseCalculator {
public:
int m_A;
int m_B;
// write your code here......
virtual int getResult(){//这里的函数必须虚化,不然后面的程序可能会读到这里
return 0;//这里随便取,因为已经虚化
}
};
// 加法计算器类
class AddCalculator : public BaseCalculator {
// write your code here......
public:
int getResult(){
return m_A+m_B;//返回加法
}
};
// 减法计算器类
class SubCalculator : public BaseCalculator {
// write your code here......
public:
int getResult(){
return m_A-m_B;//返回减法
}
};
int main() {
BaseCalculator* cal = new AddCalculator;//从AddCalculator堆里开出内存,储存cal
cal->m_A = 10;
cal->m_B = 20;
cout << cal->getResult() << endl;
delete cal;//删除这个内存
cal = new SubCalculator;
cal->m_A = 20;
cal->m_B = 10;
cout << cal->getResult() << endl;
delete cal;
return 0;
}
实现计算器的功能(牛客网题)
最新推荐文章于 2024-08-28 14:35:43 发布