c++多态例子:制作饮料

c++多态例子:制作饮料,第1张

c++多态例子:制作饮料
#include
#include
using namespace std;
class abstractdrinking {
public:
	//煮水
	virtual void boil() = 0;
	//冲茶
	virtual void brew() = 0;
	//加入杯中
	virtual void pourincup() = 0;
	//加入辅料
	virtual void putsomething() = 0;
	void makedrink() {
		boil();
		brew();
		pourincup();
		putsomething();
	}
};
//煮咖啡
class coffee :public abstractdrinking {
	//煮水
	virtual void boil() {
		cout << "放入农夫山泉" << endl;
	}
	//冲茶
	virtual void brew() {

		cout << "冲泡咖啡" << endl;
	}
	//加入杯中
	virtual void pourincup() {
		cout << "倒入杯中" << endl;
	}
	//加入辅料
	virtual void putsomething() {
		cout << "加入糖和牛奶" << endl;
	}
};
//煮茶叶
class tea :public abstractdrinking {
	//煮水
	virtual void boil() {
		cout << "放入农夫山泉" << endl;
	}
	//冲茶
	virtual void brew() {

		cout << "冲泡茶叶" << endl;
	}
	//加入杯中
	virtual void pourincup() {
		cout << "倒入杯中" << endl;
	}
	//加入辅料
	virtual void putsomething() {
		cout << "加入枸杞" << endl;
	}
};
void zhizuo(abstractdrinking*a) {
	a->makedrink();
	delete a;//释放
}
void test1() {
	zhizuo(new coffee);
	cout << "-------------" << endl;
	zhizuo(new tea);
}
int main() {
	test1();
	system("pause");
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4752134.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-08
下一篇 2022-11-08

发表评论

登录后才能评论

评论列表(0条)

保存