第十周第十一周项目3(2)

/*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称 :
*作    者 : 李钊
*完成日期 : 2016年5月10号
*版 本 号 : v1.0
*
*问题描述 :    警察与厨师的派生类
*输入描述 :
*程序输出 :    人的行为警察的动作厨师的动作
*/

#include <iostream>
using namespace std;
class Person
{
protected:
    int age;
    string name;
public:
    void action();
    Person(int a,string nam);
    string getname();
};
Person::Person(int a,string nam)
{
    age=a;
    name=nam;
}
string Person::getname()
{
    return name;
}
void Person::action()
{
    cout<<"she is "<<name<<" she steal a heart "<<endl;
}
class Police:public Person
{
private:
    int level;
    Person leader;
public:
    Police(int a,string nam,int l,int la,string ln);
    void arrest(Person);
    void show();

};
Police::Police(int a,string nam,int l,int la,string ln):Person(a,nam),level(l),leader(la,ln){}
void Police::show()
{
    cout<<leader.getname()<<" is leader"<<endl;
}
void Police::arrest(Person human)
{
    cout<<name<<" arrest "<<human.getname()<<" because she steal something from "<<name<<endl;
}
class Cooker:public Person
{
private:
    double salary;
    Police protecter;
public:
    void cooking();
    Cooker(int a,string nam,double s,int pa,string pn,int p1,int p1a,string p1n);
    void show();

};
Cooker::Cooker(int a,string nam,double s,int pa,string pn,int p1,int p1a,string p1n):Person(a,nam),salary(s),protecter(pa,pn,p1,p1a,p1n){}
void Cooker::cooking()
{
    cout<<"she make a cake for lizhao and get "<<salary<<endl;
}
void Cooker::show()
{
    cout<<protecter.getname()<<" protect "<<getname()<<endl;
}
int main()
{
    Person w(19,"wangyilin");
    Police l(18,"lizhao",100,20,"xiaoxiaozhao");
    Cooker p(20,"laowang",1500,25,"dalin",3000,26,"sidalin");
    w.action();
    l.arrest(w);
    p.cooking();
    l.show();
    p.show();
    return 0;
}

运行结果:

学习心得:

知识点总结:

增加对象成员难度增加

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值