




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、c + +實(shí) 驗(yàn) 8 - 繼 承 與 派生上機(jī)練習(xí)題精品文檔1. 定義一個哺乳動物類 Mamma,l 并從中派生出一個狗類 Dog,下面給出Mamma類l 的定義,要求:(1) 添加 Dog類的顏色數(shù)據(jù)成員,訪問屬性為私有,通過 SetColor 和 GetColor 成員函數(shù)來對顏色進(jìn)行設(shè)置和獲取。(2) 分別為基類和派生類添加相應(yīng)的構(gòu)造函數(shù)(有參、無參)和析構(gòu)函 數(shù),并進(jìn)行測試。class Mammalprotected:int itsAge;int itsWeight;public:int GetAge()return itsAge;void SetAge(int age) itsAge
2、=age;int GetWeight() return itsWeight;void SetWeight(int weight) itsWeight= weight;class Dog : public Mammal/ 定義 Dog 類的數(shù)據(jù)成員和成員函數(shù);改:#include #include using namespace std;class Mammalprotected:int itsAge;int itsWeight;public:Mammal();Mammal();收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔int GetAge()return itsAge;void SetAge
3、(int age) itsAge=age;int GetWeight() return itsWeight;void SetWeight(int weight) itsWeight= weight; ;class Dog : public Mammal protected:char itscolor20; public:Dog();void Setcolor(char *color) strcpy(itscolor,color);void getcolor()cout 狗的顏色 itscolorendl;/ 定義 Dog 類的數(shù)據(jù)成員和成員函數(shù) ;/ Mammal:Mammal() int a
4、ge1,weight1;cout 請輸入動物的年齡 :age1; SetAge(age1);cout 請輸入動物的體重 :weight1;SetWeight(weight1);Mammal:Mammal()coutDestructor called.endl;Dog:Dog() char color20;cout 請輸入狗的顏色 :color;Setcolor(color);cout 狗的顏色 itscolor 體重 GetWeight() 年齡 GetAge()endl;void main()Dog dog1;( 4)設(shè)計(jì)人員基類 Person。其成員包括: 數(shù)據(jù)成員:姓名(字符數(shù)組)、性別
5、(字符數(shù)組)和年齡(整型) 成員函數(shù): SetPerson ,設(shè)置人員數(shù)據(jù)函數(shù);DisplayPerson ,顯示人員數(shù)據(jù)函數(shù);設(shè)計(jì)派生類 1:Teacher ,派生于 Person。新增成員包括: 數(shù)據(jù)成員:職稱(字符數(shù)組)、教研室(字符數(shù)組)和所授課程(字符數(shù) 組)成員函數(shù): SetTeacher ,設(shè)置數(shù)據(jù)成員函數(shù);DisplayTeacher ,顯示數(shù)據(jù)成員函數(shù);設(shè)計(jì)派生類 2:Student ,派生于 Person。新增成員包括: 數(shù)據(jù)成員:專業(yè)(字符數(shù)組)、班級(字符數(shù)組)和類別( int ) 其中類別取值: 1(本科生)、 2(碩士生)、 3(博士生) 成員函數(shù): SetStud
6、ent ,設(shè)置數(shù)據(jù)成員函數(shù);DisplayStudent ,顯示數(shù)據(jù)成員函數(shù);收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔設(shè)計(jì)派生類 3:PostDoctor (博士后),多重繼承于 Student 與 Teacher 。新增成員包括:數(shù)據(jù)成員:無成員函數(shù): SetPostDoctor ,設(shè)置數(shù)據(jù)成員函數(shù);DisplayPostDoctor ,顯示數(shù)據(jù)成員函數(shù);主函數(shù):輸入并輸出一個教師、一個本科生、一個博士后數(shù)據(jù)。#include #include using namespace std;#define n 20/ 類的定義 class Personprotected:char namen;
7、char sexn;int age;public:Person();void setperson();void displayperson();class Teacher :virtual public Personprotected:char jobn;char roomn;char subjectn;public :Teacher();void setteacher();void displayteacher();收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔class Student:virtual public Person protected: char majorn; char ba
8、njin; int leibie;public : Student(); void setstudent(); void displaystudent();class Postdoctor:public Teacher,public Student public : Postdoctor(); void setpostdoctor(); void displaypostdoctor();/ 結(jié)構(gòu)函數(shù)Person:Person()setperson();Teacher:Teacher()setteacher();Student:Student()setstudent();Postdoctor:P
9、ostdoctor()/ 設(shè)置數(shù)據(jù) /收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔void Person:setperson()cout*name;cout*sex;cout*age;void Teacher:setteacher()cout*job;cout*room;cout*subject;void Student:setstudent()cout*major; cout*banji;cout*leibie;/ 數(shù)據(jù)顯示 /void Person:displayperson()cout姓名 :name性別 :sex 年齡:age;收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔void Te
10、acher:displayteacher() displayperson();cout職稱:job 教研室:room所授課程 :subjectendl;void Student:displaystudent() displayperson();cout專業(yè):major班級:banji 類別:leibieendl;void Postdoctor:displaypostdoctor() displayperson();cout職稱:job 教研室:room所授課程 :subject專業(yè):major班級:banji 類別:博士后endl;/ void main() cout 您正在輸入一個老師的信息 :endl;Teacher t1;coutsyy割endl;cout 您正在輸入一個學(xué)生的信息 :endl;Student s1;cout cout 您正在輸入一個博士后的信息 :endl;syy
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 科技助力智能設(shè)備在老年人口腔健康管理中的應(yīng)用
- 科技培訓(xùn)用戶心理剖析及個性化服務(wù)設(shè)計(jì)
- 科技與商業(yè)模式的深度融合探討
- 咖啡連鎖股權(quán)轉(zhuǎn)讓居間合同
- 知識產(chǎn)權(quán)交易市場的商業(yè)模式創(chuàng)新
- 農(nóng)藥運(yùn)輸司機(jī)勞動合同樣本
- 花店裝修合同終止
- 基于深度學(xué)習(xí)的口腔曲面斷層片目標(biāo)識別方法
- 電纜材料配送協(xié)議范本
- 在線藥店連鎖經(jīng)營與配送服務(wù)行業(yè)跨境出海戰(zhàn)略研究報告
- 高甘油三酯血癥性急性胰腺炎診治急診專家共識2021解讀
- 雕版印刷術(shù)的發(fā)明《金剛經(jīng)》課件
- 精密測量課程中的思政案例弘揚(yáng)工匠精神助力科技強(qiáng)國
- 殘疾人就業(yè)服務(wù)
- 傳統(tǒng)的中國紋樣與飾品設(shè)計(jì)
- 工業(yè)園區(qū)消防培訓(xùn)課件
- 供水管網(wǎng)項(xiàng)目背景
- 淺層高效氣浮池技術(shù)說明
- 小學(xué)大觀念教學(xué):設(shè)計(jì)與實(shí)施
- 《安全原理》習(xí)題庫及參考答案
-
評論
0/150
提交評論