11周——警察和厨师为Polic类和Cook类增加了对象成员,请扩充代码

  1. /* 
  2. *Copyright (c) 2016,烟台大学计算机学院 
  3. *All rights reserved. 
  4. *文件名称:main.cpp 
  5. *作    者:李德彪
  6. *完成时间:2016年5月16日 
  7. *版 本 号:v1.0 
  8. * 
  9. *问题描述:项目-警察和厨师为Polic类和Cook类增加了对象成员,请扩充代码。 
  10. *输入描述:无。  
  11. *程序输出:信息。 
  12. */  
  13. #include<iostream>  
  14. #include <string>  
  15. using namespace std;  
  16. class Person  
  17. {  
  18.   
  19. public:  
  20.     Person(int, string);  
  21.     void action();  
  22.     string getName()  
  23.     {  
  24.         return name;  
  25.     }  
  26. private:  
  27.     int age;  
  28.     string name;  
  29. };  
  30. Person::Person(int a, string na)  
  31. {  
  32.     age=a;  
  33.     name=na;  
  34. }  
  35. void Person::action()  
  36. {  
  37.     cout<<name<<" do some action"<<endl;  
  38. }  
  39. class Police: public Person  
  40. {  
  41. public:  
  42.     Police(int, string, int,Person);  
  43.     //void arrest(Person);  
  44.     void show();  
  45. private:  
  46.     int level; //级别  
  47.      Person leader;  
  48. };  
  49. Police::Police(int a, string n, int l, Person p):Person(a,n),level(l),leader(p) {}  
  50. /*void Police::arrest(Person a) 
  51. { 
  52.     cout<<" Police "<<getName()<<" arrest " <<a.getName()<<endl; 
  53. }*/  
  54. void Police::show()  
  55. {  
  56.     cout<<"Police "<<getName()<<", leader is " <<leader.getName()<<endl;  
  57. }  
  58. class Cook: public Person  
  59. {  
  60. public:  
  61.     Cook(int, string, double,Police);  
  62.     //void getCake(int);  
  63.     void show();  
  64. private:  
  65.     double salary; //薪水  
  66.     Police protector;  
  67. };  
  68. Cook::Cook(int a, string n, double s,Police p):Person(a,n),salary(s),protector(p) {}  
  69. /*void Cook::getCake(int n) 
  70. { 
  71.     cout<<" Cook "<<getName()<<" gave me " <<n<<" cakes."<<endl; 
  72.  
  73. }*/  
  74. void Cook::show()  
  75. {  
  76.     cout<<"Cook "<<getName()<<" is protected by Police "<<protector.getName()<<endl;  
  77. }  
  78. int main()  
  79. {  
  80.     Person tom(120,"Tom");  
  81.     Police jack(30,"Jack",2,tom);  
  82.     Cook john(24,"John",5000,jack);  
  83.       jack.show();  
  84.     john.show();  
  85.     return 0;  
  86. }  
  87. /*
    *Copyright (c) 2016,烟台大学计算机学院
    *All rights reserved.
    *文件名称:main.cpp
    *作    者:李磊涛
    *完成时间:2016年5月7日
    *版 本 号:v1.0
    *
    *问题描述:项目-警察和厨师为Polic类和Cook类增加了对象成员,请扩充代码。
    *输入描述:无。 
    *程序输出:信息。
    */
    #include<iostream>
    #include <string>
    using namespace std;
    class Person
    {
    
    public:
        Person(int, string);
        void action();
        string getName()
        {
            return name;
        }
    private:
        int age;
        string name;
    };
    Person::Person(int a, string na)
    {
    	age=a;
    	name=na;
    }
    void Person::action()
    {
    	cout<<name<<" do some action"<<endl;
    }
    class Police: public Person
    {
    public:
        Police(int, string, int,Person);
        //void arrest(Person);
    	void show();
    private:
        int level; //级别
    	 Person leader;
    };
    Police::Police(int a, string n, int l, Person p):Person(a,n),level(l),leader(p) {}
    /*void Police::arrest(Person a)
    {
    	cout<<" Police "<<getName()<<" arrest " <<a.getName()<<endl;
    }*/
    void Police::show()
    {
        cout<<"Police "<<getName()<<", leader is " <<leader.getName()<<endl;
    }
    class Cook: public Person
    {
    public:
        Cook(int, string, double,Police);
        //void getCake(int);
    	void show();
    private:
        double salary; //薪水
    	Police protector;
    };
    Cook::Cook(int a, string n, double s,Police p):Person(a,n),salary(s),protector(p) {}
    /*void Cook::getCake(int n)
    {
        cout<<" Cook "<<getName()<<" gave me " <<n<<" cakes."<<endl;
    
    }*/
    void Cook::show()
    {
        cout<<"Cook "<<getName()<<" is protected by Police "<<protector.getName()<<endl;
    }
    int main()
    {
        Person tom(120,"Tom");
        Police jack(30,"Jack",2,tom);
        Cook john(24,"John",5000,jack);
          jack.show();
        john.show();
        return 0;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值