案例描述
利用多态技术实现,提供抽象制作饮品基类,提供子类制作咖啡和茶叶
示例
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
class AbstractDrinking
{
public:
//烧水
virtual void Boil() = 0;
//泡
virtual void Brew() = 0;
//倒入
virtual void PourInCup() = 0;
//加入辅料
virtual void PutSometing() = 0;
//制作
void makeDrink()
{
Boil();
Brew();
PourInCup();
PutSometing();
}
private:
};
class Coffee :public AbstractDrinking
{
public:
//烧水
virtual void Boil()
{
cout << "烧纯净水" << endl;
}
//泡
virtual void Brew()
{
cout << "泡咖啡" << endl;
}
//倒入
virtual void PourInCup()
{
cout << "倒入马克杯" << endl;
}
//加入辅料
virtual void PutSometing()
{
cout << "加入奶泡" << endl;
}
private:
};
void doWork(AbstractDrinking *abs)
{
abs->makeDrink();
delete abs;
}
int main()
{
doWork(new Coffee);
system("pause");
return 0;
}
来源:CSDN
作者:才文嘉
链接:https://blog.csdn.net/qq_38872537/article/details/104133621