问题及代码:
</pre><pre name="code" class="cpp">/*copyright(c)2016.烟台大学计算机学院
* All rights reserved,
* 文件名称:text.Cpp
* 作者:吴敬超
* 完成日期:2016年5月4日
* 版本号:codeblock
*
* 问题描述: 警察和厨师
* 输入描述:
* 程序输出: 输出结果
*/
#include<iostream>
using namespace std;
class Person
{
public:
Person(int a,string nam);
void action();
string getName()
{
return name;
}
protected:
int age;
string name;
};
Person::Person(int a,string nam)
{
age=a;
name=nam;
}
void Person::action()
{
cout<<name<<" do some action"<<endl;
}
class Police:public Person
{
public:
Police(int a,string nam,int le,Person);
void arrest(Person);
void show();
protected:
int level;
Person leader;
};
Police::Police(int a,string nam,int le,Person lead):Person(a,nam),level(le),leader(lead){}
void Police::arrest(Person lead)
{
cout<<" Police "<<getName()<<" arrest " <<lead.getName()<<endl;
}
void Police::show()
{
cout<<"Police "<<getName()<<", leader is " <<leader.getName()<<endl;
}
class Cook:public Person
{
public:
Cook(int a,string nam,double salary,Person p);
void getCake(int );
void show();
protected:
double salary;
Person protector;
};
Cook::Cook(int a,string nam,double s,Person p):Person(a,nam),salary(s),protector(p){}
void Cook::getCake(int n)
{
cout<<" Cook "<<getName()<<" gave me " <<n<<" cakes."<<endl;
}
void Cook::show()
{
cout<<"Cook "<<getName()<<" is protected by Police "<<protector.getName()<<endl;
}
int main()
{
Person jerry(43,"Jerry");
Police jack(30,"Jack",2,jerry);
Cook john(24,"John",5000,jack);
jack.show();
john.show();
return 0;
}
运行结果: