版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、第4章 繼承與派生一、簡答題1. 有以下程序結(jié)構(gòu),請分析訪問屬性。class A /A為基類public: void func1( ); int i;protected: void func2( ); int j;private:int k; ;class B: public A /B為A的公用派生類public : void func3( ) ;protected: int m;private :int n;class C: public B / C為B的公用派生類public:void func4( );private:int p;int main( ) A a; /a是基類A的對象 B
2、b; /b是派生類B的對象C c; /c是派生類C的對象return 0; 問:(1)在main函數(shù)中能否用b.i,b.j 和b.k 訪問派生類B對象b中基類A的成員?(2)派生類B中的成員函數(shù)能否調(diào)用基類A中的成員函數(shù)func1和func2?(3)派生類B中的成員函數(shù)能否訪問基類A中的數(shù)據(jù)成員i,j,k?(4)能否在main函數(shù)中用c.i,c.j,c.k,c.m,c.n,c.p訪問基類A的成員i,j,k,派生類B的成員m,n,以及派生類C的成員p?(5)能否在main函數(shù)中用c. func1( ),c. func2( ),c. func3( )和c. func4( )調(diào)用func1,func
3、2,func3,func4成員函數(shù)?(6)派生類C的成員函數(shù)func4能否調(diào)用基類A中的成員函數(shù)func1,func2和派生類B中的成員函數(shù)func3?【答案要點(diǎn)】各成員在各類的范圍內(nèi)的訪問權(quán)限如下表:類的范圍func1ifunc2jkfunc3mnfunc4p基類A公用公用保護(hù)保護(hù)私有公用派生類B公用公用保護(hù)保護(hù)不可訪問公用保護(hù)私有公用派生類C公用公用保護(hù)保護(hù)不可訪問公用保護(hù)不可訪問公用私有(1)在main 函數(shù)中能用b.i訪問派生類B對象b中基類A的成員i,因?yàn)樗谂缮怋中是公用數(shù)據(jù)成員。不能用b.j訪問派生類B對象b中基類A的成員j,因?yàn)樗谂缮怋中是保護(hù)數(shù)據(jù)成員,不能被類外訪問。不
4、能用b.k訪問派生類B對象b中基類A的成員k,因?yàn)樗腔怉的私用數(shù)據(jù)成員,只有基類A的成員函數(shù)可以訪問,不能被類外訪問。(2)派生類B中的成員函數(shù)能調(diào)用基類A中的成員函數(shù)func1和func2,因?yàn)閒unc1、func2在派生類B中是公用成員和保護(hù)成員,可以被派生類的成員函數(shù)訪問。(3)派生類B中的成員函數(shù)能訪問基類A中的數(shù)據(jù)成員i、j,因?yàn)閕、j在派生類B中是公用成員和保護(hù)成員,可以被派生類的成員函數(shù)訪問。派生類B中的成員函數(shù)不能訪問基類A中的數(shù)據(jù)成員k,因?yàn)樗谂缮怋中是不可訪問的成員。(4)能在main 函數(shù)中用c.i訪問基類A的成員i,不能用c.j、c.k訪問基類A的成員j、k,因
5、為它們在派生類C中是保護(hù)成員和私有成員,不能被類外訪問。也不能用c.m、c.n訪問派生類B的成員m、n,因?yàn)樗鼈冊谂缮怌中也是保護(hù)成員和私有成員,不能被類外訪問。也不能用c.p訪問派生類C中的私用成員p。(5)能在main函數(shù)中用c. func1()、c. func3()和c. func4()調(diào)用func1、func3、func4成員函數(shù),因?yàn)樗鼈冊谂缮怌中是公用成員函數(shù),可以在類外被訪問。不能在main函數(shù)中用c. func2()調(diào)用func2成員函數(shù),因?yàn)樗谂缮怌中是保護(hù)成員函數(shù),不能在類外被訪問。(6)派生類C的成員函數(shù)func4能調(diào)用基類A中的成員函數(shù)func1、func2和派
6、生類中的成員函數(shù)func3,因?yàn)閒unc1、func3在派生類C中是公用成員函數(shù),func2在派生類C中是保護(hù)成員函數(shù),都可以被派生類C的成員函數(shù)調(diào)用。2. 已給商品類及其多層的派生類。以商品類為基類。第一層派生出服裝類、家電類、車輛類。第二層派生出襯衣類、外衣類、帽子類、鞋子類;空調(diào)類、電視類、音響類;自行車類、轎車類、摩托車類。請給出商品類及其多層派生類的基本屬性和派生過程中增加的屬性?!敬鸢敢c(diǎn)】按題意沒有操作,所以只列出各個(gè)類的數(shù)據(jù)成員,也不再在main函數(shù)中對各類進(jìn)行測試。#includeusing namespace std;class Commoditydouble price;
7、 /價(jià)格char name20; /商品名char manufacturer20; /生產(chǎn)廠家int items; /數(shù)量; class Clothing: public Commodity /服裝類char texture20; /材料質(zhì)地; class ElectricAppliance: public Commodity /家電類enum Black,Whitetype; /黑白家電; class Vehicle: public Commodity /車輛類int wheelNum; /車輪數(shù)量; class Shirt: public Clothing /襯衣類 enum Formal
8、,Casualstyle; /式樣:正式、休閑; class Garment: public Clothing /外衣類enum Jacket,Coatstyle; /式樣:夾克、外套 ; class Hat: public Clothing /帽子類enum Winter,Summer,Spring,Autumnstyle; /季節(jié)風(fēng)格; class Shoes: public Clothing /鞋子類enum Winter,Summer,Spring,Autumnstyle; /季節(jié)風(fēng)格; class AirCondition: public ElectricAppliance /空調(diào)b
9、ool warmCool; /是否冷暖float power; /功率; class Television: public ElectricAppliance /電視類int size; /尺寸 bool isColor; /是否彩色 ; class Acoustics: public ElectricAppliance /音響類int speakerNum; /喇叭數(shù)目 float power; /功率 ; class Bicycle: public Vehicle /自行車類int speedGrades; /調(diào)速級數(shù)int wheelSize; /輪子大小 ; class Car: pu
10、blic Vehicle /轎車類float volume; /排氣量bool isSkyLight; /是否有天窗int boxNum; /廂數(shù); class Motorcycle: public Vehicle /摩托車類float volume; /排氣量 ; int main() return 0; 二、編程題1定義一個(gè)國家基類Country,包含國名、首都、人口等屬性,派生出省類Province,增加省會(huì)城市、人口數(shù)量屬性。【程序參考代碼】#includeusing namespace std;#includeclass Countrypublic:Country(char *n,c
11、har *c,double p) strcpy(name,n); strcpy(capital,c); population=p; void print() coutname,capital,populationendl; private:char name10,capital50;double population;class Province:private Country public: Province(char *n,char *c,double p,char *cc,double pp):Country(n,c,p) strcpy(provinceCapital,cc); prov
12、incePopulation=pp; void display() print(); coutprovinceCapital, provincePopulationendl;private:char provinceCapital50;double provincePopulation;int main() Province p(China,Beijing,1.36e+010,guang dong,1.05e+009); p.display(); return 0;2定義一個(gè)基類Person類,有姓名、性別、年齡,再由基類派生出學(xué)生類Student類和教師類Teacher類,學(xué)生類增加學(xué)號、班
13、級、專業(yè)和入學(xué)成績,教師類增加工號、職稱和工資?!境绦騾⒖即a】#include #include using namespace std;class Person /聲明公共基類Personpublic: void set() cin namesexage; /姓名、性別、年齡的輸入void display() coutname=nametsex=sextage=stuIdstuClassprofessionscore; /學(xué)號、班級、專業(yè)、入學(xué)成績的輸入 void display() Person:display(); /姓名、性別、年齡的顯示couttstuId=stuIdtstuCla
14、ss=stuClasstprofession =professiontscore=scoreendl teachId title wage; /工號、職稱、工資的輸入void display() Person:display(); /姓名、性別、年齡的顯示couttteachId=teachIdttitle=titletwage=wageendl; /工號、職稱、工資的顯示private: string teachId; /工號string title; /職稱 float wage; /工資;int main( )Student student;Teacher teacher; coutPl
15、ease enter students name,sex,age,stuId,stuClass,profession and score:endl; student.input(); coutDisplay students name,sex,age,stuId,stuClass,profession and score:endl; student.display();coutPlease enter teachers name,sex,age,teachId, title and wage:endl; teacher.set(); coutDisplay teachers name,sex,
16、age,teachId, title and wage:endl; teacher.display(); return 0;3設(shè)計(jì)一個(gè)基類Building類,有樓房的層數(shù)、房間數(shù)和總面積,再由基類派生出教學(xué)樓TeachBuilding類和宿舍樓類DormBuilding類,教學(xué)樓類增加教室數(shù),宿舍樓類增加宿舍數(shù)、容納學(xué)生總?cè)藬?shù)?!境绦騾⒖即a】#include using namespace std;class Building public: Building(int f,int r,double a) floors=f; rooms=r; area=a; protected: int fl
17、oors;int rooms;double area;class TeachBuilding:public Buildingpublic: TeachBuilding (int f,int r,double a,int cr):Building(f,r,a) classrooms=cr; void show() coutfloors=floors rooms=rooms area=area classrooms=classroomsendlendl; private: int classrooms;class DormBuilding:public Building public: DormB
18、uilding (int f,int r,double a,int d,int sc) :Building(f,r,a) dormitories =d; studcount =sc; void show() coutfloors=floors rooms=rooms area=area dormitories=dormitories studcount=studcountendlendl; private: int dormitories; int studcount; ;int main() TeachBuilding TB(6, 80,200,35); TB.show(); DormBui
19、lding DB(6, 80,200,35,300); DB.show(); return 0;4定義一個(gè)基類汽車類,有型號、顏色、發(fā)動(dòng)機(jī)功率、車速、重量、車牌號碼,再由汽車類派生出客車類和貨車類,客車類增加客車座位數(shù)、客運(yùn)公司,貨車類增加載貨重量、貨運(yùn)公司?!境绦騾⒖即a】#include #include using namespace std;class Carpublic:void show();protected: string model; /型號 int color; /顏色 unsigned int motopower; /發(fā)動(dòng)機(jī)功率 unsigned int speed; /
20、車速 unsigned int weight; /重量 unsigned int id; /車牌號碼;class Bus: public Carpublic: void show();Bus( string _model, int _color, unsigned int _motopower, unsigned int _speed,unsigned int _weight, unsigned int _id, unsigned int _seatnum, string _corp) model = _model; color = _color; motopower = _motopower
21、; speed = _speed;weight = _weight; id = id; _seatnum = _seatnum; corp = _corp; private: unsigned int seatnum; /客車座位數(shù) string corp; /客運(yùn)公司;class Truck : public Carpublic: void show(); Truck(string _model, int _color, unsigned int _motopower, unsigned int _speed, unsigned int _weight, unsigned int _id,u
22、nsigned int _load, string _corp) model = _model; color = _color; motopower = _motopower; speed = _speed; weight = _weight; id = _id; load = _load; corp = _corp; private: unsigned int load; /載貨重量string corp; /貨運(yùn)公司;void Car:show()cout型號:modelendl;cout顏色:colorendl; cout發(fā)動(dòng)機(jī)功率:motopowerendl; cout車速:speed
23、endl; cout重量:weightendl; cout車牌號碼:idendl;void Bus:show()cout型號:modelendl; cout顏色:colorendl; cout發(fā)動(dòng)機(jī)功率:motopowerendl; cout車速:speedendl; cout重量:weightendl; cout車牌號碼:idendl; cout客車座位數(shù):seatnumendl; cout客運(yùn)公司:corpendl;void Truck:show() cout型號:modelendl;cout顏色:colorendl;cout發(fā)動(dòng)機(jī)功率:motopowerendl;cout車速:speed
24、endl; cout重量:weightendl;cout車牌號碼:idendl;cout載貨重量:loadendl;cout貨運(yùn)公司:corpendl;int main( int argc, char* argv ) Car *a = new Bus(黃海,3,300,100,2000,10101010,50,北京客運(yùn));Car *b = new Truck(東風(fēng),1,400,200,5000,10101011,3000,北京貨運(yùn));cout=show();cout=show();system(pause);return 0;5定義一個(gè)Table類和Circle類,再由它們共同派生出Round
25、Table類?!境绦騾⒖即a】#includeusing namespace std;#includeclass Circlepublic:Circle(double r)radius=r;double getArea()return 3.1416*radius*radius;private:double radius;class Tablepublic:Table(double h)height=h;double getHeight()return height;private:double height;class RoundTable:public Table,public Circle
26、public:RoundTable(double h,double r,char c):Table(h),Circle(r)color=new charstrlen(c) +1;strcpy(color,c);char* getColor()return color;private: char *color;int main()RoundTable rt(0.8,1.0,白色);cout圓桌的高:rt. getHeight()endl;cout圓桌面積:rt.getArea()endl;cout圓桌顏色:rt.getColor()endl;return 0;6在第4題的基礎(chǔ)上,由客車類和貨車類
27、再派生出一個(gè)客貨兩用車類,一輛客貨兩用車既具有客車的特征(有座位,可以載客),又具有貨車的特征(有裝載車廂,可以載貨)。要求將客貨兩用車類的間接共同基類汽車類聲明為虛基類?!境绦騾⒖即a】#include #include using namespace std;class Carpublic:Car(string _model, int _color, unsigned int _motopower, unsigned int _speed,unsigned int _weight, unsigned int _id) model = _model; color = _color; moto
28、power = _motopower; speed = _speed; weight = _weight; id = _id; void show();private: string model; int color; unsigned int motopower; unsigned int speed; unsigned int weight; unsigned int id;class Bus: virtual public Carpublic: Bus( string model, int color, unsigned int motopower, unsigned int speed
29、,unsigned int weight, unsigned int id, unsigned int _seatnum,string _corp ): Car(model,color, motopower,speed,weight,id) seatnum = _seatnum; corp = _corp; void display();protected: unsigned int seatnum; string corp;class Wagon : virtual public Carpublic: Wagon( string model, int color, unsigned int
30、motopower, unsigned int speed,unsigned int weight,unsigned int id, unsigned int _load,string _corp ): Car(model,color, motopower,speed,weight,id) load = _load; corp = _corp; void display();protected: unsigned int load; string corp;class StationWagon : public Bus,public Wagonpublic: StationWagon( str
31、ing model, int color, unsigned int motopower, unsigned int speed,unsigned int weight,unsigned int id,unsigned int seatnum,unsigned int load, string corp ): Car(model,color, motopower,speed,weight,id),Bus(model,color, motopower,speed,weight,id,seatnum,corp),Wagon(model,color, motopower,speed,weight,i
32、d,load,corp) void display();void Car:show()cout型號:modelendl;cout顏色:colorendl; cout發(fā)動(dòng)機(jī)功率:motopowerendl; cout車速:speedendl; cout重量:weightendl; cout車牌號碼:idendl; void Bus:display()show(); cout客車座位數(shù):seatnumendl; cout客運(yùn)公司:corpendl;void Wagon:display() show();cout載貨重量:loadendl;cout貨運(yùn)公司:corpendl;void Station
33、Wagon:display() show();cout客車座位數(shù):seatnumendl;cout載貨重量:loadendl;cout客運(yùn)及貨運(yùn)公司:Bus:corpendl;int main( ) StationWagon sw(黃海,3,300,100,2000,10101010,50,3000,北京客貨運(yùn));sw.display();system(pause);return 0;7設(shè)計(jì)一個(gè)雇員類Employee,存儲(chǔ)雇員的編號、姓名和生日等信息,要求該類使用日期類作為成員對象,雇員類的使用如下:/定義一個(gè)雇員,其雇員號為1111,姓名為Tom,生日為1980年11月20日 Employe
34、e Tom(1111, Tom, 1980, 11, 20)Date today(1980, 11, 20); if (Tom.isBirthday(today) /判斷今天是否為Tom的生日 /【程序參考代碼】#include #include using namespace std;class Datepublic:Date() year=0; month=0; day=0; /無參構(gòu)造函數(shù)Date(int mm, int dd, int yy) year=yy; month=mm; day=dd; /帶參數(shù)的構(gòu)造函數(shù)Date(const Date &d) year=d.year; mon
35、th=d.month; day=d.day; /復(fù)制構(gòu)造函數(shù)void setDate(const int, const int, const int); /修改日期的函數(shù)void display(); /輸出日期的函數(shù) int getYear(); /獲取年份的函數(shù)int getMonth(); /獲取月份的函數(shù) int getDay(); /獲取日信息的函數(shù)private:int year, month, day;/設(shè)置成員變量,mm:月份;dd:天數(shù);yy:年份;void Date:setDate(const int mm, const int dd, const int yy)year=yy; month=mm; day=dd; void Date:display() coutmo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年三方分包施工合同(三篇)
- 2025年度金融科技股權(quán)眾籌投資合同范本
- 二零二四年汽車指標(biāo)租賃與車輛租賃保險(xiǎn)合同6篇
- 2025年度智能家居系統(tǒng)供應(yīng)商服務(wù)合同范本
- 住宅房產(chǎn)交易正式合同(2024版)一
- 2025年上海市個(gè)人簡易房屋租賃合同樣本(2篇)
- 2025版高層住宅電梯安裝居間代理服務(wù)合同4篇
- 2025年度全球貨物保險(xiǎn)理賠服務(wù)合同
- 2025年度智能穿戴設(shè)備銷售與服務(wù)合同(定制版)
- 2025年果樹種植基地土壤改良與施肥服務(wù)合同
- 2025年N1叉車司機(jī)考試試題(附答案)
- 《醫(yī)院財(cái)務(wù)分析報(bào)告》課件
- 2024年考研政治試題及答案
- 2025年初級社會(huì)工作者綜合能力全國考試題庫(含答案)
- 2022-2023學(xué)年五年級數(shù)學(xué)春季開學(xué)摸底考(四)蘇教版
- 【螞蟻?!?024中國商業(yè)醫(yī)療險(xiǎn)發(fā)展研究藍(lán)皮書
- 授信審批部工作計(jì)劃及思路
- 建筑工程質(zhì)量、安全與進(jìn)度管控
- ASME B16.5-16.47法蘭尺寸對照表
- 對外漢語詞匯教學(xué)(第二版)PPT完整全套教學(xué)課件
- 產(chǎn)品報(bào)價(jià)單(5篇)
評論
0/150
提交評論