代码如下:
taos@ubuntu:~/code/cpp/cxymsbd$ cat 15.1.4.cpp
#include
#include
#include
#include
using namespace std;
class Manager{
protected:
Manager * m_manager;
string m_name;
public:
Manager(Manager * direct_boss, string boss_name){
m_manager = direct_boss;
m_name = boss_name;
srand(unsigned(time(NULL)));
}
virtual void dealWithRequest(string employee_name, int money) = 0;
};
class BigBoss : public Manager{
public:
BigBoss(Manager * direct_boss, string boss_name) : Manager(direct_boss, boss_name){}
virtual void dealWithRequest(string employee_name, int money){
cout << employee_name << " want increase " << money << endl;
if (rand() % 1000 >= 800){
cout << "BigBoss allowed"<< employee_name <<" increasing " << money << ", haha!!!" << endl;
}else {
cout << "BigBoss : NOWAY!!!" << endl;
}
}
};
class majorManager : public Manager{
public:
majorManager(Manager * direct_boss, string boss_name) : Manager(direct_boss, boss_name){}
virtual void dealWithRequest(string employee_name, int money){
if(money < 2000){
cout << employee_name << " want increase " << money << endl;
if (rand() % 1000 >= 500){
cout << "majorMamager allowed "<< employee_name <<" increasing " << money << ", haha!!!" << endl;
}else {
cout << "majorManager : NOWAY!!!" << endl;
}
}else{
m_manager->dealWithRequest(employee_name, money);
}
}
};
class commonManager : public Manager{
public:
commonManager(Manager * direct_boss, string boss_name): Manager(direct_boss, boss_name){}
virtual void dealWithRequest(string employee_name, int money){
if(money < 800){
cout << employee_name << " want increase " << money << endl;
if (rand() % 1000 >= 300){
cout << "commonManager allowed "<< employee_name <<" increasing " << money << ", haha!!!" << endl;
}else {
cout << "commonManager : NOWAY!!!" << endl;
}
}else{
m_manager->dealWithRequest(employee_name, money);
}
}
};
int main(){
BigBoss BigBoom(NULL,"taoge");
majorManager senior(&BigBoom,"major");
commonManager common(&senior,"common_manager_oo");
common.dealWithRequest("a",400);
common.dealWithRequest("b",500);
common.dealWithRequest("c",800);
common.dealWithRequest("d",1000);
common.dealWithRequest("e",5000);
return 0;
}
结果如下:
taos@ubuntu:~/code/cpp/cxymsbd$ ./a.out
a want increase 400
commonManager allowed a increasing 400, haha
b want increase 500
commonManager allowed b increasing 500, haha
c want increase 800
majorManager : NOWAY!!!
d want increase 1000
majorMamager allowed d increasing 1000, haha
e want increase 5000
BigBoss : NOWAY!!!
敲一敲也有意思 O(∩_∩)O哈哈哈~