实验8:适配器模式---c++

 

 C++代码:

#include<iostream>
using namespace std;
class Cat{
public:
    virtual void cry()=0;
    virtual void zhua()=0;
};
class Dog{
public:
    virtual void wang()=0;
    virtual void run()=0;
};
class RealCat:public Cat{
public:
    void cry(){
        cout<<"喵喵叫!"<<endl;
    }
    void zhua(){
        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 zhua(){
    }
    void run(){
        cout<<"小狗学小猫!"<<endl;
        cat->zhua();
    }
    void cry(){
        cout<<"小猫学小狗:"<<endl;
        dog->wang();
    }
};
Cat* Adapter::cat=new RealCat();
Dog* Adapter::dog=new RealDog();
int main(){
    Adapter adapter;
    adapter.run();
    adapter.cry();
    return 0;
}

 

posted @ 2022-10-09 16:59  好(justice)……  阅读(19)  评论(0编辑  收藏  举报