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

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論