/*copyright(c)2016.烟台大学计算机学院 * All rights reserved, * 文件名称:text.Cpp * 作者:舒文超 * 完成日期:2016年5月6日 * 版本号:vc++6.0 * 问题描述:(1)下面的类图,为Polic类 和Cook类增加了对象成员, 请扩充代码,完成上述各项要 求 (图片见附件) 要求:各个成员函数,只要输出相关的信息 即可,暂不深究其业务功能请为各个类增加 构造函数在实现中,可以增加需要的其他函 数自行编制main函数,完成初步的测试。 */ #include <iostream> using namespace std; class Person { public: Person(int a, string n,string w):age(a),name(n),work(w){} void action(); string getName() { return name; } private: int age; string name; string work; }; void Person::action() { cout<<name<<" is a "<<work<<"."<<endl; } class Police: public Person { public: Police(int a, string n,string w,Person p,int l); void printleader(); void arrest(Person); private: int level; Person leader; }; Police::Police(int a,string n,string w,Person lea,int l):Person(a,n,w),leader(lea),level(l){} void Police::printleader() { action(); cout<<getName()<<"'s leader is " <<leader.getName()<<endl; } void Police::arrest(Person p) { cout<<"Police "<<getName()<<" arrest " <<p.getName()<<endl; } class Cook: public Person { public: Cook(int a, string n,string w,Police pro,double s); void getCake(int,Person); void printpro(); private: double salary; //薪水 Police protector; }; Cook::Cook(int a, string n,string w,Police pro,double s):Person(a,n,w),protector(pro),salary(s){} void Cook::printpro() { action(); cout<<getName()<<"'s protector is "<<protector.getName()<<endl; } void Cook::getCake(int n,Person p) { cout<<"Cooker "<<getName()<<" gave "<<p.getName()<<" "<<n<<" cakes."<<endl; } int main() { Person Bob(120,"Bob","public"); Police Tom(30,"Tom","police",Bob,2); Cook Jason(24,"Jason","cooker",Tom,5000); Tom.printleader(); Tom.arrest(Bob); Jason.printpro(); Jason.getCake(6,Bob); return 0; }
时间: 2024-10-13 01:03:04