标签:
/*Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 李钊 *完成日期 : 2016年5月10号 *版 本 号 : v1.0 * *问题描述 : 警察与厨师的派生类 *输入描述 : *程序输出 : 人的行为警察的动作厨师的动作 */ #include <iostream> using namespace std; class Person { protected: int age; string name; public: void action(); Person(int a,string nam); string getname(); }; Person::Person(int a,string nam) { age=a; name=nam; } string Person::getname() { return name; } void Person::action() { cout<<"she is "<<name<<" she steal a heart "<<endl; } class Police:public Person { private: int level; Person leader; public: Police(int a,string nam,int l,int la,string ln); void arrest(Person); void show(); }; Police::Police(int a,string nam,int l,int la,string ln):Person(a,nam),level(l),leader(la,ln){} void Police::show() { cout<<leader.getname()<<" is leader"<<endl; } void Police::arrest(Person human) { cout<<name<<" arrest "<<human.getname()<<" because she steal something from "<<name<<endl; } class Cooker:public Person { private: double salary; Police protecter; public: void cooking(); Cooker(int a,string nam,double s,int pa,string pn,int p1,int p1a,string p1n); void show(); }; Cooker::Cooker(int a,string nam,double s,int pa,string pn,int p1,int p1a,string p1n):Person(a,nam),salary(s),protecter(pa,pn,p1,p1a,p1n){} void Cooker::cooking() { cout<<"she make a cake for lizhao and get "<<salary<<endl; } void Cooker::show() { cout<<protecter.getname()<<" protect "<<getname()<<endl; } int main() { Person w(19,"wangyilin"); Police l(18,"lizhao",100,20,"xiaoxiaozhao"); Cooker p(20,"laowang",1500,25,"dalin",3000,26,"sidalin"); w.action(); l.arrest(w); p.cooking(); l.show(); p.show(); return 0; }
运行结果:
学习心得:
略
知识点总结:
增加对象成员难度增加
标签:
原文地址:http://blog.csdn.net/l347037341/article/details/51365244