当前位置:网站首页>多态案例-制作饮品
多态案例-制作饮品
2022-07-26 13:46:00 【人间正道是沧桑80】
#if 1
#include<iostream>
using namespace std;
class AbstractDrinking
{
public:
//煮水
virtual void Boil() = 0;
//冲泡
virtual void Brew() = 0;
//倒入杯中
virtual void PoutInCup() = 0;
//加入辅料
virtual void PutSomething() = 0;
//制作饮品
void makeDrink()
{
Boil();
Brew();
PoutInCup();
PutSomething();
}
};
class Coffee :public AbstractDrinking
{
public:
//煮水
virtual void Boil()
{
cout << "煮农夫山泉" << endl;
}
//冲泡
virtual void Brew()
{
cout << "冲泡咖啡" << endl;
}
//倒入杯中
virtual void PoutInCup()
{
cout << "倒入杯中" << endl;
}
//加入辅料
virtual void PutSomething()
{
cout << "加入糖和牛奶" << endl;
}
};
//制作茶叶
class Tea :public AbstractDrinking {
public:
//煮水
virtual void Boil()
{
cout << "煮矿泉水" << endl;
}
//冲泡
virtual void Brew()
{
cout << "冲泡茶叶" << endl;
}
//倒入杯中
virtual void PoutInCup()
{
cout << "倒入杯中" << endl;
}
//加入辅料
virtual void PutSomething()
{
cout << "加入枸杞" << endl;
}
};
//制作函数
void doWork(AbstractDrinking* abs) //AbstractDrinking* abs = new coffee;
{
abs->makeDrink();
delete abs;//释放
}
void test01()
{
//制作咖啡
doWork(new Coffee);
cout << "------------" << endl;
doWork(new Tea);
}
int main()
{
test01();
system("pause");
return 0;
}
#endif
边栏推荐
- 华为机考 ~ 偏移量实现字符串加密
- JSON data transfer parameters & date type parameter transfer
- Pytorch学习笔记(一)安装与常用函数的使用
- Pytoch learning notes (I) installation and use of common functions
- B+ tree index use (6) leftmost principle -- MySQL from entry to proficiency (18)
- This article explains the FS file module and path module in nodejs in detail
- 冒泡排序的时间复杂度分析
- [beauty of open source] nanomsg (2): req/rep mode
- 万字长文,浅谈企业数字化建模蓝图
- Familiarize you with the "phone book" of cloud network: DNS
猜你喜欢

Photoshop (cc2020) unfinished
![[collection of topics that C language learners must know 1] consolidate the foundation and steadily improve](/img/95/bec94176cadfac112585df259156c9.png)
[collection of topics that C language learners must know 1] consolidate the foundation and steadily improve

Why does WPS refuse advertising?

With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
![[oauth2] VII. Wechat oauth2 authorized login](/img/1a/3f2b9fc57759a1fa3fda1451492e5c.png)
[oauth2] VII. Wechat oauth2 authorized login

历时15年、拥有5亿用户的飞信,彻底死了

The shell script in Jenkins fails to execute but does not exit by itself

.net6与英雄联盟邂逅之——根据官方LCU API制作游戏助手

天津市应急局与驻津央企签署协议深化应急联动机制建设

力扣------字符串中的单词数
随机推荐
消息的订阅和发布
Docker swarm cluster builds highly available MySQL active and standby
向路由组件传递参数
JUC总结
估值15亿美元的独角兽被爆裁员,又一赛道遇冷?
Why does WPS refuse advertising?
WPS凭什么拒绝广告?
MySQL's practice of SQL analysis and optimization from the index principle
B+ tree index use (9) grouping, back to table, overlay index (21)
404 pages and routing hooks
从标注好的xml文件中截取坐标点(人脸框四个点坐标)人脸图像并保存在指定文件夹
Synchronization mechanism of go (sync.mutex)
The shell script in Jenkins fails to execute but does not exit by itself
Ultimate doll 2.0 | cloud native delivery package
Pytorch学习笔记(一)安装与常用函数的使用
JS object assignment problem
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
Propagation of transactions
7-25 0-1 backpack (50 points)
The picture moves horizontally with the phone - gyroscope. 360 degree setting conditions