当前位置:网站首页>利用多态实现简单的计算器
利用多态实现简单的计算器
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;
}
边栏推荐
- 3d立体相册,情人节,情侣生日礼物代码适用
- Navicat连接MySQL8.0的正确方法(亲测有效)
- Unity SplashImage 缩放问题
- Uncover the practice of Baidu intelligent test in the field of automatic test execution
- Redis的缓存雪崩、缓存击穿、缓存穿透与缓存预热、缓存降级
- 如何优雅的写 Controller 层代码?
- [use of veux developer tools - use of getters]
- MySQL数据库:分区Partition
- Redis' data expiration clearing strategy and memory obsolescence strategy
- matplotlib直方图,柱状图
猜你喜欢

go-zero微服务实战系列(七、请求量这么高该如何优化)

mysql多表查询

I talked about exception handling for more than half an hour during the interview yesterday

台式机主板上保护cpu的盖子安装和拆卸

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

《canvas》之第12章 其他应用

Sixty years of deep learning

BYD has three years left

Applet Wechat: un nouveau réseau exclusif de microgroupes de développement de Cloud

吐血整理:一份不可多得的架构师图谱!
随机推荐
ANSVC无功补偿装置在河北某购物广场中的应用
Uncover the practice of Baidu intelligent test in the field of automatic test execution
超 Nice 的表格响应式布局小技巧
Wechat applet: Yunkai publishes white wall wechat applet source code download server free and domain name support traffic main revenue
Tiktok's global short video dominance may be reversed by YouTube
文物数字藏品,开启文化传承的新方式
【置顶】博客使用须知,公告板,留言板,关于博主
“死掉”的诺基亚,一年躺赚1500亿
Installation and removal of cover for CPU protection on desktop motherboard
Recruiting talents and seeking development | Jincang of the National People's Congress won the "best employer school recruitment case Award"
微信小程序:全新獨家雲開發微群人脈
《canvas》之第5章 文本操作
stm32 mbed 入门教程(四)---PWM
tcpdump如何对特定的tcp标志位进行过滤
微信小程序:云开发表白墙微信小程序源码下载免服务器和域名支持流量主收益
数字IC手撕代码--交通灯
Underlying implementation principle of five data structures of redis
深度学习的坎坷六十年
Redis的数据过期清除策略 与 内存淘汰策略
Redis的持久化机制