C++设计模式之桥接模式

来源:互联网 发布:杜蕾斯大胆爱厚度 知乎 编辑:程序博客网 时间:2024/05/22 14:36
#include <iostream>#include <string>#include <stdlib.h>using namespace std;class OS{private:    string name;public:    OS(string n) :name(n){};    void set_name(string n)    {        this->name = n;    }    string get_name()    {        return this->name;    }};class XP :public OS{public:    XP(string n = "XP") :OS(n){};};class Win7 :public OS{public:    Win7(string n = "Win7") :OS(n){};};class Linux :public OS{public:    Linux(string n = "Linux") :OS(n){};};class Computer{private:    string brand;    OS os;public:    Computer(OS xt, string b) :brand(b), os(xt){};    void set_os(OS os)    {        this->os = os;    }    OS get_os()    {        return this->os;    }    void init()    {        cout << brand << "电脑正在运行" << os.get_name() << endl;    }};class Dell:public Computer{public:    Dell(OS os, string b = "Dell") :Computer(os, b){};};class Lenovo :public Computer{public:    Lenovo(OS os, string b = "Lenovo") :Computer(os, b){};};int main(){    XP xp;    Win7 win7;    Linux linux;    Dell dell(xp);    Lenovo lenovo(win7);    dell.init();    lenovo.init();    dell.set_os(linux);    dell.init();    system("pause");}


原创粉丝点击