東北大學C++實驗報告_第1頁
東北大學C++實驗報告_第2頁
東北大學C++實驗報告_第3頁
東北大學C++實驗報告_第4頁
東北大學C++實驗報告_第5頁
免費預覽已結束,剩余1頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、.實驗六1. 實驗要求(1)定義 Point 類,有坐標 _x ,_y 兩個成員變量; 對 Point 類重載 “ ”(自增 )、“ ”(自減 )運算符,實現對坐標值的改變。(2)定義一個車 (vehiele)基類,有 Run、Stop 等成員函數,由此派生出自行車 (bicycle)類、汽車 (motorcar)類,從 bicycle 和 motorcar 派生出摩托車 (motorcycle)類,它們都有 Run、Stop 等成員函數。觀察虛函數的作用。2. 實驗內容及實驗步驟(1) 編寫程序定義 Point 類,在類中定義整型的私有成員變量 _x_y,定義成員函數 Point& oper

2、ator+();Point operator+(int);以實現對 Point 類重載 “+”(自增 )運算符,定義成員函數 Point operator ();Point operator (int) ;以實現對 Point 類重載“”(自減 )運算符,實現對坐標值的改變。程序名:1ab8_1cpp。(2) 編寫程序定義一個車 (vehicle)基類,有 Run、Stop 等成員函數,由此派生出自行車 (bicycle)類、汽車 (motorcar)類,從 bicycle 和 motorcar 派生出摩托車(motorcycle)類,它們都有 Run、Stop 等成員函數。 在 main()

3、函數中定義 vehicle、bicycle、motorcar、motorcycle 的對象,調用其 Run()、Stop()函數,觀察其執行情況。再分別用 vehicle 類型的指針來調用這幾個對象的成員函數,看看能否成功;把 Run、Stop 定義為虛函數,再試試看。程序名: 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);Point operator-();Poi

4、nt 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 main()Point A(6

5、,7);coutPoint+: ;(A+).Putout();cout+Point: ;(+A).Putout();coutPoint-: ;(A-).Putout();cout-Point: ;(-A).Putout();return 0;運行結果.Lab8_21)在 main()函數中定義 vehicle、bicycle、motorcar、motorcycle 的對象,調用其Run()、 Stop()函數。#includeusing namespace std;class Vehiclepublic:void Run()cout 基類 Vehicle 的 Run 函數調用 endl; vo

6、id Stop()cout 基類 Vehicle 的 Stop 函數調用 endl;class bicycle :public Vehiclepublic:void Run()cout 派生類 bicycle 的 Run 函數調用 endl; void Stop()cout 派生類 bicycle 的 Stop 函數調用 endl;class motorcar :public Vehiclepublic:void Run()cout 派生類 motorcar 的 Run 函數調用 endl; void Stop()cout 派生類 motorcar 的 Stop 函數調用 endl;class

7、 motorcycle :public bicycle,public motorcarpublic:void Run()cout 派生類 motorcycle 的 Run 函數調用 endl; void Stop()cout 派生類 motorcycle 的 Stop 函數調用 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;運行結果2)用 vehicle 類型的指

8、針來調用這幾個對象的成員函數。#includeusing namespace std;class Vehiclepublic:virtual void Run()cout 基類 Vehicle 的 Run 函數調用 endl; virtual void Stop()cout 基類 Vehicle 的 Stop 函數調用 endl;class bicycle :virtual public Vehiclepublic:void Run()cout 派生類 bicycle 的 Run 函數調用 endl; void Stop()cout 派生類 bicycle 的 Stop 函數調用 endl;c

9、lass motorcar :virtual public Vehiclepublic:void Run()cout 派生類 motorcar 的 Run 函數調用 endl; void Stop()cout 派生類 motorcar 的 Stop 函數調用 endl;class motorcycle :public bicycle,public motorcarpublic:void Run()cout 派生類 void Stop()coutRun();motorcycle 的 Run 函數調用 endl; motorcycle 的 Stop 函數調用 Stop();int main()Vehicle V;bicycle B;motorcar M;motorcycle Mc;fun(&V

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論