多态案例二-制作饮品

cpp 复制代码
#include<iostream>
using namespace std;
class AbsstractDrinking
{
public:
	//煮水
	virtual void Boil() = 0;
	//冲泡
	virtual void Brew() = 0;
	//倒入杯中
	virtual void PourInCup() = 0;
	//加入佐料
	virtual void Putsomething() = 0;
	void MakeDrinking()
	{
		Boil();
		Brew();
		PourInCup();
		Putsomething();
	}

};
class Coffee:public AbsstractDrinking
{
public:
	//煮水
	virtual void Boil()
	{
		cout << "煮矿泉水" << endl;
	}
	//冲泡
	virtual void Brew()
	{
		cout << "冲泡咖啡" << endl;
	}
	//倒入杯中
	virtual void PourInCup()
	{
		cout << "倒入杯中" << endl;
	}
	//加入佐料
	virtual void Putsomething()
	{
		cout << "加入牛奶和白砂糖" << endl;
	}

};
class Tea :public AbsstractDrinking
{
public:
	//煮水
	virtual void Boil()
	{
		cout << "煮农夫山泉" << endl;
	}
	//冲泡
	virtual void Brew()
	{
		cout << "冲泡茶叶" << endl;
	}
	//倒入杯中
	virtual void PourInCup()
	{
		cout << "倒入茶壶" << endl;
	}
	//加入佐料
	virtual void Putsomething()
	{
		cout << "加入枸杞和枣子" << endl;
	}

};
void doDrinking()
{
	AbsstractDrinking* abs = new Coffee;
	abs->MakeDrinking();
	delete abs;

	cout << "____________________" << endl;

	abs = new Tea;
	abs->MakeDrinking();
	delete abs;
}
void test01()
{
	doDrinking();
}
int main()
{
	test01();
	return 0;
}
相关推荐
郝学胜_神的一滴18 小时前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境3 天前
C++ 的Eigen 库全解析
c++
卷无止境3 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴3 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
博客18005 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴5 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨5 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
clint45610 天前
C++进阶(1)——前景提要
c++
夜悊10 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴10 天前
CMake 021: IF 条件判据详诠
c++·cmake