第10、11周-警察与厨师

问题及代码:

 
 /*copyright 计算机与控制工程学院
完成日期:2016年5月8日
作者:马艳艳
问题描述:各个成员函数,只要输出相关的信息即可,暂不深究其业务功能
请为各个类增加构造函数
在实现中,可以增加需要的其他函数
自行编制main函数,完成初步的测试
输入描述:无
输出描述:成员信息
*/
#include <iostream>

using namespace std;
class Person
{
public:
    Person(int ,string ,string);
    void action();
    string getN(){return name;}
    int getA(){return age;}
    string  getS(){return sex;}
private:
    int age;
    string name;
    string sex;
};
Person::Person (int a,string n,string s):age(a),name(n),sex(s){}//给人,定义构造函数,包括年名名字性别
void Person::action()
{
    cout<<name<<"is doing something "<<endl;
}
class Police:public Person
{
public:
    Police (int ,string,string ,int,Person);
    void arrest(Person);
    void show();
private:
    int level;// 级别
    Person leader;
};
Police::Police(int a,string n,string s,int l,Person p):Person(a,n,s),level(l),leader(p){}
void Police::arrest(Person p)
{
    cout<<"Police "<<" "<<getN()<<" "<<"whose sex is "<<p.getS()<<"  "<<"arrrest"<<" "<<p.getN()<<endl;
}
void Police:: show()
{
    cout<<"Police "<<getN()<<", leader is  "<<leader.getN()<<endl;
}
class Cook:public Person
{
public:
    Cook(int ,string,string,double,Police );
    void getC(int );
    void show();
private:
    double salary;
    Police protector;

};
Cook::Cook(int a,string n,string s, double sa,Police p):Person(a,n,s),salary(sa),protector(p){}
void Cook::getC(int n)
{
    cout<<" A Cook a "<<" "<<getS()<<" "<<getN()<<" "<<"give"<<" "<<n<<" "<<"cakes"<<endl;
}
void Cook::show()
{
    cout<<"Cook "<<getN()<<"  is protected by police "<<protector.getN()<<endl;
}
int main()
{
    Person Tom(40,"Tom","male");
    Police Jack(30,"Jack","male",2,Tom);
    Cook Mary(24,"Mary","female",5000,Jack);
    Jack.show();
    Mary.show();
    return 0;
}

运行结果:

知识点总结:

我们希望看到jack警察的上司就是一个人,john厨师的保卫者,就是一个警察。
- 需要做的是,利用对象作为构造函数的参数,使结构清晰。
- 当然,这时需要增加相关的复制构造函数了。可以使用默认复制构造函数。


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值