



下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、實驗六1 .實驗要求(1)定義Point類,有坐標(biāo)_x, _y兩個成員變量;對Point類重載 4+ ”自 增卜“一一”自減)運算符,實現(xiàn)對坐標(biāo)值的改變。(2)定義一個車(vehiele)基類,有Run、Stop等成員函數(shù),由此派生出自 行車(bicycle)類、汽車(motorcar)類,從 bicycle 和 motorcar 派生出摩托車 (motorcycle)類,它們都有Run、Stop等成員函數(shù)。觀察虛函數(shù)的作用。2 .實驗內(nèi)容及實驗步驟(1)編寫程序定義Point類,在類中定義整型的私有成員變量 _x_y,定義成 員函數(shù) Point& operator+() ; Poin
2、t operator+(int);以實現(xiàn)對 Point 類重載 “+”自(增)運算符,定義成員函數(shù) Point & operator(); Point operator(int);以實現(xiàn)對Point類重載(自減)運算符,實現(xiàn)對坐標(biāo)值的改變。程 序名:1ab8_1 . cpp。(2)編寫程序定義一個車(vehicle)基類,有Run、Stop等成員函數(shù),由此派 生出自行車(bicycle)類、汽車(motorcar)類,從bicycle和motorcar派生出摩托 車(motorcycle)類,它們都有 Run、Stop等成員函數(shù)。在 main()函數(shù)中定義 vehicle > b
3、icycle > motorcar > motorcycle 的對象,調(diào)用其 Run()、Stop()函數(shù), 觀察其執(zhí)行情況。再分別用vehicle類型的指針來調(diào)用這幾個對象的成員函數(shù), 看看能否成功;把Run、Stop定義為虛函數(shù),再t4試看。程序名:lab8_2 . cpp。3 .源程序Lab8_1#include<iostream>using namespace std; class Point public:Point(int X,int Y): _x(X),_y(Y)Point operator+();Point operator+(int);Point op
4、erator-。;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
5、:operator-()_x-;_y-;return *this;Point Point:operator-(int)-_x;-_y;return *this;int main()Point A(6,7);cout<<"Point+:"(A+).Putout();cout<<"+Point:"(+A).Putout();cout<<"Point-:"(A-).Putout();cout<<"-Point:"(-A).Putout();return 0;運行結(jié)果Lab8
6、_21)在 main()函數(shù)中定義 vehicle、bicycle、motorcar、motorcycle 的對象,調(diào) 用其 Run()、Stop()函數(shù)。#include<iostream>using namespace std;class Vehiclepublic:void Run()cout<<"基類 Vehicle 的 Run 函數(shù)調(diào)用"<<endl;void Stop()cout<<"基類 Vehicle 的 Stop 函數(shù)調(diào)用"<<endl;class bicycle :publi
7、c 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<<"派生類 moto
8、rcar 的 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)用"<<endl;int main()Vehicle V;bicycle B;motorcar M;motorcycle Mc;VRun();B.
9、Run();M.Run();VStop();B.Stop();M.Stop();Mc.Run(); Mc.Stop(); return 0;運行結(jié)果2)用vehicle類型一的指針來調(diào)用這幾個對象的成員函數(shù)。#include<iostream>using namespace std;class Vehiclepublic:virtual void Run()cout<<” 基類 Vehicle 的 Run 函數(shù)調(diào)用"<<endl;virtual void Stop()cout<<" 基類 Vehicle 的 Stop 函數(shù)調(diào)用
10、"<<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 :virtual public Vehiclepublic:void Run()cout<<"派生類 motorcar 的 Run 函
11、數(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)用"<<endl; ;void fun(Vehicle *p)p->Run();p->Stop();int main()Vehicle V;bicycle B;motorcar M;mot
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 湖北中醫(yī)藥大學(xué)《預(yù)防醫(yī)學(xué)綜合設(shè)計》2023-2024學(xué)年第二學(xué)期期末試卷
- 2025年重慶市安全員《A證》考試題庫
- 成都工業(yè)學(xué)院《數(shù)字電視節(jié)目編導(dǎo)與制作》2023-2024學(xué)年第二學(xué)期期末試卷
- 西寧城市職業(yè)技術(shù)學(xué)院《城市傳播》2023-2024學(xué)年第二學(xué)期期末試卷
- 上海交通大學(xué)《單片機原理及其應(yīng)用》2023-2024學(xué)年第二學(xué)期期末試卷
- 內(nèi)蒙古大學(xué)《材料化學(xué)與物理》2023-2024學(xué)年第二學(xué)期期末試卷
- 西安海棠職業(yè)學(xué)院《風(fēng)景園林制圖》2023-2024學(xué)年第二學(xué)期期末試卷
- 襄陽職業(yè)技術(shù)學(xué)院《設(shè)計基礎(chǔ)(1)》2023-2024學(xué)年第二學(xué)期期末試卷
- 河南藝術(shù)職業(yè)學(xué)院《形體基訓(xùn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 隴南師范高等??茖W(xué)?!渡锇踩c實驗室安全》2023-2024學(xué)年第二學(xué)期期末試卷
- QSB快速反應(yīng)看板
- 初中信息技術(shù)備課組工作計劃8篇
- 售后維修服務(wù)單模板
- (中職)電子技術(shù)基礎(chǔ)與技能(電子信息類)教案
- 汪小蘭有機化學(xué)課件(第四版)3
- 減少電力監(jiān)控系統(tǒng)告警信息上傳方法的研究(QC成果)
- 交易商協(xié)會非金融企業(yè)債務(wù)融資工具發(fā)行注冊工作介紹
- 《人與環(huán)境》課程教學(xué)大綱
- 班組長管理能力提升培訓(xùn)(PPT96張)課件
- 深圳市城市用地分類表
- 內(nèi)蒙古自治區(qū)小額貸款公司試點管理實施細則
評論
0/150
提交評論