/*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; public: Police(int a,string nam,int l); void arrest(Person); }; Police::Police(int a,string nam,int l):Person(a,nam),level(l){} void Police::arrest(Person human) { cout<<name<<" arrest "<<human.getname()<<" because she steal something from "<<name<<endl; } class Cooker:public Person { private: double salary; public: void cooking(); Cooker(int a,string nam,double s); }; Cooker::Cooker(int a,string nam,double s):Person(a,nam),salary(s){} void Cooker::cooking() { cout<<"she make a cake for lizhao and get "<<salary<<endl; } int main() { Person w(19,"wangyilin"); Police l(18,"lizhao",100); Cooker p(20,"laowang",1500); w.action(); l.arrest(w); p.cooking(); return 0; }
运行结果:
学习心得:
派生类的简单运用
知识点总结:
继承
时间: 2024-11-05 23:33:56