#include <iostream>
#include <string.h>
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();
}
};
//制作coffee
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;
}
};
//制作Tea
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* a)
{
a->makeDrink();
delete a;
};
void test()
{
doWork(new Coffee);
cout << "--------------" << endl;
doWork(new Tea);
}
void main()
{
test();
}