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

下載本文檔

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

文檔簡介

試驗(yàn)六試驗(yàn)?zāi)康?.掌握運(yùn)算符重載的措施2.學(xué)習(xí)使用虛函數(shù)實(shí)現(xiàn)動(dòng)態(tài)多態(tài)性試驗(yàn)規(guī)定1.定義Point類,有坐標(biāo)_x,_y兩個(gè)組員變量;對(duì)Point類重載“++”(自增)、“――”(自減)運(yùn)算符,實(shí)現(xiàn)對(duì)坐標(biāo)值的變化。2.定義一種車(vehiele)基類,有Run、Stop等組員函數(shù),由此派生出自行車(bicycle)類、汽車(motorcar)類,從bicycle和motorcar派生出摩托車(motorcycle)類,它們均有Run、Stop等組員函數(shù)。觀測(cè)虛函數(shù)的作用。3.(選做)對(duì)試驗(yàn)4中的People類重載“==”運(yùn)算符和“-”運(yùn)算符,“==”運(yùn)算符判斷兩個(gè)people類對(duì)象的id屬性與否相等;“-”運(yùn)算符實(shí)現(xiàn)People類對(duì)象的賦值操作。試驗(yàn)內(nèi)容及試驗(yàn)環(huán)節(jié)編寫程序定義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;classPoint{public: Point(); Point(intx,inty); ~Point(){}//Point類析構(gòu)函數(shù) Point&operator++();//公有組員函數(shù) Pointoperator++(int); Point&operator--(); Pointoperator--(int); voidShow();private: int_x;//私有數(shù)據(jù)組員 int_y;};Point::Point(){//Point類構(gòu)造函數(shù)_x=0;_y=0;}Point::Point(intx,inty)//Point類構(gòu)造函數(shù){ _x=x; _y=y;}Point&Point::operator++()//重載後置++運(yùn)算符為Point類組員函數(shù){ _x++; _y++;}PointPoint::operator++(int)//重載前置++運(yùn)算符為Point類組員函數(shù){ Pointold=*this; ++(this->_x); ++(this->_y); returnold;}Point&Point::operator--()//重載後置--運(yùn)算符為Point類組員函數(shù){ _x--; _y--;}PointPoint::operator--(int)//重載前置--運(yùn)算符為Point類組員函數(shù){ Pointold=*this; --(this->_x); --(this->_y); returnold;}voidPoint::Show()//輸出Point的坐標(biāo)值{cout<<_x<<","<<_y<<")"<<endl;}intmain(){ Pointa(2,3);//定義一種Point類對(duì)象a Pointb=a++;//定義Point類對(duì)象b并用a++初始化b Pointc=++a;//定義Point類對(duì)象c并用++a初始化c Pointd=--a;//定義Point類對(duì)象d并用--a初始化d Pointe=a--;//定義Point類對(duì)象e并用a--初始化e cout<<"Pointa("; a.Show();//輸出a的坐標(biāo) cout<<"Pointb("; b.Show();//輸出b的坐標(biāo) cout<<"Pointc("; c.Show();//輸出c的坐標(biāo) cout<<"Pointd("; d.Show();//輸出d的坐標(biāo) cout<<"Pointe("; e.Show();//輸出e的坐標(biāo) return0;}運(yùn)行成果:2編寫程序定義一種車(vehicle)基類,有Run、Stop等組員函數(shù),由此派生出自行車(bicycle)類、汽車(motorcar)類,從bicycle和motorcar派生出摩托車(motorcycle)類,它們均有Run、Stop等組員函數(shù)。在main()函數(shù)中定義vehicle、bicycle、motorcar、motorcycle的對(duì)象,調(diào)用其Run()、Stop()函數(shù),觀測(cè)其執(zhí)行狀況。再分別用vehicle類型的指針來調(diào)用這幾種對(duì)象的組員函數(shù),看看能否成功;把Run、Stop定義為虛函數(shù),再試試看。程序名:lab8_2.cpp#include<iostream>usingnamespacestd;classvehicle//基類vehicle{public: vehicle(){} ~vehicle(){} virtualvoidRun(){cout<<"Thevehicleisrunning!"<<endl;}//定義虛函數(shù)Run() virtualvoidStop(){cout<<"Thevehiclehasstopped!"<<endl;}//定義虛函數(shù)Stop()};classbicycle:virtualpublicvehicle//定義派生類bicycle,申明基類為派生類的虛基類{public: voidRun(){cout<<"Thebicycleisrunning!"<<endl;} voidStop(){cout<<"Thebicyclehasstopped!"<<endl;} bicycle(){} ~bicycle(){}};classmotorcar:virtualpublicvehicle//定義派生類motorcar,申明基類為派生類的虛基類{public: voidRun(){cout<<"Themotorcarisrunning!"<<endl;} voidStop(){cout<<"Themotorcarhasstopped!"<<endl;} motorcar(){} ~motorcar(){}};classmotorcycle:publicbicycle,publicmotorcar//以bicycle類和motorcar類作為基類派生新類motorcycle{public: voidRun(){cout<<"Themotorcycleisrunning!"<<endl;} voidStop(){cout<<"Themotorcyclehasstopped!"<<endl;}};intmain(){ vehiclea;//定義vehicle類的一種對(duì)象a vehicle*p;//定義一種vehicle類的指針 bicycleb;//定義bicycle類的對(duì)象b motorcarc;//定義motorcar類的對(duì)象c motorcycled;//定義motorcycle類的對(duì)象d a.Run(); a.Stop(); b.Run(); b.Stop(); c.Run(); c.Stop(); d.Run(); d.Stop(); p=&a;//使指針p指向vehicle類對(duì)象a p->Run();//通過指針調(diào)用vehicle類組員函數(shù) p->Stop(); p=&b;//使指針p指向bicycle類對(duì)象b p->Run();//通過指針調(diào)用bicycle類組員函數(shù) 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(); return0;}運(yùn)行成果:3.(選做)對(duì)試驗(yàn)4中的People類重載“==”運(yùn)算符和“-”運(yùn)算符,“==”運(yùn)算符判斷兩個(gè)people類對(duì)象的id屬性與否相等;“-”運(yùn)算符實(shí)現(xiàn)People類對(duì)象的賦值操作。源程序:#include<iostream>#include<string>usingnamespacestd;classBirthday{public: Birthday(intYear,intMonth,intDay);//構(gòu)造函數(shù) Birthday(){}//構(gòu)造函數(shù)~Birthday(){}//析構(gòu)函數(shù)Birthday(Birthday&p);//復(fù)制構(gòu)造函數(shù)intshowBirthday(){cout<<year<<"年"<<month<<"月"<<day<<"曰";}//內(nèi)聯(lián)組員函數(shù)intenter();private: intyear,month,day;};Birthday::Birthday(Birthday&p){year=p.year;month=p.month;day=p.day;}Birthday::Birthday(intYear,intMonth,intDay)//Birthday類構(gòu)造函數(shù) { year=Year; month=Month; day=Day; }intBirthday::enter(){cout<<"生曰:";cin>>year>>month>>day;}classpeople{//定義people類public: people(){}//people類構(gòu)造函數(shù) ~people(){}//people類析構(gòu)函數(shù) people(people&p); peopleoperator==(people&); peopleoperator-(people&); intshow(); intenter();private: stringnumber,id,sex;//字符串類型變量數(shù)據(jù)組員Birthdayp1;//Birthday類數(shù)據(jù)組員};intpeople::show(){cout<<"\n性別"<<sex<<"編號(hào)";cout<<number;cout<<"生曰";p1.showBirthday();//調(diào)用Birthday類組員函數(shù)cout<<"身份證號(hào)"<<id<<endl;}intpeople::enter(){ p1.enter(); cout<<"性別:";cin>>sex;cout<<"編號(hào):";cin>>number;cout<<"身份證號(hào):";cin>>id;}people::people(people&p):p1(p.p1)//people類復(fù)制構(gòu)造函數(shù){ number=p.number; sex=p.sex;id=p.id;}peoplepeople::operator==(people&V)//重載==運(yùn)算符組員函數(shù){ if(id==V.id) { cout<<"havethesameid!"<<endl; } else {cout<<"havedifferentid!"<<endl;}}peoplepeople::operator-(people&U)//重載-運(yùn)算符組員函數(shù){number=U.number;//使用字符串賦值運(yùn)算符sex=U.sex;id=U.id;p1=U.p1;return*this;//返回this指針}intmain(){ intt; peoplep[2];//定義對(duì)象數(shù)組p[2] for(t=0;t<2;t++)//輸入對(duì)象數(shù)組組員信息 { cout<<"輸入第"<<t+1<<"個(gè)人員的信息"<<endl; p[t].enter(); } for(t=0;t<2;t++)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論