当前位置:网站首页>利用多态实现简单的计算器
利用多态实现简单的计算器
2022-06-29 13:56:00 【codexixi】
利用多态实现简单的计算器
#include <iostream>
using namespace std;
class AbstractCalculator
{
public:
virtual int getChar()
{
return 0;
}
int m_A = 0;
int m_B = 0;
};
class AddCalculator : public AbstractCalculator
{
public:
int getChar()
{
return m_A + m_B;
}
};
class SubCalculator : public AbstractCalculator
{
public:
int getChar()
{
return m_A - m_B;
}
};
class MulCalculator : public AbstractCalculator
{
public:
int getChar()
{
return m_A * m_B;
}
};
class DivCalculator : public AbstractCalculator
{
public:
int getChar()
{
return m_A / m_B;
}
};
void test01()
{
AbstractCalculator* abc = new AddCalculator;
abc->m_A = 10;
abc->m_B = 20;
cout << abc->m_A << "+" << abc->m_B << "=" << abc->getChar() << endl;
delete abc;
abc = new SubCalculator;
abc->m_A = 10;
abc->m_B = 20;
cout << abc->m_A << "-" << abc->m_B << "=" << abc->getChar() << endl;
delete abc;
abc = new MulCalculator;
abc->m_A = 10;
abc->m_B = 20;
cout << abc->m_A << "*" << abc->m_B << "=" << abc->getChar() << endl;
delete abc;
abc = new DivCalculator;
abc->m_A = 10;
abc->m_B = 20;
cout << abc->m_A << "/" << abc->m_B << "=" << abc->getChar() << endl;
delete abc;
}
int main()
{
test01();
return 0;
}
边栏推荐
猜你喜欢

Stable currency risk profile: are usdt and usdc safe?

VQA不只需要图片,还需要外部知识!华盛顿大学&微软提出提出REVIVE,用GPT-3和Wikidata来辅助回答问题!...
一次mysql的.ibd文件过大处理过程记录

Goby full port scan
![[blackduck] configure the specified Synopsys detect scan version under Jenkins](/img/85/73988e6465e8c25d6ab8547040a8fb.png)
[blackduck] configure the specified Synopsys detect scan version under Jenkins

leetcode:226. Flip binary tree

“死掉”的诺基亚,一年躺赚1500亿

微信小程序:装B神器P图修改微信流量主小程序源码下载趣味恶搞图制作免服务器域名

Thanos Store 组件

微信小程序:(更新)云开发微群人脉
随机推荐
[shell] Jenkins shell realizes automatic deployment
电脑时间校对后不自动更新要如何解决
留给比亚迪的时间还有三年
东莞虎门券商公司股票开户哪个更好更安全?
Thinkpad VMware 安装虚拟机出现此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态(问题解决方法)
ANSVC无功补偿装置在河北某购物广场中的应用
leetcode:226. 翻转二叉树
吐血整理:一份不可多得的架构师图谱!
zabbix 5.0如何将esxi6.7添加到监控
Tiktok's global short video dominance may be reversed by YouTube
Shell——文本处理命令
纳人才,谋发展 | 人大金仓喜获“最佳雇主校招案例奖”
TikTok全球短视频霸主地位或被YouTube反超
Application of ansvc reactive power compensation device in a shopping mall in Hebei
在同花顺上开户安全吗 开户在哪里申请
分布式唯一 ID 生成方案浅谈
[high concurrency] 28000 words' summary of callable and future interview knowledge points. After reading it, I went directly to ByteDance. Forgive me for being a little drifting (middle)
传输层 选择性确认 SACK
Can Ruida futures open an account? Is it safe and reliable?
MySQL数据库:drop、truncate、delete的区别