当前位置:网站首页>Using polymorphism to realize simple calculator
Using polymorphism to realize simple calculator
2022-06-29 14:37:00 【codexixi】
Using polymorphism to realize simple calculator
#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;
}
边栏推荐
- 揭秘!付费会员制下的那些小心机!
- 《canvas》之第8章 像素操作
- 【烹饪记录】--- 酸辣白菜
- 【关联分析实战篇】为什么 BI 软件都搞不定关联分析
- Wechat applet: Halloween avatar box generation tool
- 【shell】jenkins shell实现自动部署
- [Jenkins] pipeline controls the sequential execution of multiple jobs for timed continuous integration
- 分布式唯一 ID 生成方案浅谈
- Wechat applet: Yunkai publishes white wall wechat applet source code download server free and domain name support traffic main revenue
- 【黑马早报】中公教育市值蒸发逾2000亿;新东方直播粉丝破2000万;HM关闭中国首店;万科郁亮称房地产已触底;微信上线“大爆炸”功能...
猜你喜欢

微信小程序:万圣节头像框生成工具

BYD has three years left

吐血整理:一份不可多得的架构师图谱!

Stable currency risk profile: are usdt and usdc safe?

校园转转二手市场源码

uniApp问题清单与经验

Transport layer user datagram protocol (UDP)

TikTok全球短视频霸主地位或被YouTube反超
![[important notice] the 2022 series of awards and recommendations of China graphics society were launched](/img/ae/2fe0cf9964e5fd3b18e5f295638d8b.png)
[important notice] the 2022 series of awards and recommendations of China graphics society were launched

Analysis of istio -- observability
随机推荐
台式机主板上保护cpu的盖子安装和拆卸
BYD has three years left
Detailed explanation of redis sentry mechanism
留给比亚迪的时间还有三年
在同花顺上开户安全吗 开户在哪里申请
Stm32 mbed tutorial (IV) --pwm
Wechat applet: Halloween avatar box generation tool
灵感收集·创意写作软件评测:Flomo、Obsidian Memo、Napkin、FlowUs
Laravel - Composer 安装指定 Laravel 版本
delphi7中 url的编码
Applet Wechat: un nouveau réseau exclusif de microgroupes de développement de Cloud
Redis的五种数据结构的底层实现原理
电脑时间校对后不自动更新要如何解决
Methods of accessing external services in istio grid
Persistence mechanism of redis
微信小程序:全新独家云开发微群人脉
[important notice] the 2022 series of awards and recommendations of China graphics society were launched
Transport layer user datagram protocol (UDP)
28000 word summary of callable and future interview knowledge points. After reading it, I went directly to ByteDance. Forgive me for being a little floating (Part 2)
3d立体相册,情人节,情侣生日礼物代码适用