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è),還剩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)介

實(shí)驗(yàn)六 多態(tài)性1. 實(shí)驗(yàn)?zāi)康?.掌握運(yùn)算符重載的方法2.學(xué)習(xí)使用虛函數(shù)實(shí)現(xiàn)動(dòng)態(tài)多態(tài)性2. 實(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屬性是否相等;“=”運(yùn)算符實(shí)現(xiàn)People類對(duì)象的賦值操作。3. 實(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。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類型的指針來(lái)調(diào)用這幾個(gè)對(duì)象的成員函數(shù),看看能否成功;把Run、Stop定義為虛函數(shù),再試試看。程序名:lab8_2cpp。4. 思考題1. 如何將一個(gè)運(yùn)算符重載為類的成員函數(shù)?函數(shù)類型 operator 運(yùn)算符(形參表) 函數(shù)體;2. 如何將一個(gè)運(yùn)算符重載為類的友元函數(shù)?friend 函數(shù)類型 operator 運(yùn)算符(形參表) 函數(shù)體;3.如何實(shí)現(xiàn)運(yùn)行時(shí)刻的多態(tài)?在基類的成員函數(shù)前加上virtual,就可以在它的派生類中聲明相同名字和類型的成員函數(shù),在運(yùn)行過(guò)程中,系統(tǒng)會(huì)自動(dòng)判斷并調(diào)用相應(yīng)類中的成員函數(shù),從而在調(diào)用過(guò)程中實(shí)現(xiàn)多態(tài)。5. 源程序1. lab8_1.cpp#includeusing namespace std;class Pointprivate: int _x; int _y;public: /構(gòu)造.析構(gòu)函數(shù) Point() Point(int,int); Point() /+.-重載 Point& operator +(); Point operator +(int); Point& operator -(); Point operator -(int); /輸出點(diǎn)坐標(biāo) void showPoint();Point:Point(int x,int y) _x=x; _y=y;Point& Point:operator +() _x+; _y+; return *this;Point Point:operator +(int) Point p=*this; +(*this); return p;Point& Point:operator -() _x-; _y-; return *this;Point Point:operator -(int) Point p=*this; -(*this); return p;void Point:showPoint() coutThe point is (_x,_y)endl;int main() Point apoint(3,5); apoint.showPoint(); (apoint+).showPoint();/測(cè)試后置+ apoint.showPoint(); (+apoint).showPoint();/測(cè)試前置+ apoint.showPoint(); (apoint-).showPoint();/測(cè)試后置- apoint.showPoint(); (-apoint).showPoint();/測(cè)試前置- apoint.showPoint(); return 0;2. lab8_2.cpp#includeusing namespace std; class Vehiclepublic: /基類的成員函數(shù)為虛函數(shù) virtual void run()coutVehicle is running!endl; virtual void stop()coutVehicle is stopping!endl;class Bicycle: virtual public Vehicle/按虛基類繼承public: void run()coutBicycle is running!endl; void stop()coutBicycle is stopping!endl;class Motorcar:virtual public Vehicle/按虛基類繼承public: void run()coutMotorcar is running!endl; void stop()coutMotorcar is stopping!endl;class Motorcycle:public Bicycle,public Motorcarpublic: void run()coutMotorcycle is running!endl; void stop()coutMotorcycle is stopping!run(); p-stop(); p=&bic; p-run(); p-stop(); p=&mot; p-run(); p-stop(); p=&m; p-run(); p-stop(); return 0;3. lab8_3#include#includeusing namespace std;/Date類class Dateprivate:int year;int month;int day;public:Date();Date(int y,int m,int d);Date(Date &p);Date();void setDate();void showDate();/People類,其中含Date類型的數(shù)據(jù)class Peopleprivate:char name11;char number7;char sex3;Date birthday;public:char id16;People();People(char* n,char* nu,char* s,Date b,char* i);People(People &p);People();bool operator =(People&);People& operator =(People&);void setName();void setNumber();void setSex();void setId();void showPeople();/Date構(gòu)造函數(shù)Date:Date()Date:Date(int y,int m,int d)year=y;month=m;day=d;Date:Date(Date &p)year=p.year;month=p.month;day=p.day;/析構(gòu)inline Date:Date()/Date成員函數(shù),設(shè)置出生年月日void Date:setDate()int y,m,d;couty;coutm;coutd;year=y;month=m;day=d;/Date內(nèi)聯(lián)成員函數(shù),輸出年月日inline void Date:showDate()coutBirthday is year年month月day日endl;/People構(gòu)造函數(shù)People:People();People:People(char* n,char* nu,char* s,Date b,char* i)strcpy(name,n);strcpy(number,nu);strcpy(sex,s);birthday=b;strcpy(id,i);People:People(People &p)strcpy(name,);strcpy(number,p.number);birthday=p.birthday;strcpy(id,p.id);/People析構(gòu)inline People:People()/People成員函數(shù),設(shè)置各類數(shù)據(jù)void People:setName()coutPlease input the persons name:;cin.getline(name,11,n);void People:setNumber()coutInput number:;cin.getline(number,7,n);void People:setSex()coutInput sex:;cin.getline(sex,3,n);void People:setId()coutInput id:;cin.getline(id,16,n);/People內(nèi)聯(lián)成員函數(shù),輸出人員信息inline void People:showPeople()coutName:nameendl;coutNumber:numberendl;coutSex:sexendl;coutID:idendl;bool People:operator =(People& p)return id=p.id;People& People:operator =(People& p)strcpy(name,);strcpy(number,p.number);birthday=p.birthday;strcpy(id,p.id);return *this;/檢測(cè)=重載的普通函數(shù)void test(People& x,People& y)if(strcmp(x.id,y.id)=0)coutTheirs IDs are sameendl;elsecoutTheirs IDs are differentendl;int main()/*int i;char spaceA;/生成3個(gè)Date類型的對(duì)象Date date3=Date(0,0,0),Date(0,0,0),Date(0,0,0);/生成3個(gè)People類型的對(duì)象People person3=People(0,0,0,date0,0),People(0,0,0,date1,0),People(0,0,0,date2,0);/設(shè)置這3個(gè)對(duì)象的各類信息for(i=0;i3;i+)personi.setName();personi.setNumber();personi.setSex();personi.setId();datei.setDate();spaceA=getchar();/輸出這3個(gè)對(duì)象的各類信息for(i=0;i3;i+)personi.showPeople();datei.showDate();*/測(cè)試=重載Date d1(0,0,0);People p1(lizhibo,01,male,d1,);People p2(wangyusen,02,male,d1,123); People p3(zhouhao,03,male,d1,);test(p1,p2);test(p1,p3);/測(cè)試=重載coutBefore =endl;p1.showPeople();p1=p3;coutAfter =endl;p1.showPeople();return 0;6. 運(yùn)行結(jié)果1. 2.直接使用對(duì)象.函數(shù)的形式可以成功調(diào)用函數(shù)

溫馨提示

  • 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)論