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è)
已閱讀5頁(yè),還剩6頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(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)康恼莆者\(yùn)算符重載的方法學(xué)習(xí)使用虛函數(shù)實(shí)現(xiàn)動(dòng)態(tài)多態(tài)性實(shí)驗(yàn)要求定義Point類,有坐標(biāo)_x,_y兩個(gè)成員變量;對(duì)Point類重載"+”(自增)、“”(自減)運(yùn)算符,實(shí)現(xiàn)對(duì)坐標(biāo)值而改變。1. 定義一個(gè)車(vehiele)基類,有RunStop等成員函數(shù),由此派生出自行車(bicycle)類、汽車(motorcar)類,從bicycle和motorcar派生出摩托車(motorcycle)類,它們都有RunStop等成員函數(shù)。觀察虛函數(shù)的作用。2. (選做)對(duì)實(shí)驗(yàn)4中的People類重載“=”運(yùn)算符和“”運(yùn)算符,“=”運(yùn)算符判斷兩個(gè)people類對(duì)象的id屆性是否相等;“”運(yùn)算符

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

3、intx,inty);Point()Point&operator+();Pointoperator+(int);Point&operator-();Pointoperator-(int);voidShow();private:int_x;int_y;Point:Point()_x=0;_y=0;Point:Point(intx,inty)/Point/Point/Point類析構(gòu)函數(shù)公有成員函數(shù)私有數(shù)據(jù)成員類構(gòu)造函數(shù)類構(gòu)造函數(shù)(_x=x;_y=y;Point&Point:operator+()/(_x+;_y+;PointPoint:operator+(int)/(Po

4、intold=*this;+(this->_x);+(this->_y);returnold;Point&Point:operator-()/(_x-;_y-;PointPoint:operator-(int)/(Pointold=*this;-(this->_x);-(this->_y);returnold;voidPoint:Show()/(cout<<_x<<”,"<<_y<<”)”<<endl;intmain()(Pointa(2,3);/Pointb=a+;/Pointc=+a;/P

5、ointd=-a;/Pointe=a-;/cout<<"Pointa("a.Show();/cout<<"Pointb("b.Show();/cout<<"Pointc("重載后置+運(yùn)算符為Point類成員函數(shù)重載前置+運(yùn)算符為Point類成員函數(shù)重載后置-運(yùn)算符為Point類成員函數(shù)重載前置-運(yùn)算符為Point類成員函數(shù)輸出Point的坐標(biāo)值定義一個(gè)Point類對(duì)象a定義Point類對(duì)象b并用a+初始化b定義Point類對(duì)象c并用+a初始化c定義Point類對(duì)象d并用-a初始化d定義Point類對(duì)

6、象e并用a-初始化e輸出a的坐標(biāo)輸出b的坐標(biāo)c.Show();/輸出c的坐標(biāo)cout<<"Pointd("d.Show();cout<<"Pointe("/輸出d的坐標(biāo)e.Show();return0;/輸出e的坐標(biāo)運(yùn)行結(jié)果:2編寫程序定義一個(gè)車(vehicle)基類,有Run、Stop等成員函數(shù),由此派生出自行車(bicycle)類、汽車(motorcar)類,從bicycle和motorcar派生出摩托車(motorcycle)類,它們都有Run、Stop等成員函數(shù)。在main()函數(shù)中定義vehicle、bicycle、mo

7、torcar、motorcycle的對(duì)象,調(diào)用其Run()、Stop()函數(shù),觀察其執(zhí)行情況。再分別用vehicle類型的指針來(lái)調(diào)用這幾個(gè)對(duì)象的成員函數(shù),看看能否成功;把Run、Stop定義為虛函數(shù),再試試看。程序名:lab8_2.cpp#include<iostream>usingnamespacestd;classvehicle/基類vehiclepublic:vehicle()vehicle()virtualvoidRun()cout<<"Thevehicleisrunning!"<<endl;/定義虛函數(shù)Run()virtualv

8、oidStop()cout<<"Thevehiclehasstopped!"<<endl;/定義虛函數(shù)Stop();classbicycle:virtualpublicvehicle/定義派生類bicycle,聲明基類為派生類的虛基類public:voidRun()cout<<"Thebicycleisrunning!"<<endl;voidStop()cout<<"Thebicyclehasstopped!"<<endl;bicycle()bicycle();c

9、lassmotorcar:virtualpublicvehicle/定義派生類motorcar,聲明基類為派生類的虛基類public:voidRun()cout<<"Themotorcarisrunning!"<<endl;voidStop()cout<<"Themotorcarhasstopped!"<<endl;motorcar()motorcar();classmotorcycle:publicbicycle,publicmotorcar/以bicycle類和motorcar類作為基類派生新類moto

10、rcyclepublic:voidRun()cout<<"Themotorcycleisrunning!"<<endl;voidStop()cout<<"Themotorcyclehasstopped!"<<endl;intmain()vehiclea;/的一個(gè)對(duì)象avehicle*p;/vehicle類的指針bicycleb;/的對(duì)象bmotorcarc;/類的對(duì)象cmotorcycled;/類的對(duì)象d定義vehicle類定義一個(gè)定義bicycle類定義motorcar定義motorcyclea.Run(

11、);Stop();Run();b.Stop();c.Run();c.Stop();d.Run();d.Stop();p=&a;/使指針p指向vehicle類對(duì)象ap->Run();/通過(guò)指針調(diào)用vehicle類成員函數(shù)p->Stop();p=&b;/使指針p指向bicycle類對(duì)象bp->Run();/通過(guò)指針調(diào)用bicycle類成員函數(shù)p->Stop();p=&c;/使指針p指向motorcar類對(duì)象cp->Run();/通過(guò)指針調(diào)用motorcar類成員函數(shù)p->Stop();p=&d;/使指針指向motorbicycle

12、類對(duì)象dp->Run();/通過(guò)指針調(diào)用motorcycle類成員函數(shù)p->Stop();return0;運(yùn)行結(jié)果:3.(選做)對(duì)實(shí)驗(yàn)4中的People類重載“=”運(yùn)算符和“”運(yùn)算符,“=”運(yùn)算符判斷兩個(gè)people類對(duì)象的id屆性是否相等;賦值操作。源程序:#include<iostream>#include<string>usingnamespacestd;classBirthdaypublic:Birthday(intYear,intMonth,intDay);Birthday()/Birthday()/Birthday(Birthday&p)

13、;/intshowBirthday()cout<<year<<"年"<<month<<"月"<<day<<"日intenter();private:intyear,month,day;Birthday:Birthday(Birthday&p)運(yùn)算符實(shí)現(xiàn)People類對(duì)象的/II;/構(gòu)造函數(shù)構(gòu)造函數(shù)析構(gòu)函數(shù)復(fù)制構(gòu)造函數(shù)內(nèi)聯(lián)成員函數(shù)year=p.year;month=p.month;day=p.day;Birthday:Birthday(intYear,intMonth

14、,intDay)/Birthday類構(gòu)造函數(shù)year=Year;month=Month;day=Day;intBirthday:enter()cout<<"生日:"cin>>year>>month>>day;classpeople/定義people類public:people()(/peoplepeople()(/peoplepeople(people&p);peopleoperator=(people&);peopleoperator-(people&);intshow();intenter();pr

15、ivate:stringnumber,id,sex;/Birthdayp1;/Birthday;intpeople:show()(cout<<"n性別"<<sex<<"編號(hào)"cout<<number;cout<<"生日"p1.showBirthday();/cout<<"身份證號(hào)"<<id<<endl;intpeople:enter()(p1.enter();cout<<"性別:";cin

16、>>sex;cout<<"編號(hào):"cin>>number;cout<<"身份證號(hào):"cin>>id;people:people(people&p):p1(p.p1)/people(number=p.number;sex=p.sex;id=p.id;peoplepeople:operator=(people&V)/(if(id=V.id)(cout<<"havethesameid!"<<endl;else(cout<<&quo

17、t;havedifferentid!"<<endl;類構(gòu)造函數(shù)類析構(gòu)函數(shù)字符申類型變量數(shù)據(jù)成員類數(shù)據(jù)成員調(diào)用Birthday類成員函數(shù)類復(fù)制構(gòu)造函數(shù)重載=運(yùn)算符成員函數(shù)number=U.number;sex=U.sex;id=U.id;p1=U.p1;return*this;/intmain()(intt;peoplep2;/for(t=0;t<2;t+)/使用字符申賦值運(yùn)算符返回this指釘定義對(duì)象數(shù)組p2輸入對(duì)象數(shù)組成員信息(cout<<"輸入第"<<t+1<<"個(gè)人員的信息"<&

18、lt;endl;pt.enter();for(t=0;t<2;t+)/輸出對(duì)象數(shù)組成員信息(cout<<"n第"<<t+1<<"個(gè)人員信息如下:"<<endl;pt.show();peoplep3;/定義people類的對(duì)象p3p3-p1;/使用重載運(yùn)算符將p1賦給p3cout<<"n第3個(gè)人員信息如下:"<<endl;p3.show();cout<<"p0,p1"p0=p1;/使用重載運(yùn)算符-判斷p0和p1的id是否相等return0;運(yùn)行結(jié)果:1. 思考題如何將一個(gè)運(yùn)算符重載為類的成員函數(shù)?一般語(yǔ)法形式:2. 返回類型operator運(yùn)算符(形參表)(函數(shù)體函數(shù)的參數(shù)個(gè)數(shù)比原來(lái)的曹祖數(shù)個(gè)數(shù)要少一個(gè)(后置“+”,“-”除外)如何將一個(gè)運(yùn)算符重載為類的友元函數(shù)?一般形式:friend返回類型op

溫馨提示

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