C++上機(jī)實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)五_第1頁(yè)
C++上機(jī)實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)五_第2頁(yè)
C++上機(jī)實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)五_第3頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、實(shí)驗(yàn)五繼承與派生1. 實(shí)驗(yàn)?zāi)康?. 學(xué)習(xí)定義和使用類的繼承關(guān)系,定義派生類2. 熟悉不同繼承方式下對(duì)基類成員的訪問(wèn)控制3. 學(xué)習(xí)利用虛基類解決二義性問(wèn)題2. 實(shí)驗(yàn)要求1. 定義一個(gè)基類 Animal,有私有整型成員變量 age,構(gòu)造其派生類dog,在其成員函數(shù)SetAge(int n )中直接給age賦值,看看會(huì)有什么問(wèn)題,把a(bǔ)ge改為公有成員變量,還會(huì)有問(wèn)題嗎?編程嘗試。2. 定義一個(gè)基類 BaseClass,有整型變量 Number,構(gòu)造其派生類 DerivedClass,觀察構(gòu)造函 數(shù)和析構(gòu)函數(shù)的執(zhí)行情況。3. 定義一個(gè)車(vehicle )基類,具有 Maxspeed、Weight等成

2、員,Run Stop等成員函數(shù), 由此派生出自行車 (bicycle )類、汽車(motorcar )類。自行車(bicycle )類有高度(Height) 等屬性,汽車(motorcar )類有座位數(shù)(SeatNun)等屬性。從 bicycle 和motorcar派生出 摩托車(motorbicycle )類,在繼承過(guò)程中,注意把vehicle 設(shè)置為虛基類。如果不把vehicle 設(shè)置為虛基類,會(huì)有什么問(wèn)題?編程嘗試。3. 實(shí)驗(yàn)容及實(shí)驗(yàn)步驟1. 編寫(xiě)程序定義基類 Animal,成員變量age定義為私有的。構(gòu)造派生類dog,在其成員函數(shù) SetAge ( intn )中直接對(duì)age賦值時(shí),會(huì)

3、出現(xiàn)類似以下的錯(cuò)誤提示:error C2248 : age' : cannot access private member declared in class Animal 'error C2248 : age' : cannot access private member declared in class Animal '把a(bǔ)ge改為公有成員變量后重新編譯就可以了。程序名:Iab7_1.cpp。2. 編寫(xiě)程序定義一個(gè)基類BaseClass,構(gòu)造其派生類 DerivedClass ,在構(gòu)造函數(shù)和析構(gòu)函數(shù)中用cout輸出提示信息,觀察構(gòu)造函數(shù)和析構(gòu)函數(shù)的執(zhí)行情況

4、。程序名:Iab7_2.cpp。3. 用debug功能跟蹤程序Iab7_2的執(zhí)行過(guò)程,觀察基類和派生類的構(gòu)造函數(shù)和析構(gòu)函數(shù)的 執(zhí)行情況。4. 編寫(xiě)程序定義一個(gè)車 (vehicle )基類,由此派生出自行車 (bicycle )類、汽車(motorcar ) 類,注意把vehicle 派生為虛基類。再?gòu)腷icycle 和motorcar派生出摩托車 (motorcycle ) 類,在main()函數(shù)中測(cè)試這個(gè)類。程序名 :lab7_3.cpp 。編譯成功后,把vehicle設(shè)置為非虛基類,再編譯一次,此時(shí)系統(tǒng)報(bào)錯(cuò),無(wú)法編譯成功。這 是因?yàn)槿舨话裿ehicle設(shè)置為虛基類,會(huì)出現(xiàn)二義性錯(cuò)誤,程序不

5、能成功編譯。4. 思考題1. 如何在已有的類的基礎(chǔ)上設(shè)計(jì)新的類?采用類的派生的方法,利用基類派生出子類,子類繼承基類的屬性,從而在已有基類的基礎(chǔ)上設(shè)計(jì)新的派生類,模式如下:class派生類名:繼承方式基類名1,繼承方式基類名 2,,繼承方式基類名n派生類成員聲明;;2. 基類和派生類對(duì)象的構(gòu)造順序是怎樣的?先調(diào)用基類的構(gòu)造函數(shù), 然后再調(diào)用嵌對(duì)象(派生類中的對(duì)象)的構(gòu)造函數(shù)。基類構(gòu)造函數(shù) 的調(diào)用順序是按照派生類定義時(shí)的順序,而嵌對(duì)象的構(gòu)造函數(shù)調(diào)用順序是按照成員在類中聲明的順序。3. 如何利用虛基類解決二義性問(wèn)題? 將共同基類設(shè)置為虛基類,語(yǔ)法為:class派生類名:virtual繼承方式基類名

6、5. 源程序Lab7_1.cpp#in clude<iostream>using n amespace std;class Ani mal/A nimal類/*private:int age;*/public:int age;An imal()An imal()An imal(i nt );class Dog:public Animal/Dog類,Animalprivate:int nu mber;public:Dog()Dog()Dog(i nt,i nt);voidsetAge(i nt );voidshowNumber();voidshowAge();Ani mal:A ni

7、mal(i nt a)/An imal構(gòu)造函數(shù)age=a;Dog:Dog(i ntn ,i nt a)/Dog構(gòu)造函數(shù)nu mber =n;age=a;void Dog:setAge( int n)/Dog成員函數(shù),age=n;的公有派生類setAge/數(shù)據(jù)輸出函數(shù)void Dog:showNumber()cout<<"Number:"< <nu mber<<e ndl;void Dog:showAge()cout<<"Age:"<<age<<e ndl;int mai n()Dog

8、 dog1(12,0);dog1.setAge(7);dog1.showNumber(); dog1.showAge();return 0;Lab_2#in clude<iostream>using n amespace std;classBaseClassprivate:int grade;int populati on;public:BaseClass( in tg,i nt p)/BaseClass的構(gòu)造函數(shù)grade=g;populati on=p;cout<<"c on struct ing BaseClass"<<e ndl;

9、BaseClass() 析構(gòu) cout<<"destruct ing BaseClass"<<e ndl;classDerivedClass:publicBaseClassprivate:char n ame;的構(gòu)造函數(shù)public:DerivedClass(i ntg,i ntp,char n ):BaseClass(g,p)/DerivedClass n ame=n;cout<<"c on struct ing DerivedClass"<<e ndl;DerivedClass()cout<<

10、;"destruct ing DerivedClass"<<e ndl;int mai n()DerivedClassa(7,55,'A');return 0;Lab_3#in clude<iostream>using n amespace std;class Vehiclepublic:floatmaxspeed;float weight;Vehicle()Vehicle(float m,float w)maxspeed=m;weight=w;Vehicle()void run()cout<<"Vehicle

11、runnin g"<<e ndl;void stop()cout<<"Vehicle stopp in g"<<e ndl;classBicycle:virtual public Vehiclepublic:float height;Bicycle(float m,floatw,float h):Vehicle(m,w)height=h;Bicycle(float h)height=h;Bicycle();classMotorcar:virtual public Vehiclepublic:in tseat num;Motorc

12、ar(float m,floatw,i nt s):Vehicle(m,w)seat num=s;Motorcar()Motorcar(float s)seat num=s;classMotorbicycle:publicBicycle,public Motorcarpublic:Motorbicycle(floatm,floatw,floath,i nts):Vehicle(m,w),Bicycle(h),Motorcar(s)voidshowI nformatio n()cout<<"The maxspeed of this motorbicycle is:"

13、;<<maxspeed<<e ndl;cout<<"The weight of this motorbicycle is:"<<weight<<e ndl;cout<<"The height of this motorbicycle is:"<<height<<e ndl;cout<<"The seat nu mber of this motorbicycle is:"<<seat num <<e ndl

14、;int mai n()Motorbicyclea(60,21.5,45.8,2);a.ru n();a.showI nformatio n();a.stop();return 0;6. 運(yùn)行結(jié)果Lab7_1age為Animal的私有成員數(shù)據(jù)時(shí):編譯報(bào)錯(cuò): see declaration of 'agp-: error- C2Z48:1 age' z cdnnu t 日匚 cess private nienber declr'ccl in class * Aiiintdl.cpp6) : see declaration of 'age':error C2

15、248 1' : cannot access private menber declared in class 1Anirtjl .cppC6 : see declaration of 'age'age為Animal的公有成員數(shù)據(jù)時(shí):正常運(yùn)行Lab_2亡叫 *C : DociueTits and SettingsAconstructing BascClass constructing DepiuedClass cl e struct in g Deriue dC las s de右truct ing BaseClciss Press anv key "to conlzxnixeLab_3"C: XI> ocuAent s and Sett inf sAd* ini st ratorXJMXDebufeVlabT-. Uehic Le runn ingThe m&xspeed cf ch is nDto>*bic vcle isThe veight of this nDtorbicpcLe is:21.5I lie heof t

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 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ì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論