警察和厨师-(2)

来源:互联网 发布:淘宝女鞋文案 编辑:程序博客网 时间:2024/04/28 02:07
  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4.   
  5. class Person  
  6. {  
  7. public:  
  8.     Person(int n = 0, string nam = " "):age(n),name(nam){}  
  9.     void action(){cout << name << " " << age << endl;};  
  10.     string getName(){return name;}  
  11.     void show_person()  
  12.     {  
  13.         cout << "age:" << age << endl;  
  14.         cout << "name:" << name << endl;  
  15.     }  
  16. private:  
  17.     int age;  
  18.     string name;  
  19. };  
  20.   
  21. class Polic:public Person  
  22. {  
  23. public:  
  24.     Polic(int n = 0, string nam = " "int lev = 0,int n1 = 0,string nam1 = " "):Person(n,nam),level(lev),leader(n1,nam1){}  
  25.     void arrest(Person &person){cout << person.getName() << " were arrested by police officer " << this->getName() << endl;}  
  26.     void show_polic()  
  27.     {  
  28.         cout << "leader:" << endl;  
  29.         leader.show_person();  
  30.         cout << "polic:" << endl;  
  31.         this->show_person();  
  32.         cout << "level" << level << endl;  
  33.     }  
  34. private:  
  35.     int level;  
  36.     Person leader;  
  37. };  
  38.   
  39. class Cook:public Person  
  40. {  
  41. public:  
  42.     Cook(int n = 0, string nam = " "double sal = 0, int n1 = 0, string nam1 = " "int lev = 0, int n2 = 0, string nam2 = " "):  
  43.         Person(n,nam),salary(sal),protector(n1,nam1,lev,n2,nam2){}  
  44.     string getCake(){return "ok";}  
  45.     void show_cake()  
  46.     {  
  47.         cout << "protector:" << endl;  
  48.         protector.show_polic();  
  49.         cout << "cake:" << endl;  
  50.         cout << "salary:" << salary << endl;  
  51.     }  
  52. private:  
  53.     double salary;  
  54.     Polic protector;  
  55. };  
测试函数:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. int main()  
  2. {  
  3.     Person p(20,"Jack");  
  4.     p.show_person();  
  5.     cout << endl;  
  6.     Polic po(18,"Bob",2,20,"Jack");  
  7.     po.show_polic();  
  8.     cout << endl;  
  9.     Cook c(22,"Marry",5000,18,"Bob",2,20,"Jack");  
  10.     c.show_cake();  
  11.     return 0;  
  12. }  
0 0
原创粉丝点击