第十一週專案訓練10.1警察和廚子
阿新 • • 發佈:2019-02-20
/*
*Copyright(c) 2016.煙臺大學計算機與控制工程學院
*ALL rights reserved.
*檔名稱:main.cpp
*作者:王璵璠
*完成日期:2016年6月2日
問題描述:根據下面的圖,定義各個類:
要求:請為各個類增加建構函式,可以增加需要的其他函式,自行編制main函式,完成初步的測試
程式:
#include <iostream> using namespace std; class Person { public: Person(int a,string nam):age(a),name(nam){} void action(); string getName() { return name; } private: int age; string name; }; void Person::action() { cout<<name<<endl; } class Police:public Person { public: Police(int a,string nam,int l); void arrest(Person); private: int level; //級別 }; Police::Police(int a,string nam,int l):Person(a,nam),level(l){} void Police::arrest(Person p) { cout<<" Police "<<getName()<<" arrest "<<p.getName()<<endl; } class Cook:public Person { public: Cook(int a,string nam,double s); void getCake(int ); private: double salary; //薪水 }; Cook::Cook(int a,string nam,double s):Person(a,nam),salary(s){} void Cook::getCake(int c) { cout<<" Cook "<<getName()<<" gave me " <<c<<" cakes "<<endl; } int main() { Person tom(120,"Tom"); Police jack(30,"Jack",2); Cook john(24,"John",5000); jack.arrest(tom); john.getCake(4); return 0; }
執行結果: