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

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、 實(shí)驗(yàn)六 實(shí)驗(yàn)?zāi)康?1.掌握運(yùn)算符重載的方法 2.學(xué)習(xí)使用虛函數(shù)實(shí)現(xiàn)動(dòng)態(tài)多態(tài)性 實(shí)驗(yàn)要求 1.定義Point類,有坐標(biāo)_x,_y兩個(gè)成員變量;對(duì)Point類重載“”(自增)、“”(自減)運(yùn)算符,實(shí)現(xiàn)對(duì)坐標(biāo)值的改變。 2.定義一個(gè)車(vehiele)基類,有Run、Stop等成員函數(shù),由此派生出自行車(bicycle)類、汽車(motorcar)類,從bicycle和motorcar派生出摩托車(motorcycle)類,它們都有Run、Stop等成員函數(shù)。觀察虛函數(shù)的作用。 3. (選做)對(duì)實(shí)驗(yàn)4中的People類重載“”運(yùn)算符和“”運(yùn)算符,“”運(yùn)算符判斷兩個(gè)people類對(duì)象的id屬性是否相

2、等;“”運(yùn)算符實(shí)現(xiàn)People類對(duì)象的賦值操作。 實(shí)驗(yàn)內(nèi)容及實(shí)驗(yàn)步驟 1.編寫程序定義Point類,在類中定義整型的私有成員變量_x_y,定義成員函數(shù)Point& operator+();Point operator+(int);以實(shí)現(xiàn)對(duì)Point類重載“+”(自增)運(yùn)算符,定義成員函數(shù)Point operator();Point operator(int);以實(shí)現(xiàn)對(duì)Point類重載“”(自減)運(yùn)算符,實(shí)現(xiàn)對(duì)坐標(biāo)值的改變。程序名:1ab8_1Cpp #include using namespace std; class Point public: Point(); Point(int x,in

3、t y); Point() /Point類析構(gòu)函數(shù) Point& operator+(); /公有成員函數(shù) Point operator+(int); Point&operator-(); Point operator-(int); void Show(); private: int _x; /私有數(shù)據(jù)成員 int _y; ; Point:Point() /Point類構(gòu)造函數(shù) _x=0;_y=0; Point:Point(int x,int y) /Point類構(gòu)造函數(shù) _x=x; _y=y; Point & Point:operator+() /重載后置+運(yùn)算符為Point類成員函數(shù) _x

4、+; _y+; Point Point:operator+(int) /重載前置+運(yùn)算符為Point類成員函數(shù) Point old=*this; +(this-_x); +(this-_y); return old; Point & Point:operator-() /重載后置-運(yùn)算符為Point類成員函數(shù) _x-; _y-; Point Point:operator-(int) /重載前置-運(yùn)算符為Point類成員函數(shù) Point old=*this; -(this-_x); -(this-_y); return old; void Point:Show() /輸出Point的坐標(biāo)值 co

5、ut_x,_y)endl; int main() Point a(2,3); /定義一個(gè)Point類對(duì)象a Point b=a+; /定義Point類對(duì)象b并用a+初始化b Point c=+a; /定義Point類對(duì)象c并用+a初始化c Point d=-a; /定義Point類對(duì)象d并用-a初始化d Point e=a-; /定義Point類對(duì)象e并用a-初始化e coutPoint a(; a.Show(); /輸出a的坐標(biāo) coutPoint b(; b.Show(); /輸出b的坐標(biāo) coutPoint c(; c.Show(); /輸出c的坐標(biāo) coutPoint d(; d.Sh

6、ow(); /輸出d的坐標(biāo) coutPoint e(; e.Show(); /輸出e的坐標(biāo) return 0; 運(yùn)行結(jié)果: 2編寫程序定義一個(gè)車(vehicle)基類,有Run、Stop等成員函數(shù),由此派生出自行車(bicycle)類、汽車(motorcar)類,從bicycle和motorcar派生出摩托車(motorcycle)類,它們都有Run、Stop等成員函數(shù)。在main()函數(shù)中定義vehicle、bicycle、motorcar、motorcycle的對(duì)象,調(diào)用其Run()、Stop()函數(shù),觀察其執(zhí)行情況。再分別用vehicle類型的指針來調(diào)用這幾個(gè)對(duì)象的成員函數(shù),看看能否成功

7、;把Run、Stop定義為虛函數(shù),再試試看。 程序名:lab8_2cpp #include using namespace std; class vehicle /基類vehicle public: vehicle() vehicle() virtual void Run()coutThe vehicle is running!endl; /定義虛函數(shù)Run() Stop() 定義虛函數(shù)/ virtual void Stop()coutThe vehicle has stopped!endl; ; bicycle,聲明基類為派生類的虛基類 class bicycle: virtual publ

8、ic vehicle /定義派生類 public: void Run()coutThe bicycle is running!endl; void Stop()coutThe bicycle has stopped!endl; bicycle() bicycle() ; ,聲明基類為派生類的虛基類 /定義派生類motorcarclass motorcar: virtual public vehicle public: void Run()coutThe motorcar is running!endl; void Stop()coutThe motorcar has stopped!endl;

9、 motorcar() motorcar() ; motorcycle class motorcycle:public bicycle,public motorcar /以bicycle類和motorcar類作為基類派生新類 public: void Run()coutThe motorcycle is running!endl; void Stop()coutThe motorcycle has stopped!Run(); /通過指針調(diào)用vehicle類成員函數(shù) p-Stop(); p=&b; /使指針p指向bicycle類對(duì)象b p-Run(); /通過指針調(diào)用bicycle類成員函數(shù)

10、p-Stop(); p=&c; /使指針p指向motorcar類對(duì)象c p-Run(); /通過指針調(diào)用 motorcar類成員函數(shù) p-Stop(); p=&d; /使指針指向motorbicycle類對(duì)象d p-Run(); /通過指針調(diào)用motorcycle類成員函數(shù) p-Stop(); return 0; 運(yùn)行結(jié)果: 3. (選做)對(duì)實(shí)驗(yàn)4中的People類重載“”運(yùn)算符和“”運(yùn)算符,“”運(yùn)算符判斷兩個(gè)people類對(duì)象的id屬性是否相等;“”運(yùn)算符實(shí)現(xiàn)People類對(duì)象的賦值操作。 源程序: #include #include using namespace std; class B

11、irthday public: Birthday(int Year,int Month,int Day); /構(gòu)造函數(shù) Birthday() /構(gòu)造函數(shù) Birthday() /析構(gòu)函數(shù) Birthday(Birthday &p); /復(fù)制構(gòu)造函數(shù) int showBirthday() ?潣瑵?敹牡?年?洼湯桴?月?搼祡?日; /內(nèi)聯(lián)成員函數(shù) int enter(); private: int year,month,day; ; Birthday:Birthday(Birthday &p) year=p.year; month=p.month; day=p.day; Birthday:Birt

12、hday(int Year,int Month,int Day) /Birthday類構(gòu)造函數(shù) year=Year; month=Month; day=Day; int Birthday:enter() ?潣瑵?生日:; cinyearmonthday; class people /定義people類 public: people() /people類構(gòu)造函數(shù) people() /people類析構(gòu)函數(shù) people(people &p); people operator=(people &); people operator-(people &); int show(); int enter

13、(); private: string number,id,sex; /字符串類型變量數(shù)據(jù)成員 類數(shù)據(jù)成員 Birthday p1; /Birthday; int people:show() ; sex 性別編號(hào) cout coutnumber; ; cout 生日調(diào)用Birthday類成員函數(shù) p1.showBirthday(); / idendl; coutsex; :; ?撓畯?尼編號(hào) cinnumber; :; 身份證號(hào)?撓畯?尼 cinid; 類復(fù)制構(gòu)造函數(shù) people:people(people &p):p1(p.p1) /people number=p.number; sex=

14、p.sex; id=p.id; 運(yùn)算符成員函數(shù)=重載people people:operator=(people &V) / if(id=V.id) cout have the same id! endl; else cout have different id!endl; people people:operator-(people &U) /重載-運(yùn)算符成員函數(shù) number=U.number; /使用字符串賦值運(yùn)算符 sex=U.sex; id=U.id; p1=U.p1; return *this; /返回this指針 int main() int t; people p2; /定義對(duì)

15、象數(shù)組p2 for(t=0;t2;t+) /輸入對(duì)象數(shù)組成員信息 潣瑵?輸入第?瓊?個(gè)人員的信息endl; pt.enter(); for(t=0;t2;t+) /輸出對(duì)象數(shù)組成員信息 cout第?瓊?個(gè)人員信息如下:endl; pt.show(); people p3; /定義people類的對(duì)象p3 p3-p1; /使用重載運(yùn)算符將p1賦給p3 cout第3個(gè)人員信息如下:endl; p3.show(); coutp0,p1; p0=p1; /使用重載運(yùn)算符-判斷p0和p1的id是否相等 return 0; 運(yùn)行結(jié)果: 思考題 1.如何將一個(gè)運(yùn)算符重載為類的成員函數(shù)? 一般語(yǔ)法形式: 返回類型 operator 運(yùn)算符(形參表) 函數(shù)體 函數(shù)的參數(shù)個(gè)數(shù)比原來的曹祖數(shù)個(gè)數(shù)要少一個(gè)(后置“+”,“-”除外)。 2.如何將一個(gè)運(yùn)算符重載為類的友元函數(shù)? 一般形式: friend 返回類型 operator 運(yùn)算符(形參表) 函數(shù)體 運(yùn)算所需的操作數(shù)都需要通過函數(shù)的形參表傳遞,在形參表中形參從左至右

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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)論