制作饮品------运用纯虚函数知识
#include<iostream>
#include<string>
using namespace std;
class AbstractDrink {
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 AbstractDrink {
public:
virtual void Boil() {
cout << "加入农夫山泉" << endl;
}
virtual void Brew() {
cout << "倒入煮好的农夫山泉" << endl;
}
virtual void PourInCup() {
cout << "倒入杯子中" << endl;
}
virtual void PutSomething() {
cout << "加入牛奶和糖" << endl;
}
};
class Tea :public AbstractDrink {
public:
virtual void Boil() {
cout << "加入自来水" << endl;
}
virtual void Brew() {
cout << "倒入煮好的自来水" << endl;
}
virtual void PourInCup() {
cout << "倒入茶具中" << endl;
}
virtual void PutSomething() {
cout << "加入枸杞和糖" << endl;
}
};
void Work(AbstractDrink* abc) {
abc->makeDrink();
}
int main()
{
Work(new Coffee);
cout << "-----------------------------" << endl;
Work(new (Tea));
return 0;
}
标签:cout,饮品,void,多态,virtual,---,public,Boil,makeDrink
From: https://blog.csdn.net/2301_79969370/article/details/142286074