/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年5月8号 *版 本 号 : v6.0 * *问题描述 : *输入描述 : 无 *程序输出 : */ #include<iostream> #include<cstring> using namespace std; class Person { public: Person(int a,string nam):age(a),name(nam){} void action(); void showperson(); int getage(){return age;} string getname(){return name;} private: int age; string name; }; void Person::action() { cout<<"姓名:"<<name<<" 年龄:"<<age<<" 平时最爱做的事为吃饭、睡觉、打豆豆!!!"<<endl; } void Person::showperson() { cout<<"姓名:"<<name<<endl; cout<<"年龄:"<<age<<endl; } class Polic:public Person { public: Polic(int a,string nam,int l,int a1,string nam1):Person(a,nam),level(l),leader(a1,nam1){} void arrest(Person); void showpolic(); private: int level; Person leader; }; void Polic::arrest(Person per) { cout<<"获得犯罪分子的姓名:"<<per.getname()<<" 获得犯罪分子的年龄:"<<per.getage()<<"\n************该罪犯被逮获!!!"<<endl; } void Polic::showpolic() { leader.showperson(); cout<<"等级: "<<level<<endl; } class Cook:public Polic { public: Cook(int a,string nam,int l,int a1,string nam1,double s):Polic(a,nam,l,a1,nam1),salary(s),protector(a,nam,l,a1,nam1){} string getcake(int); void showprotector(); private: double salary; Polic protector; string cake; }; void Cook::showprotector() { cout<<"保护者为:"<<endl; protector.showpolic(); } string Cook::getcake(int n) { switch(n) { case 0: cake="Helveticrolls"; return cake; case 1: cake="Tiramisu"; return cake; case 2: cake="Souffle"; return cake; case 3: cake="BostonCreamPie"; return cake; case 4: cake="Yule log"; return cake; case 5: cake="Cheesecake"; return cake; default : cake="不存在"; return cake; } } int main() { int n; string ccake; Person per(23,"Alice"); Polic po(18,"juice",6,45,"orange"); Cook co(56,"apple",9,12,"banana",2000); per.action(); po.arrest(po); po.showpolic(); co.showprotector(); cout<<"0*************************** Helveticrolls"<<endl; cout<<"1*************************** Tiramisu"<<endl; cout<<"2*************************** Souffle"<<endl; cout<<"3*************************** BostonCreamPie"<<endl; cout<<"4*************************** Yule log"<<endl; cout<<"5*************************** Cheesecake"<<endl; cout<<"请输入所选蛋糕的序号:"; cin>>n; ccake=co.getcake(n); cout<<"您所选的蛋糕为:"<<ccake<<endl; return 0; }
运行结果:
时间: 2024-11-23 00:21:58