问题及代码:
/*Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:main.cpp *作 者:王艺霖 *完成日期:2016年5月9日 *版 本 号:v1.0 *问题描述:为Polic类和Cook类增加了对象成员,请扩充代码,完成上述各项要求 *输入描述: *输出描述: */ #include<iostream> #include<string> using namespace std; class Person { public: Person(int a,string n); void action(); string getname() { return name; } private: int age; string name; }; Person::Person(int a,string n):age(a),name(n){}; void Person::action() { cout<<name<<"开始干活!"<<endl; } class Polic: public Person { public: Polic(int a,string n,int l,int la, string ln); void arrest(Person); private: int level; Person leader; }; Polic::Polic(int a,string n,int l,int la, string ln):Person(a,n),level(l),leader(la,ln){}; void Polic::arrest(Person w) { cout<<"警察"<<getname()<<"抓获"<<w.getname()<<",警察局长"<<leader.getname()<<endl; } class Cook: public Person { public: Cook(int a,string nam,double s,int pa, string pn, int pl, int pla, string pln); void getCake(int); private: double salary; Polic protector; }; Cook::Cook(int a,string n,double s,int pa, string pn, int pl, int pla, string pln):Person(a,n),salary(s),protector(pl,pn,pl,pla,pln){}; void Cook::getCake(int n) { cout<<"厨师"<<getname()<<"送给她"<<n<<"个蛋糕"<<"并且被"<<protector.getname()<<"保护"<<endl; } int main() { Person Lz(19,"lz"); Polic Wyl(19,"wyl",2,40,"hlj"); Cook Xxz(2,"xxl",10000,30,"wyl",2,40,"hlj"); Wyl.arrest(Lz); Xxz.getCake(10); return 0; }
运行结果: