![C++上機實驗報告實驗六_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/9/360d6daf-20d0-4df0-9148-d379c280a67c/360d6daf-20d0-4df0-9148-d379c280a67c1.gif)
![C++上機實驗報告實驗六_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/9/360d6daf-20d0-4df0-9148-d379c280a67c/360d6daf-20d0-4df0-9148-d379c280a67c2.gif)
![C++上機實驗報告實驗六_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/9/360d6daf-20d0-4df0-9148-d379c280a67c/360d6daf-20d0-4df0-9148-d379c280a67c3.gif)
![C++上機實驗報告實驗六_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/9/360d6daf-20d0-4df0-9148-d379c280a67c/360d6daf-20d0-4df0-9148-d379c280a67c4.gif)
![C++上機實驗報告實驗六_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/9/360d6daf-20d0-4df0-9148-d379c280a67c/360d6daf-20d0-4df0-9148-d379c280a67c5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、實驗六 多態(tài)性1. 實驗?zāi)康?.掌握運算符重載的方法2.學(xué)習(xí)使用虛函數(shù)實現(xiàn)動態(tài)多態(tài)性2. 實驗要求1.定義Point類,有坐標(biāo)_x,_y兩個成員變量;對Point類重載“”(自增)、“”(自減)運算符,實現(xiàn)對坐標(biāo)值的改變。2.定義一個車(vehiele)基類,有Run、Stop等成員函數(shù),由此派生出自行車(bicycle)類、汽車(motorcar)類,從bicycle和motorcar派生出摩托車(motorcycle)類,它們都有Run、Stop等成員函數(shù)。觀察虛函數(shù)的作用。3. (選做)對實驗4中的People類重載“”運算符和“=”運算符,“”運算符判斷兩個people類對象的id屬性
2、是否相等;“=”運算符實現(xiàn)People類對象的賦值操作。3. 實驗內(nèi)容及實驗步驟1.編寫程序定義Point類,在類中定義整型的私有成員變量_x_y,定義成員函數(shù)Point& operator+();Point operator+(int);以實現(xiàn)對Point類重載“+”(自增)運算符,定義成員函數(shù)Point operator();Point operator(int);以實現(xiàn)對Point類重載“”(自減)運算符,實現(xiàn)對坐標(biāo)值的改變。程序名:1ab8_1cpp。2.編寫程序定義一個車(vehicle)基類,有Run、Stop等成員函數(shù),由此派生出自行車(bicycle)類、汽車(moto
3、rcar)類,從bicycle和motorcar派生出摩托車(motorcycle)類,它們都有Run、Stop等成員函數(shù)。在main()函數(shù)中定義vehicle、bicycle、motorcar、motorcycle的對象,調(diào)用其Run()、Stop()函數(shù),觀察其執(zhí)行情況。再分別用vehicle類型的指針來調(diào)用這幾個對象的成員函數(shù),看看能否成功;把Run、Stop定義為虛函數(shù),再試試看。程序名:lab8_2cpp。4. 思考題1. 如何將一個運算符重載為類的成員函數(shù)?函數(shù)類型 operator 運算符(形參表) 函數(shù)體;2. 如何將一個運算符重載為類的友元函數(shù)?friend 函數(shù)類型 op
4、erator 運算符(形參表) 函數(shù)體;3.如何實現(xiàn)運行時刻的多態(tài)?在基類的成員函數(shù)前加上virtual,就可以在它的派生類中聲明相同名字和類型的成員函數(shù),在運行過程中,系統(tǒng)會自動判斷并調(diào)用相應(yīng)類中的成員函數(shù),從而在調(diào)用過程中實現(xiàn)多態(tài)。5. 源程序1. lab8_1.cpp#include<iostream>using namespace std;class Pointprivate: int _x; int _y;public: /構(gòu)造.析構(gòu)函數(shù) Point() Point(int,int); Point() /+.-重載 Point& operator +(); Poi
5、nt operator +(int); Point& operator -(); Point operator -(int); /輸出點坐標(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;Poin
6、t Point:operator -(int) Point p=*this; -(*this); return p;void Point:showPoint() cout<<"The point is ("<<_x<<","<<_y<<")"<<endl;int main() Point apoint(3,5); apoint.showPoint(); (apoint+).showPoint();/測試后置+ apoint.showPoint(); (+apoin
7、t).showPoint();/測試前置+ apoint.showPoint(); (apoint-).showPoint();/測試后置- apoint.showPoint(); (-apoint).showPoint();/測試前置- apoint.showPoint(); return 0;2. lab8_2.cpp#include<iostream>using namespace std; class Vehiclepublic: /基類的成員函數(shù)為虛函數(shù) virtual void run()cout<<"Vehicle is running!&quo
8、t;<<endl; virtual void stop()cout<<"Vehicle is stopping!"<<endl;class Bicycle: virtual public Vehicle/按虛基類繼承public: void run()cout<<"Bicycle is running!"<<endl; void stop()cout<<"Bicycle is stopping!"<<endl;class Motorcar:virtua
9、l public Vehicle/按虛基類繼承public: void run()cout<<"Motorcar is running!"<<endl; void stop()cout<<"Motorcar is stopping!"<<endl;class Motorcycle:public Bicycle,public Motorcarpublic: void run()cout<<"Motorcycle is running!"<<endl; void st
10、op()cout<<"Motorcycle is stopping!"<<endl;int main() Vehicle veh; Bicycle bic; Motorcar mot; Motorcycle m; /對象名.函數(shù)測試 /*veh.run(); veh.stop(); bic.run(); bic.stop(); mot.run(); mot.stop(); m.run(); m.stop();*/ /基類指針測試 Vehicle* p; p=&veh; p->run(); p->stop(); p=&bic
11、; p->run(); p->stop(); p=&mot; p->run(); p->stop(); p=&m; p->run(); p->stop(); return 0;3. lab8_3#include<iostream>#include<cstring>using namespace std;/Date類class Dateprivate:int year;int month;int day;public:Date();Date(int y,int m,int d);Date(Date &p);Dat
12、e();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
13、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;cout<<"Input the year:&
14、quot;cin>>y;cout<<"Input the month:"cin>>m;cout<<"Input the day:"cin>>d;year=y;month=m;day=d;/Date內(nèi)聯(lián)成員函數(shù),輸出年月日inline void Date:showDate()cout<<"Birthday is "<<year<<"年"<<month<<"月"<<da
15、y<<"日"<<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);/Peop
16、le析構(gòu)inline People:People()/People成員函數(shù),設(shè)置各類數(shù)據(jù)void People:setName()cout<<"Please input the person's name:"cin.getline(name,11,'n');void People:setNumber()cout<<"Input number:"cin.getline(number,7,'n');void People:setSex()cout<<"Input sex:&
17、quot;cin.getline(sex,3,'n');void People:setId()cout<<"Input id:"cin.getline(id,16,'n');/People內(nèi)聯(lián)成員函數(shù),輸出人員信息inline void People:showPeople()cout<<"Name:"<<name<<endl;cout<<"Number:"<<number<<endl;cout<<"
18、Sex:"<<sex<<endl;cout<<"ID:"<<id<<endl;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;/檢測=重載的普通函數(shù)void test(People
19、& x,People& y)if(strcmp(x.id,y.id)=0)cout<<"Their's IDs are same"<<endl;elsecout<<"Their's IDs are different"<<endl;int main()/*int i;char spaceA;/生成3個Date類型的對象Date date3=Date(0,0,0),Date(0,0,0),Date(0,0,0);/生成3個People類型的對象People person3=Pe
20、ople("0","0","0",date0,"0"),People("0","0","0",date1,"0"),People("0","0","0",date2,"0");/設(shè)置這3個對象的各類信息for(i=0;i<3;i+)personi.setName();personi.setNumber();personi.setSex();personi.setId();datei.setDate();spaceA=getchar();/輸出這3個對象的各類信息for(i=0;i<3;i+)personi.showPeople();datei.showDate();*/測試=重載Date d1(0,0,0);People p1("lizhibo"
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國速凍叉燒包數(shù)據(jù)監(jiān)測研究報告
- 二零二五年度金融產(chǎn)品投資咨詢服務(wù)協(xié)議書模板
- 2025年度配音演員與舞臺劇聘用合同范本
- 2025年后成型彎曲機項目可行性研究報告
- 2025至2030年白雞腿菇菌粉項目投資價值分析報告
- 2025至2030年母子樂雕塑項目投資價值分析報告
- 2025至2030年子午線輪胎項目投資價值分析報告
- 2025至2030年埋弧焊機項目投資價值分析報告
- 2025至2030年余氯測量系統(tǒng)項目投資價值分析報告
- 2025年度房地產(chǎn)開發(fā)土方收購合同范本
- 第1課+古代亞非(教學(xué)設(shè)計)【中職專用】《世界歷史》(高教版2023基礎(chǔ)模塊)
- 新教科版六年級下冊科學(xué)全冊教案
- 物業(yè)客服管家的培訓(xùn)課件
- 2024年房地產(chǎn)行業(yè)的樓市調(diào)控政策解讀培訓(xùn)
- 《統(tǒng)計學(xué)-基于Python》 課件全套 第1-11章 數(shù)據(jù)與Python語言-時間序列分析和預(yù)測
- 《GMP實務(wù)教程》 完整全套教學(xué)課件 項目1-14 GMP基礎(chǔ)知識-藥品生產(chǎn)行政檢查
- 裝飾定額子目(河南省)
- 【高速鐵路乘務(wù)工作存在的問題及對策研究9800字】
- 北師大版英語課文同步字帖三年級下冊課文對話原文及翻譯衡水體英語字帖三年級起點
- GB/T 2550-2016氣體焊接設(shè)備焊接、切割和類似作業(yè)用橡膠軟管
- GB/T 21295-2014服裝理化性能的技術(shù)要求
評論
0/150
提交評論