多态案例二-制作饮品

多态案例二-制作饮品_第1张图片

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

你可能感兴趣的:(c++,开发语言)