赞
踩
代码示例如下:
- #include<iostream>
- using namespace std;
-
- class AbstractDrinking
- {
- public:
- virtual void Soil() = 0;//第一步、煮开水
- virtual void Brew() = 0;//第二步、冲泡
- virtual void PourInCup() = 0;//第三步、倒入杯中
- virtual void AddSomething() = 0;//第四步、加入辅料
- };
-
- void makeDrink(AbstractDrinking* abd)
- {
- abd->Soil();
- abd->Brew();
- abd->PourInCup();
- abd->AddSomething();
- }
-
- class Coffee : public AbstractDrinking
- {
- public:
- void Soil()
- {
- cout << "煮咖啡" << endl << "第一步、煮沸百岁山" << endl;
- }
-
- void Brew()
- {
- cout << "第二步、冲泡咖啡" << endl;
- }
- void PourInCup()
- {
- cout << "第三步、倒入杯中" << endl;
- }
- void AddSomething()
- {
- cout << "第四步、加入糖和牛奶" << endl << endl;
- }
- };
-
- class Tea : public AbstractDrinking
- {
- void Soil()
- {
- cout << "泡茶叶" << endl << "第一步、煮沸怡宝" << endl;
- }
-
- void Brew()
- {
- cout << "第二步、冲泡茶叶" << endl;
- }
- void PourInCup()
- {
- cout << "第三步、倒入杯中" << endl;
- }
- void AddSomething()
- {
- cout << "第四步、加入枸杞" << endl;
- }
- };
-
- int main() {
- AbstractDrinking* cof = new Coffee;
- AbstractDrinking* tea = new Tea;
-
- makeDrink(cof);
- makeDrink(tea);
- }

结果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。