C++繼承與派生12頁(yè)_第1頁(yè)
C++繼承與派生12頁(yè)_第2頁(yè)
C++繼承與派生12頁(yè)_第3頁(yè)
C++繼承與派生12頁(yè)_第4頁(yè)
C++繼承與派生12頁(yè)_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、第4章 繼承與派生一、簡(jiǎn)答題1. 有以下程序結(jié)構(gòu),請(qǐng)分析訪問(wèn)屬性。class A /A為基類(lèi)public: void func1( ); int i;protected: void func2( ); int j;private:int k; ;class B: public A /B為A的公用派生類(lèi)public : void func3( ) ;protected: int m;private :int n;class C: public B / C為B的公用派生類(lèi)public:void func4( );private:int p;int main( ) A a; /a是基類(lèi)A的對(duì)象 B

2、b; /b是派生類(lèi)B的對(duì)象C c; /c是派生類(lèi)C的對(duì)象return 0; 問(wèn):(1)在main函數(shù)中能否用b.i,b.j 和b.k 訪問(wèn)派生類(lèi)B對(duì)象b中基類(lèi)A的成員?(2)派生類(lèi)B中的成員函數(shù)能否調(diào)用基類(lèi)A中的成員函數(shù)func1和func2?(3)派生類(lèi)B中的成員函數(shù)能否訪問(wèn)基類(lèi)A中的數(shù)據(jù)成員i,j,k?(4)能否在main函數(shù)中用c.i,c.j,c.k,c.m,c.n,c.p訪問(wèn)基類(lèi)A的成員i,j,k,派生類(lèi)B的成員m,n,以及派生類(lèi)C的成員p?(5)能否在main函數(shù)中用c. func1( ),c. func2( ),c. func3( )和c. func4( )調(diào)用func1,func

3、2,func3,func4成員函數(shù)?(6)派生類(lèi)C的成員函數(shù)func4能否調(diào)用基類(lèi)A中的成員函數(shù)func1,func2和派生類(lèi)B中的成員函數(shù)func3?【答案要點(diǎn)】各成員在各類(lèi)的范圍內(nèi)的訪問(wèn)權(quán)限如下表:類(lèi)的范圍func1ifunc2jkfunc3mnfunc4p基類(lèi)A公用公用保護(hù)保護(hù)私有公用派生類(lèi)B公用公用保護(hù)保護(hù)不可訪問(wèn)公用保護(hù)私有公用派生類(lèi)C公用公用保護(hù)保護(hù)不可訪問(wèn)公用保護(hù)不可訪問(wèn)公用私有(1)在main 函數(shù)中能用b.i訪問(wèn)派生類(lèi)B對(duì)象b中基類(lèi)A的成員i,因?yàn)樗谂缮?lèi)B中是公用數(shù)據(jù)成員。不能用b.j訪問(wèn)派生類(lèi)B對(duì)象b中基類(lèi)A的成員j,因?yàn)樗谂缮?lèi)B中是保護(hù)數(shù)據(jù)成員,不能被類(lèi)外訪問(wèn)。不

4、能用b.k訪問(wèn)派生類(lèi)B對(duì)象b中基類(lèi)A的成員k,因?yàn)樗腔?lèi)A的私用數(shù)據(jù)成員,只有基類(lèi)A的成員函數(shù)可以訪問(wèn),不能被類(lèi)外訪問(wèn)。(2)派生類(lèi)B中的成員函數(shù)能調(diào)用基類(lèi)A中的成員函數(shù)func1和func2,因?yàn)閒unc1、func2在派生類(lèi)B中是公用成員和保護(hù)成員,可以被派生類(lèi)的成員函數(shù)訪問(wèn)。(3)派生類(lèi)B中的成員函數(shù)能訪問(wèn)基類(lèi)A中的數(shù)據(jù)成員i、j,因?yàn)閕、j在派生類(lèi)B中是公用成員和保護(hù)成員,可以被派生類(lèi)的成員函數(shù)訪問(wèn)。派生類(lèi)B中的成員函數(shù)不能訪問(wèn)基類(lèi)A中的數(shù)據(jù)成員k,因?yàn)樗谂缮?lèi)B中是不可訪問(wèn)的成員。(4)能在main 函數(shù)中用c.i訪問(wèn)基類(lèi)A的成員i,不能用c.j、c.k訪問(wèn)基類(lèi)A的成員j、k,因

5、為它們?cè)谂缮?lèi)C中是保護(hù)成員和私有成員,不能被類(lèi)外訪問(wèn)。也不能用c.m、c.n訪問(wèn)派生類(lèi)B的成員m、n,因?yàn)樗鼈冊(cè)谂缮?lèi)C中也是保護(hù)成員和私有成員,不能被類(lèi)外訪問(wèn)。也不能用c.p訪問(wèn)派生類(lèi)C中的私用成員p。(5)能在main函數(shù)中用c. func1()、c. func3()和c. func4()調(diào)用func1、func3、func4成員函數(shù),因?yàn)樗鼈冊(cè)谂缮?lèi)C中是公用成員函數(shù),可以在類(lèi)外被訪問(wèn)。不能在main函數(shù)中用c. func2()調(diào)用func2成員函數(shù),因?yàn)樗谂缮?lèi)C中是保護(hù)成員函數(shù),不能在類(lèi)外被訪問(wèn)。(6)派生類(lèi)C的成員函數(shù)func4能調(diào)用基類(lèi)A中的成員函數(shù)func1、func2和派

6、生類(lèi)中的成員函數(shù)func3,因?yàn)閒unc1、func3在派生類(lèi)C中是公用成員函數(shù),func2在派生類(lèi)C中是保護(hù)成員函數(shù),都可以被派生類(lèi)C的成員函數(shù)調(diào)用。2. 已給商品類(lèi)及其多層的派生類(lèi)。以商品類(lèi)為基類(lèi)。第一層派生出服裝類(lèi)、家電類(lèi)、車(chē)輛類(lèi)。第二層派生出襯衣類(lèi)、外衣類(lèi)、帽子類(lèi)、鞋子類(lèi);空調(diào)類(lèi)、電視類(lèi)、音響類(lèi);自行車(chē)類(lèi)、轎車(chē)類(lèi)、摩托車(chē)類(lèi)。請(qǐng)給出商品類(lèi)及其多層派生類(lèi)的基本屬性和派生過(guò)程中增加的屬性。【答案要點(diǎn)】按題意沒(méi)有操作,所以只列出各個(gè)類(lèi)的數(shù)據(jù)成員,也不再在main函數(shù)中對(duì)各類(lèi)進(jìn)行測(cè)試。#includeusing namespace std;class Commoditydouble price;

7、 /價(jià)格char name20; /商品名char manufacturer20; /生產(chǎn)廠家int items; /數(shù)量; class Clothing: public Commodity /服裝類(lèi)char texture20; /材料質(zhì)地; class ElectricAppliance: public Commodity /家電類(lèi)enum Black,Whitetype; /黑白家電; class Vehicle: public Commodity /車(chē)輛類(lèi)int wheelNum; /車(chē)輪數(shù)量; class Shirt: public Clothing /襯衣類(lèi) enum Formal

8、,Casualstyle; /式樣:正式、休閑; class Garment: public Clothing /外衣類(lèi)enum Jacket,Coatstyle; /式樣:夾克、外套 ; class Hat: public Clothing /帽子類(lèi)enum Winter,Summer,Spring,Autumnstyle; /季節(jié)風(fēng)格; class Shoes: public Clothing /鞋子類(lèi)enum Winter,Summer,Spring,Autumnstyle; /季節(jié)風(fēng)格; class AirCondition: public ElectricAppliance /空調(diào)b

9、ool warmCool; /是否冷暖float power; /功率; class Television: public ElectricAppliance /電視類(lèi)int size; /尺寸 bool isColor; /是否彩色 ; class Acoustics: public ElectricAppliance /音響類(lèi)int speakerNum; /喇叭數(shù)目 float power; /功率 ; class Bicycle: public Vehicle /自行車(chē)類(lèi)int speedGrades; /調(diào)速級(jí)數(shù)int wheelSize; /輪子大小 ; class Car: pu

10、blic Vehicle /轎車(chē)類(lèi)float volume; /排氣量bool isSkyLight; /是否有天窗int boxNum; /廂數(shù); class Motorcycle: public Vehicle /摩托車(chē)類(lèi)float volume; /排氣量 ; int main() return 0; 二、編程題1定義一個(gè)國(guó)家基類(lèi)Country,包含國(guó)名、首都、人口等屬性,派生出省類(lèi)Province,增加省會(huì)城市、人口數(shù)量屬性?!境绦騾⒖即a】#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è)基類(lèi)Person類(lèi),有姓名、性別、年齡,再由基類(lèi)派生出學(xué)生類(lèi)Student類(lèi)和教師類(lèi)Teacher類(lèi),學(xué)生類(lèi)增加學(xué)號(hào)、班

13、級(jí)、專(zhuān)業(yè)和入學(xué)成績(jī),教師類(lèi)增加工號(hào)、職稱(chēng)和工資?!境绦騾⒖即a】#include #include using namespace std;class Person /聲明公共基類(lèi)Personpublic: void set() cin namesexage; /姓名、性別、年齡的輸入void display() coutname=nametsex=sextage=stuIdstuClassprofessionscore; /學(xué)號(hào)、班級(jí)、專(zhuān)業(yè)、入學(xué)成績(jī)的輸入 void display() Person:display(); /姓名、性別、年齡的顯示couttstuId=stuIdtstuCla

14、ss=stuClasstprofession =professiontscore=scoreendl teachId title wage; /工號(hào)、職稱(chēng)、工資的輸入void display() Person:display(); /姓名、性別、年齡的顯示couttteachId=teachIdttitle=titletwage=wageendl; /工號(hào)、職稱(chēng)、工資的顯示private: string teachId; /工號(hào)string title; /職稱(chēng) 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è)基類(lèi)Building類(lèi),有樓房的層數(shù)、房間數(shù)和總面積,再由基類(lèi)派生出教學(xué)樓TeachBuilding類(lèi)和宿舍樓類(lèi)DormBuilding類(lèi),教學(xué)樓類(lèi)增加教室數(shù),宿舍樓類(lèi)增加宿舍數(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è)基類(lèi)汽車(chē)類(lèi),有型號(hào)、顏色、發(fā)動(dòng)機(jī)功率、車(chē)速、重量、車(chē)牌號(hào)碼,再由汽車(chē)類(lèi)派生出客車(chē)類(lèi)和貨車(chē)類(lèi),客車(chē)類(lèi)增加客車(chē)座位數(shù)、客運(yùn)公司,貨車(chē)類(lèi)增加載貨重量、貨運(yùn)公司。【程序參考代碼】#include #include using namespace std;class Carpublic:void show();protected: string model; /型號(hào) int color; /顏色 unsigned int motopower; /發(fā)動(dòng)機(jī)功率 unsigned int speed; /

20、車(chē)速 unsigned int weight; /重量 unsigned int id; /車(chē)牌號(hào)碼;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; /客車(chē)座位數(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型號(hào):modelendl;cout顏色:colorendl; cout發(fā)動(dòng)機(jī)功率:motopowerendl; cout車(chē)速:speed

23、endl; cout重量:weightendl; cout車(chē)牌號(hào)碼:idendl;void Bus:show()cout型號(hào):modelendl; cout顏色:colorendl; cout發(fā)動(dòng)機(jī)功率:motopowerendl; cout車(chē)速:speedendl; cout重量:weightendl; cout車(chē)牌號(hào)碼:idendl; cout客車(chē)座位數(shù):seatnumendl; cout客運(yùn)公司:corpendl;void Truck:show() cout型號(hào):modelendl;cout顏色:colorendl;cout發(fā)動(dòng)機(jī)功率:motopowerendl;cout車(chē)速:speed

24、endl; cout重量:weightendl;cout車(chē)牌號(hào)碼: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類(lèi)和Circle類(lèi),再由它們共同派生出Round

25、Table類(lèi)?!境绦騾⒖即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ǔ)上,由客車(chē)類(lèi)和貨車(chē)類(lèi)

27、再派生出一個(gè)客貨兩用車(chē)類(lèi),一輛客貨兩用車(chē)既具有客車(chē)的特征(有座位,可以載客),又具有貨車(chē)的特征(有裝載車(chē)廂,可以載貨)。要求將客貨兩用車(chē)類(lèi)的間接共同基類(lèi)汽車(chē)類(lèi)聲明為虛基類(lèi)?!境绦騾⒖即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型號(hào):modelendl;cout顏色:colorendl; cout發(fā)動(dòng)機(jī)功率:motopowerendl; cout車(chē)速:speedendl; cout重量:weightendl; cout車(chē)牌號(hào)碼:idendl; void Bus:display()show(); cout客車(chē)座位數(shù):seatnumendl; cout客運(yùn)公司:corpendl;void Wagon:display() show();cout載貨重量:loadendl;cout貨運(yùn)公司:corpendl;void Station

33、Wagon:display() show();cout客車(chē)座位數(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è)雇員類(lèi)Employee,存儲(chǔ)雇員的編號(hào)、姓名和生日等信息,要求該類(lèi)使用日期類(lèi)作為成員對(duì)象,雇員類(lèi)的使用如下:/定義一個(gè)雇員,其雇員號(hào)為1111,姓名為T(mén)om,生日為1980年11月20日 Employe

34、e Tom(1111, Tom, 1980, 11, 20)Date today(1980, 11, 20); if (Tom.isBirthday(today) /判斷今天是否為T(mén)om的生日 /【程序參考代碼】#include #include using namespace std;class Datepublic:Date() year=0; month=0; day=0; /無(wú)參構(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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論