多态案例二-制作饮品

2023-12-28 16:52:38

#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;
}

文章来源:https://blog.csdn.net/xms12_3/article/details/135272522
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。