第十周——警察与厨师

  1. /*  
  2. *Copyright (c) 2015,烟台大学计算机学院  
  3. *All rights reserved.  
  4. *文件名称:text.cpp  
  5. *作者:李德彪  
  6. *完成日期:2015年5月16日  
  7. *版本号:v1.0  
  8.  
  9. *问题描述:为各个类增加构造函数以及其他函数,并自行编制main函数,完成初步测试  
  10. *输入描述: 无  
  11. *程序输出:此次测试信息 
  12. */  
  13. #include <iostream>  
  14. using namespace std;  
  15. class Person  
  16. {  
  17.   
  18. public:  
  19.     Person(int, string);  
  20.     void action();  
  21.     string getName()  
  22.     {  
  23.         return name;  
  24.     }  
  25. private:  
  26.     int age;  
  27.     string name;  
  28. };  
  29. Person::Person(int a, string n):age(a), name(n) {}  
  30. void Person::action()  
  31. {  
  32.     cout<<name<<" do some action"<<endl;  
  33. }  
  34. class Police: public Person  
  35. {  
  36. public:  
  37.     Police(int, string, int);  
  38.     void arrest(Person);  
  39. private:  
  40.     int level; 
  41. };  
  42. Police::Police(int a, string n, int l):Person(a,n),level(l) {}  
  43. void Police::arrest(Person p)  
  44. {  
  45.     cout<<" Police "<<getName()<<" arrest " <<p.getName()<<endl;  
  46. }  
  47. class Cook: public Person  
  48. {  
  49. public:  
  50.     Cook(int, string, double);  
  51.     void getCake(int);  
  52. private:  
  53.     double salary; //нˮ  
  54. };  
  55. Cook::Cook(int a, string n, double s):Person(a,n),salary(s) {}  
  56. void Cook::getCake(int n)  
  57. {  
  58.     cout<<" Cook "<<getName()<<" gave me " <<n<<" cakes."<<endl;  
  59. }  
  60.   
  61. int main()  
  62. {  
  63.     Person tom(120,"Tom");  
  64.     Police jack(30,"Jack",2);  
  65.     Cook john(24,"John",5000);  
  66.     jack.arrest(tom);  
  67.     john.getCake(4);  
  68.     return 0;  
  69. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值