标签:
问题及代码:
/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:zwj.cpp
*作 者:张伟晶
*完成日期:2016年5月8日
*版 本 号:v1.0
*
*问题描述:警察和厨师
*输入描述:
*程序输出:
*/
#include<iostream>
#include<string>
using namespace std;
class Person
{
private:
int age;
string name;
public:
Person(int ,string);
void action();
// int getage(){return age;}
string getname(){return name;}
};
Person::Person(int a,string nam):age(a),name(nam){}
class Polic:public Person
{
protected:
int level;
public:
Polic(int ,string ,int);
void arrest(Person);
};
Polic::Polic(int a,string nam,int l):Person(a,nam),level(l){}
class Cook:public Person
{
private:
double salary;
public:
Cook(int ,string ,double);
string getCake(int);
};
Cook::Cook(int a,string nam,double s):Person(a,nam),salary(s){}
void Person::action()
{
cout<<name<<" have some action ."<<endl;
}
void Polic::arrest(Person p)
{
cout<<getname()<<" arrest "<<p.getname()<<endl;
}
string Cook::getCake(int a)
{
cout<<getname()<<" get "<<a<<" cakes."<<endl;
return getname();
}
int main()
{
Person p(20,"person1");
Polic police(23,"police2",3);
Cook cook(19,"cook3",5000);
police.arrest(p);
cook.getCake(5);
return 0;
}
运行结果:
知识点总结:
公有继承情况下,派生类不可以直接访问基类的私有成员。
学习心得:
标签:
原文地址:http://blog.csdn.net/angeljing521/article/details/51344430