CPP46. 多态实现计算器功能
描述
完善下面的代码,使程序能够正常运行。要求 BaseCalculator 类中提供 getResult() 函数(无需实现),在 AddCalculator 类中实现两个成员相加(m_A + m_B),在 SubCalculator 类中实现两个成员相减(m_A - m_B)输入描述
无输出描述
程序已经给定输出C++ 解法, 执行用时: 2ms, 内存消耗: 272KB, 提交时间: 2022-04-14
#include <iostream> using namespace std; class BaseCalculator { public: int m_A; int m_B; // write your code here...... public: virtual int getResult(); }; // 加法计算器类 class AddCalculator : public BaseCalculator { // write your code here...... virtual int getResult() { return m_A+m_B; } }; // 减法计算器类 class SubCalculator : public BaseCalculator { // write your code here...... virtual int getResult() { return m_A-m_B; } }; int main() { BaseCalculator* cal = new AddCalculator; 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; }
C++ 解法, 执行用时: 2ms, 内存消耗: 272KB, 提交时间: 2021-11-07
#include <iostream> using namespace std; class BaseCalculator { public: int m_A; int m_B; // write your code here...... virtual int getResult(); }; // 加法计算器类 class AddCalculator : public BaseCalculator { // write your code here...... virtual int getResult() { return m_A + m_B; } }; // 减法计算器类 class SubCalculator : public BaseCalculator { // write your code here...... virtual int getResult() { return m_A - m_B; } }; int main() { BaseCalculator* cal = new AddCalculator; 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; }
C++ 解法, 执行用时: 2ms, 内存消耗: 276KB, 提交时间: 2022-08-07
#include <iostream> using namespace std; class BaseCalculator { public: int m_A; int m_B; // write your code here...... virtual int getResult() = 0; }; // 加法计算器类 class AddCalculator : public BaseCalculator { // write your code here...... int getResult() { return this->m_A + this->m_B; } }; // 减法计算器类 class SubCalculator : public BaseCalculator { // write your code here...... int getResult() { return this->m_A - this->m_B; } }; int main() { BaseCalculator* cal = new AddCalculator; 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; }
C++ 解法, 执行用时: 2ms, 内存消耗: 276KB, 提交时间: 2022-05-17
#include <iostream> using namespace std; class BaseCalculator { public: int m_A; int m_B; // write your code here...... //多态实现计算器功能 virtual int getResult(); }; // 加法计算器类 class AddCalculator : public BaseCalculator { // write your code here...... //重载getResult方法,实现功能 virtual int getResult() { return m_A+m_B; } }; // 减法计算器类 class SubCalculator : public BaseCalculator { // write your code here...... virtual int getResult() { return m_A-m_B; } }; int main() { BaseCalculator* cal = new AddCalculator; 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; }
C++ 解法, 执行用时: 2ms, 内存消耗: 276KB, 提交时间: 2022-04-02
#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; 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; }