東北大學(xué)C++實(shí)驗(yàn)報(bào)告.doc_第1頁(yè)
東北大學(xué)C++實(shí)驗(yàn)報(bào)告.doc_第2頁(yè)
東北大學(xué)C++實(shí)驗(yàn)報(bào)告.doc_第3頁(yè)
東北大學(xué)C++實(shí)驗(yàn)報(bào)告.doc_第4頁(yè)
東北大學(xué)C++實(shí)驗(yàn)報(bào)告.doc_第5頁(yè)
已閱讀5頁(yè),還剩3頁(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)要求(1)定義 Point 類,有坐標(biāo) _x,_y 兩個(gè)成員變量;對(duì) Point 類重載“” ( 自增 ) 、“” ( 自減 ) 運(yùn)算符,實(shí)現(xiàn)對(duì)坐標(biāo)值的改變。(2)定義一個(gè)車(chē) (vehiele) 基類,有 Run、Stop 等成員函數(shù),由此派生出自行車(chē) (bicycle) 類、汽車(chē) (motorcar) 類,從 bicycle 和 motorcar 派生出摩托車(chē) (motorcycle) 類,它們都有 Run、 Stop 等成員函數(shù)。觀察虛函數(shù)的作用。2. 實(shí)驗(yàn)內(nèi)容及實(shí)驗(yàn)步驟(1) 編寫(xiě)程序定義 Point 類,在類中定義整型的私有成員變量 _x_y,定義成員函數(shù) Point&

2、 operator+();Point operator+(int);以實(shí)現(xiàn)對(duì)Point類重載“ +”( 自增 ) 運(yùn)算符,定義成員函數(shù)Point operator () ; Pointoperator (int) ;以實(shí)現(xiàn)對(duì) Point 類重載“” ( 自減 ) 運(yùn)算符,實(shí)現(xiàn)對(duì)坐標(biāo)值的改變。程序名: 1ab8_1 cpp。(2) 編寫(xiě)程序定義一個(gè)車(chē) (vehicle) 基類,有 Run、Stop 等成員函數(shù),由此派生出自行車(chē) (bicycle)類、汽車(chē) (motorcar)類,從 bicycle和 motorcar 派生出摩托車(chē) (motorcycle) 類,它們都有 Run、 Stop 等成

3、員函數(shù)。在 main() 函數(shù)中定義 vehicle 、bicycle 、motorcar 、motorcycle 的對(duì)象,調(diào)用其 Run() 、 Stop()函數(shù),觀察其執(zhí)行情況。 再分別用 vehicle類型的指針來(lái)調(diào)用這幾個(gè)對(duì)象的成員函數(shù),看看能否成功;把 Run、Stop 定義為虛函數(shù),再試試看。程序名:lab8_2 cpp。3. 源程序Lab8_1#includeusing namespace std;class Pointpublic:Point(int X,int Y): _x(X),_y(Y)Point operator+();Point operator+(int);Poin

4、t operator-();Point operator-(int);void Putout() const;private:int _x,_y;void Point:Putout() constcout(_x,_y)endl;Point Point: operator+()_x+;_y+;return *this;Point Point:operator+(int)+_x;+_y;return *this;Point Point:operator-()_x-;_y-;return *this;Point Point:operator-(int)-_x;-_y;return *this;int

5、 main()Point A(6,7);coutPoint+: ;(A+).Putout();cout+Point: ;(+A).Putout();coutPoint-: ;(A-).Putout();cout-Point: ;(-A).Putout();return 0;運(yùn)行結(jié)果Lab8_21)在 main() 函數(shù)中定義 vehicle 、bicycle 、motorcar 、motorcycle 的對(duì)象,調(diào)用其 Run() 、Stop() 函數(shù)。#includeusing namespace std;class Vehiclepublic:void Run()cout基類 Vehicle

6、 的 Run函數(shù)調(diào)用 endl;void Stop()cout基類 Vehicle 的 Stop 函數(shù)調(diào)用 endl;class bicycle :public Vehiclepublic:void Run()cout派生類bicycle的 Run函數(shù)調(diào)用 endl;void Stop()cout派生類bicycle的Stop函數(shù)調(diào)用 endl;class motorcar :public Vehiclepublic:void Run()cout派生類motorcar的 Run函數(shù)調(diào)用 endl;void Stop()cout派生類motorcar的Stop函數(shù)調(diào)用 endl;class mo

7、torcycle :public bicycle,public motorcarpublic:void Run()cout 派生類 motorcycle 的 Run函數(shù)調(diào)用 endl; void Stop()cout 派生類 motorcycle 的 Stop 函數(shù)調(diào)用 endl;int main()Vehicle V;bicycle B;motorcar M;motorcycle Mc;V.Run();V.Stop();B.Run();B.Stop();M.Run();M.Stop();Mc.Run();Mc.Stop();return 0;運(yùn)行結(jié)果2)用 vehicle類型的指針來(lái)調(diào)用這幾

8、個(gè)對(duì)象的成員函數(shù)。#includeusing namespace std;class Vehiclepublic:virtual void Run()cout基類 Vehicle 的 Run函數(shù)調(diào)用 endl;virtual void Stop()cout基類 Vehicle 的 Stop 函數(shù)調(diào)用 endl;class bicycle :virtual public Vehiclepublic:void Run()cout派生類bicycle的 Run函數(shù)調(diào)用 endl;void Stop()cout派生類bicycle的Stop函數(shù)調(diào)用 endl;class motorcar :virtu

9、al public Vehiclepublic:void Run()cout派生類motorcar的 Run函數(shù)調(diào)用 endl;void Stop()cout派生類motorcar的Stop函數(shù)調(diào)用 endl;class motorcycle :public bicycle,public motorcarpublic:void Run()cout 派生類 motorcycle 的 Run函數(shù)調(diào)用 endl; void Stop()cout 派生類 motorcycle 的 Stop 函數(shù)調(diào)用 Run();p-Stop();int main()Vehicle V;bicycle B;motorcar M;motorcycle Mc;fun(&

溫馨提示

  • 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)論