连动反应

猫叫 老鼠叫 主人醒

#include <iostream>
#include <string>
#include <stdlib.h>
#include <vector>

using namespace std;

class Animal
{
    public:
    virtual string Getname()=0;
    virtual void Respond(Animal * animal,int value)=0;
};
class Mouse:public Animal
{
    public:
    string Name;
    Mouse(string name)
    {
        Name=name;
    }
    string Getname()
    {
        return Name;
    }
    void Respond(Animal *animal,int value)
    {
        if(value>2000)
        {
            cout<<"Mouse("<<Name<<"):"<<animal->Getname()<<" is coming!"<<endl;
        }
    }
};
class Human:public Animal
{
    public:
    string Name;
    Human(string name)
    {
        Name=name;
    }
    string Getname()
    {
        return Name;
    }
    void Respond(Animal*animal,int value)
    {
        if(value>100)
        {
            cout<<"Human("<<Name<<"):"<<animal->Getname()<<" is anoyying!"<<endl;
        }
    }
};
class Cat:public Animal
{
    public:
    string Name;
    vector <Animal*> response;
    Cat(string name)
    {
        Name=name;
    }
    string Getname()
    {
        return Name;
    }
    void Respond(Animal *animal,int value)
    {
        if(value>50)
        {
            cout<<"cat("<<Name<<"):"<<animal->Getname()<<" is there!"<<endl;
        }
    }
    void Add(Animal *animal)
    {
        response.push_back(animal);
    }
    void cry()
    {
        int value=rand();
        cout<<value<<endl;
        vector<Animal *>::iterator it;
        for(it=response.begin();it!=response.end();it++)
        {
            Animal *animal=*it;
            animal->Respond(this,value);
        }
    }
};
int main()
{
    Mouse m1("a"),m2("b");
    Human h1("c"),h2("d"),h3("e");
    Cat cat("tom");
    cat.Add(&m1);
    cat.Add(&m2);
    cat.Add(&h1);
    cat.Add(&h2);
    cat.Add(&h3);
    for(int i=0;i<10;i++)
    {
        cat.cry();
    }
    return 0;
}

天下雨 汽车开始减速  行人开始奔跑  小贩开始收摊

 

#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

class A
{
    public:
   // int value;
    virtual void Respond(A * p,int value)=0;

};
class Car:public A
{
    public:
    int value;
    void Respond (A*p,int value)
    {
        if(value>10000&&value<20000)
        {
            cout<<"rain!!! cars begin to slow down!"<<endl;
        }
        else
        {
            cout<<"The sky is not rainning! Cars continue go at the current speed!"<<endl;
        }
    }

};
class Footman:public A
{
    public:
  //  int value;
    void Respond(A*p ,int value)
    {
        if(value>10000&&value<20000)
        {
            cout<<"rain!!! Footmen begin to run!"<<endl;
        }
        else
        {
            cout<<"The sky is not rainning! Footmen continue to walk!"<<endl;
        }

    }

};
class Trader:public A
{
    public:
  //  int value;
    void Respond(A*P,int value)
    {
        if(value>10000&&value<20000)
        {
            cout<<"rain!!! Traders stop trading!"<<endl;
        }
        else
        {
            cout<<"The sky is not rainning! Traders continue trading!"<<endl;
        }

    }

};
class Sky:public A
{
    public:
  //  int value;
    vector<A*> R;
    void Respond(A*p,int value)
    {
        if(value>10000&&value<20000)
        {
            cout<<"The sky is rainning!"<<endl;
        }
    }
    void Add(A*p)
    {
        R.push_back(p);
    }
    void Rain()
    {
        int value=rand();
        cout<<value<<endl;
        vector<A*>::iterator it;
        for(it=R.begin();it!=R.end();it++)
        {
            A* p=*it;
            p->Respond(this,value);
        }
    }

};
int main()
{
    Car car;
    Footman footman;
    Trader trader;
    Sky sky;
    sky.Add(&car);
    sky.Add(&footman);
    sky.Add(&trader);
    for(int i=0;i<10;i++)
    {
        sky.Rain();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值