




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn)報(bào)告第8章多態(tài)性報(bào)告書(shū)名稱(chēng)實(shí)驗(yàn) 8-1多態(tài)性1姓名學(xué)號(hào)陳高雪120352010054指導(dǎo)教師日期樂(lè)仁昌任務(wù)清單場(chǎng)景聲明 Point 類(lèi),有坐標(biāo)_x , _y 兩個(gè)成員變量;對(duì)Point類(lèi)重載“+”(自增) 、“”(自減)運(yùn)算符,實(shí)現(xiàn)對(duì)坐標(biāo)值的改變。實(shí)驗(yàn)方法編寫(xiě)程序聲明Point 類(lèi),在類(lèi)中聲明整型的私有成員變量_x 、 _y ,聲明成員函數(shù)Point&operator + (); Point operator + ( int );以實(shí)現(xiàn)對(duì)Point 類(lèi)重載“ + ”(自增)運(yùn)算符,聲明成員函數(shù)Point &operator -(); Point operator - ( i
2、nt );以實(shí)現(xiàn)對(duì)Point 類(lèi)重載“” (自減)運(yùn)算符,實(shí)現(xiàn)對(duì)坐標(biāo)值的改變。程序名:lab8_1.cpp實(shí)驗(yàn)預(yù)估時(shí)間40 分鐘實(shí)驗(yàn)結(jié)果#include<iostream>using namespace std;class Pointpublic:Point(int x=0,int y=0);Point &operator +();Point operator +(int);Point &operator -();C+ 程序設(shè)計(jì)語(yǔ)言Point operator -(int);void disypaly();private:int _x,_y;Point:Point(
3、int x,int y)_x=x;_y=y;void Point:disypaly()cout<<"("<<_x<<","<<_y<<")"<<endl;Point &Point:operator +()_x+;_y+;return *this;Point Point:operator +(int)Point old=*this;+(*this);return old;Point &Point:operator -()2_x-;_y-;return
4、 *this;Point Point:operator -(int)Point old=*this;-(*this);return old;void main()Point point(15,20);cout<<"初始坐標(biāo) : "point.disypaly();cout<<"point+坐標(biāo):"(point+).disypaly();cout<<"+point坐標(biāo) :"(+point).disypaly();cout<<"Point-坐標(biāo) : "(point-).d
5、isypaly();cout<<"-Point坐標(biāo) : "(-point).disypaly();3C+ 程序設(shè)計(jì)語(yǔ)言報(bào)告書(shū)名稱(chēng)實(shí)驗(yàn) 8-2 多態(tài)性2姓名指導(dǎo)教師學(xué)號(hào)日期任務(wù)清單場(chǎng)景聲明一個(gè)車(chē) ( vehicle )基類(lèi), 具有 Run 、Stop 等成員函數(shù), 由此派生出自行車(chē) ( bicycle )類(lèi)、汽車(chē)( motorcar )類(lèi),從 bicycle 和 motorcar 派生出摩托車(chē)( motorcycle )類(lèi),它們都有 Run、 Stop 等成員函數(shù)。觀察虛函數(shù)的作用。實(shí)驗(yàn)方法編寫(xiě)程序聲明一個(gè)車(chē)(vehicle )基類(lèi),具有Run、 Stop 等成員
6、函數(shù),由此派生出自行車(chē)( bicycle )類(lèi)、汽車(chē)( motorcar )類(lèi),從 bicycle 和 motorcar 派生出摩托車(chē)( motorcycle )類(lèi),它們都有Run 、Stop 等成員函數(shù)。 在 main()函數(shù)中聲明vehicle 、bicycle 、motorcar 、motorcycle的對(duì)象,調(diào)用其Run()、Stop()函數(shù),觀察其執(zhí)行情況。再分別用vehicle類(lèi)型的指針來(lái)調(diào)用這幾個(gè)對(duì)象的成員函數(shù),看看能否成功;把Run ()、 Stop()聲明為虛函數(shù),再試試看。程序名:lab8_2.cpp實(shí)驗(yàn)預(yù)估時(shí)間35 分鐘實(shí)驗(yàn)結(jié)果#include<iostream&g
7、t;using namespace std;class vehiclepublic:virtualvoid Run()4cout << "vehicle Run:display()" << endl;virtualvoid Stop()cout << "vehicle Stop:display()" << endl<<endl;class bicycle: virtual public vehiclepublic:void Run()cout << "bicycle Run
8、:display()" << endl;void Stop()cout << "bicycle Stop:display()" << endl<<endl;class motorcar:virtual public vehiclepublic:void Run()cout << "motorcar Run:display()" << endl;void Stop()cout << "motorcar Stop:display()" <&
9、lt; endl<<endl;5C+ 程序設(shè)計(jì)語(yǔ)言class motorcycle: public bicycle,public motorcarpublic:void Run()cout << "motorcycle Run:display()" << endl;void Stop()cout << "motorcycle Stop:display()" << endl<<endl;void fun(vehicle *prt)prt->Run();prt->Stop()
10、;void main()vehicle vehicle1,*p;bicycle bicycle1;motorcar motorcar1;motorcycle motorcycle1;p=&vehicle1;fun(p);p=&bicycle1;fun(p);p=&motorcar1;fun(p);p=&motorcycle1;fun(p);6#include<iostream>using namespace std;class vehiclepublic:virtualvoid Run()cout << "vehicle Run
11、:display()" << endl;virtualvoid Stop()cout << "vehicle Stop:display()" << endl<<endl;class bicycle: virtual public vehiclepublic:void Run()7C+ 程序設(shè)計(jì)語(yǔ)言cout << "bicycle Run:display()" << endl;void Stop()cout << "bicycle Stop:displa
12、y()" << endl<<endl;class motorcar:virtual public vehiclepublic:void Run()cout << "motorcar Run:display()" << endl;void Stop()cout << "motorcar Stop:display()" << endl<<endl;class motorcycle: public bicycle,public motorcarpublic:void Run()cout << "motorcycle Run:display()" << endl;void Stop()cout << "motorcycle Stop:display()" << endl<<endl;8void main()vehicle ve
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 湘藝版音樂(lè)八年級(jí)上冊(cè)第一單元國(guó)家 教案
- 2025年編輯加工軟件合作協(xié)議書(shū)
- 2025年醫(yī)用化驗(yàn)設(shè)備器具合作協(xié)議書(shū)
- 中職新教材數(shù)學(xué)課件
- 企業(yè)高效運(yùn)營(yíng)的關(guān)鍵如何運(yùn)用智能和區(qū)塊鏈進(jìn)行管理升級(jí)
- 大數(shù)據(jù)時(shí)代的教育評(píng)估與反饋機(jī)制研究
- 醫(yī)療教育改革中的教師法律地位
- 中職數(shù)學(xué)排列講課課件
- 教育大數(shù)據(jù)分析學(xué)習(xí)效果顯著提升的秘訣
- 中職教育旅游者課件
- 中國(guó)新聞社招聘考試試卷及答案2022
- 腦血管病防治指南(2024年版)完整版
- 消化道穿孔護(hù)理
- TYCST 004-2024 透水水泥穩(wěn)定碎石基層 透水系數(shù)的測(cè)定
- 部門(mén)級(jí)安全培訓(xùn)試題加解析答案可打印
- 醫(yī)學(xué)教材 暴發(fā)性心肌炎
- 車(chē)間6S可視化管理之定置劃線標(biāo)準(zhǔn)解讀
- 小學(xué)英語(yǔ)三年級(jí)《My Family》說(shuō)課課件
- Odoo:Odoo集成與第三方應(yīng)用技術(shù)教程.Tex.header
- 2024年江西省“振興杯”地質(zhì)調(diào)查員競(jìng)賽考試題庫(kù)(含答案)
- 人教部編版九年級(jí)歷史上冊(cè):第14課 文藝復(fù)興運(yùn)動(dòng) 教學(xué)設(shè)計(jì)
評(píng)論
0/150
提交評(píng)論