#include<iostream>
#include<string>
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
{
public:
//煮水
virtual void Boil()
{
cout << "煮农夫山泉" << endl;
}
//冲泡
virtual void Brew()
{
cout << "冲泡咖啡" << endl;
}
//倒入杯中
virtual void PourInCup()
{
cout << "倒入杯中" << endl;
}
//加入辅助的佐料
virtual void Putsomething()
{
cout << "加入牛奶和糖" << endl;
}
};
//制作茶叶
class Tea : public AbstractDrinking
{
public:
//煮水
virtual void Boil()
{
cout << "煮矿泉水" << endl;
}
//冲泡
virtual void Brew()
{
cout << "冲泡茶叶" << endl;
}
//倒入杯中
virtual void PourInCup()
{
cout << "倒入杯中" << endl;
}
//加入辅助的佐料
virtual void Putsomething()
{
cout << "加入枸杞" << endl;
}
};
//制作函数
void doWork(AbstractDrinking * abs) // AbstractDrinking * abs=new Coffee
{ //父类指针指向子类对象
abs->makeDrink(); //同一个接口
delete abs;//释放
}
void test01()
{
//制作咖啡
doWork(new Coffee);
cout << "-----------------" << endl;
//制作咖啡
doWork(new Tea);
}
int main()
{
test01();
return 0;
}
注:C++黑马视频观看