#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;
}