搜索

适配器模式c++


发布时间: 2022-11-24 21:33:03    浏览次数:81 次

[实验任务一]:双向适配器

实现一个双向适配器,使得猫可以学狗叫,狗可以学猫抓老鼠。

 

 

#include<iostream>
using namespace std;
class Cat{
public:
    virtual void miao()=0;
    virtual void catchs()=0;
};
class Dog{
public:
    virtual void wang()=0;
    virtual void run()=0;
};
class RealCat:public Cat{
public:
    void miao(){
        cout<<"喵喵叫!"<<endl;
    }
    void catchs(){
        cout<<"抓老鼠!"<<endl;
    }
};
class RealDog:public Dog{
public:
    void wang(){
        cout<<"汪汪叫!"<<endl;
    }
    void run(){
        cout<<"跑跑跑!"<<endl;
    }
};
class Adapter:public Cat,public Dog{
private:
    static Cat *cat;
    static Dog *dog;
public:
    void setCat(Cat *c){
        cat=c;
    }
    void setDog(Dog *d){
        dog=d;
    }
    void wang(){
    }
    void catchs(){
    }
    void run(){
        cout<<"小狗学小猫!"<<endl;
        cat->catchs();
    }
    void miao(){
        cout<<"小猫学小狗:"<<endl;
        dog->wang();
    }
};
Cat* Adapter::cat=new RealCat();
Dog* Adapter::dog=new RealDog();
int main(){
    Adapter adapter;
    adapter.run();
    adapter.miao();
    return 0;
}

 

免责声明 适配器模式c++,资源类别:文本, 浏览次数:81 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 09:33:03。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/rongzhang/p/16923528.html