码迷,mamicode.com
首页 > 其他好文 > 详细

第10、 11周-【项目-警察和厨师】

时间:2016-05-13 00:23:23      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

问题及代码:

/*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);
    void arrest(Person);
private:
    int level;// 级别
};
Police::Police(int a,string n,string s,int l):Person(a,n,s),level(l){}
void Police::arrest(Person p)
{
    cout<<"Police "<<" "<<getN()<<" "<<"whose sex is "<<p.getS()<<"  "<<"arrrest"<<" "<<p.getN()<<endl;
}
class Cook:public Person
{
public:
    Cook(int ,string,string,double);
    void getC(int );
private:
    double salary;

};
Cook::Cook(int a,string n,string s, double sa):Person(a,n,s),salary(sa){}
void Cook::getC(int n)
{
    cout<<" A Cook a "<<" "<<getS()<<" "<<getN()<<" "<<"give"<<" "<<n<<" "<<"cakes"<<endl;
}

int main()
{
    Person Tom(120,"Tom","male");
    Police Jack(30,"Jack","male",2);
    Cook Mary(24,"Mary","female",5000);
    Jack.arrest(Tom);
    Mary.getC(4);
    return 0;
}

运行结果:

技术分享

知识点总结:

在类的继承构造函数只被调用一次

第10、 11周-【项目-警察和厨师】

标签:

原文地址:http://blog.csdn.net/qq_33267291/article/details/51345281

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!