加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c++多态案例之制作饮品

发布时间:2020-12-16 09:06:04 所属栏目:百科 来源:网络整理
导读:制作饮品的过程大致为:煮水、冲泡、倒入杯中、加入调料。 #includeiostream using namespace std; class AbstractDrink { public : virtual void Boil() = 0 ; void Brew() = void PourInCup() = void PutSomething() = void makeDrink() { Boil(); Brew();

制作饮品的过程大致为:煮水、冲泡、倒入杯中、加入调料。

#include<iostream>
using namespace std;

class AbstractDrink {
public:
    virtual void Boil() = 0;
    void Brew() = void PourInCup() = void PutSomething() = void makeDrink() {
        Boil();
        Brew();
        PourInCup();
        PutSomething();
    }
};
class Milk: AbstractDrink {
     Boil() {
        cout << "正在煮水" << endl;
    }
     Brew() {
        cout << 冲泡牛奶 PourInCup() {
        cout << 倒入杯中 PutSomething() {
        cout << 加入麦片 endl;
    }
};
class Coffe :冲泡咖啡加入牛奶void doWork(AbstractDrink * abs) {
    abs->makeDrink();
    delete abs;
}
 test() {
    doWork(new Milk);
    cout << --------------- endl;
    doWork( Coffe);
}

int main() {
    test();
    system(pause");
    return ;
}

输出:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读