




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、 實(shí)驗(yàn)四:多態(tài)性一、實(shí)驗(yàn)?zāi)康?、掌握運(yùn)算符重載的基本方法。2、掌握友元運(yùn)算符函數(shù)和成員運(yùn)算符函數(shù)的使用方法及兩者之間的不同。3、學(xué)習(xí)虛函數(shù)的定義與使用方法。4、了解靜態(tài)多態(tài)性和動(dòng)態(tài)多態(tài)性。5、學(xué)習(xí)使用虛函數(shù)和繼承實(shí)現(xiàn)動(dòng)態(tài)多態(tài)性。二、試驗(yàn)內(nèi)容1、編寫一個(gè)程序,要求:(1)生明一個(gè)類Complex(復(fù)數(shù)類),定義類Complex的兩個(gè)對(duì)象c1和c2,對(duì)象c1通過構(gòu)造函數(shù)直接指定復(fù)數(shù)的實(shí)部和虛部(類私有數(shù)據(jù)成員為double類型:real和imag)為2.5及3.7,對(duì)象c2通過構(gòu)造函數(shù)直接指定復(fù)數(shù)的實(shí)部和虛部為4.2及6.5;(2)定義友元運(yùn)算符重載函數(shù),它以c1、c2對(duì)象為參數(shù),調(diào)用該函數(shù)時(shí)能返
2、回兩個(gè)復(fù)數(shù)對(duì)象相加操作;(3)定義成員函數(shù)print,調(diào)用該函數(shù)時(shí),以格式“real+imag i”輸出當(dāng)前對(duì)象的實(shí)部和虛部,例如:對(duì)象的實(shí)部和虛部分別是4.2和6.5,則調(diào)用print函數(shù)輸出格式為:4.2+6.5 i;(4)編寫主程序,計(jì)算出復(fù)數(shù)對(duì)象c1和c2相加結(jié)果,并將其結(jié)果輸出。#include<iostream>Using namespace std;class Complexpublic:Complex()real = 0;imag = 0;Complex(double r,double i)real = r;imag = i;friend Complex opera
3、tor+(Complex a,Complex b)Complex c(a.real+b.real,a.imag+b.imag);return c;void print()cout<<this->real<<"+ i"<<this->imag<<endl;private:double real,imag;void main()Complex c1(2.5,3.7);c1.print();Complex c2(4.2,6.5);c2.print();Complex c3 = c1+c2;c3.print();getch
4、ar();2、編寫一個(gè)程序,其中設(shè)計(jì)一個(gè)時(shí)間類Time,用來保存時(shí)、分、秒等私有數(shù)據(jù)成員,通過重載操作符“+”實(shí)現(xiàn)兩個(gè)時(shí)間的相加。要求將小時(shí)范圍限制在大于等于0,分鐘范圍限制在059分,秒鐘范圍限制在059秒。提示:時(shí)間類Time的參考框架如下:class Time public: Time(int h=0,int m=0,int s=0);/構(gòu)造函數(shù) Time operator+(Time &);/運(yùn)算符重載函數(shù),實(shí)現(xiàn)兩個(gè)時(shí)間的相加 void disptime();/顯示時(shí)間函數(shù) private: int hours,minutes,seconds;代碼:#include<io
5、stream.h>class Time public: Time(int h=0,int m=0,int s=0)/構(gòu)造函數(shù) hours = h; minutes = m; seconds = s; ; Time operator+(Time &a)/運(yùn)算符重載函數(shù),實(shí)現(xiàn)兩個(gè)時(shí)間的相加 Time t(this->hours+a.hours,this->minutes+a.minutes,this->seconds+a.seconds);if(t.seconds>59)t.seconds-=60;t.minutes+=1;if(t.minutes>59
6、)t.minutes-=60;t.hours+=1;return t; ; void disptime()/顯示時(shí)間函數(shù) cout<<this->hours<<"小時(shí)"<<this->minutes<<"分鐘"<<this->seconds<<"秒"<<endl; ; private: int hours,minutes,seconds;void main()Time t1(1,2,3);t1.disptime();Time t2(4
7、,5,6);t2.disptime();(t1+t2).disptime();3、用友元運(yùn)算符函數(shù)或成員運(yùn)算符函數(shù),重載運(yùn)算符“+”、“-”、“*”,實(shí)現(xiàn)對(duì)實(shí)驗(yàn)二中實(shí)現(xiàn)的矩陣類的對(duì)象的加、減、乘法。#include<iostream>using namespace std;class Matrixprivate:int row,col;int* m;public:int &operator()(int i,int j)return m(i-1)*this->col+j-1;Matrix operator-(Matrix &a)if(a.row!=this-&g
8、t;row|a.col!=this->col)cout<<"減?不?了¢?"<<endl;return a;Matrix result(row,col);for(int i=1;i<=row;i+)for(int j=1;j<=col;j+)result(i,j) = this->m(i-1)*col+j-1-a(i,j);return result;Matrix operator*(Matrix &a)if(a.row!=this->row|a.col!=this->col)cout<&l
9、t;"乘?不?了¢?"<<endl;return a;Matrix result(a.row,col);for(int i=1;i<=row;i+)for(int j=1;j<=col;j+)int temp = 0;for(int k =1;k<col;k+) temp = this->m(i-1)*col+k-1*a(k,j);return result;Matrix operator+(Matrix &a)if(a.row!=this->row|a.col!=this->col)cout<<
10、"加¨®不?了¢?"<<endl;return a;Matrix result(row,col);for(int i=1;i<=row;i+)for(int j=1;j<=col;j+)result(i,j) = this->m(i-1)*col+j-1+a(i,j);return result;Matrix(int r,int c)int a = r*c;m = new inta;row = r;col = c;void print()for(int i=1;i<=row;i+)for(int j=1;j&
11、lt;=col;j+)cout<<m(i-1)*col+j-1<<" "cout<<endl;4、編寫一個(gè)程序,用于進(jìn)行集合的和、并和交運(yùn)算。例如輸入整數(shù)集合9,5,4,3,6,7和2,4,6,9,計(jì)算出他們進(jìn)行集合的并、差和交運(yùn)算后的結(jié)果。【提示】(1)可以用一下表達(dá)式實(shí)現(xiàn)整數(shù)集合的基本運(yùn)算:s1+s2 兩個(gè)整數(shù)集合的并運(yùn)算s1-s2 兩個(gè)整數(shù)集合的差運(yùn)算s1*s2 兩個(gè)整數(shù)集合的交運(yùn)算(2)參考以下Set類的框架,用于完成集合基本運(yùn)算所需的各項(xiàng)功能。class Set public: Set(); void input(int d);
12、/向集合中添加一個(gè)元素 int length();/返回集合中的元素個(gè)數(shù) int getd(int i);/返回集合中位置i的元素 void display();/顯示集合的所有元素 Set operator+(Set s1);/成員運(yùn)算符重載函數(shù),實(shí)現(xiàn)集合的并運(yùn)算 Set operator-(Set s1);/成員運(yùn)算符重載函數(shù),實(shí)現(xiàn)集合的差運(yùn)算 Set operator*(Set s1);/成員運(yùn)算符重載函數(shù),實(shí)現(xiàn)集合的交運(yùn)算 Set operator=(Set s1);/成員運(yùn)算符重載函數(shù),實(shí)現(xiàn)集合的賦值運(yùn)算 protected: int len;/統(tǒng)計(jì)結(jié)合中元素的個(gè)數(shù); int sMA
13、X;/存放集合中的元素 ;#include<iostream>using namespace std;class Set public: Set(int l = 0) len = l; ; void input(int d) slen = d; len+; int length() return len; int getd(int i) return si-1; void display() for (int i = 0;i<len;i+) cout<<si<<endl; Set operator+(Set s1) Set res(0); int fl
14、ag = 1; for(int i=0;i<len;i+) res.input(si); for(int j=0;j<s1.len;j+) flag = 1; for (i = 0;i<len;i+) if(s1.sj=si) flag = 0; if(flag) res.input(s1.sj); return res; ; Set operator-(Set s1) Set res(); int flag = 0; for(int j=0;j<s1.len;j+) flag = 1; for (int i = 0;i<len;i+) if(s1.sj=si)
15、flag = 0; if(flag) input(s1.sj); for(int j=0;j<s1.len;j+) flag = 1; for (int i = 0;i<len;i+) if(s1.si=sj) flag = 0; if(flag) input(sj); ; Set operator*(Set s1) for(int j=0;j<s1.len;j+) int flag = 0; for (int i = 0;i<len;i+) if(s1.sj=si) flag = 1; if(flag) input(s1.sj); Set operator=(Set
16、s1) len = 0; for(int i = 0;i<s1.len;i+)input(s1.getd(i); protected: int len; int s20; ;void main()Set a;a. operator=(Set s1);a. operator*(Set s1);a. operator+(Set s1);a. operator-(Set s1);5、下面提供一個(gè)體會(huì)繼承中的多態(tài)性和虛函數(shù)在多態(tài)性中的作用的題目。請(qǐng)根據(jù)提示進(jìn)行實(shí)驗(yàn)。定義類BaseFly,其中Fly()函數(shù)輸出特定內(nèi)容。例如: class BaseFly public: void Fly()(co
17、ut<<"n-CIass BaseFly:Fly()-n"; ; (1)定義類BirdFly、DragonFly和PlaneFly,都繼承自BaseFly,重載Fly()函數(shù),使得各類中的Fly()函數(shù)分別輸出不同的內(nèi)容。 class BirdFly:public BaseFly public: void Fly() cout<<"n-class BirdFly:Fly()-n"; ; class DragonFly:public BaseFly public: vold Fly()cout<<"n-Clas
18、s DragonFly:Fly()-n";) ); class PlaneFly:public BaseFly public: void Fly()cout<<"n-Class PlaneFly:Fly()-n"; ; (2)在main()函數(shù)中,用“new”關(guān)鍵字分配出以上四個(gè)類的實(shí)例,調(diào)用各個(gè)實(shí)例的Fly()函數(shù)測(cè)試多態(tài)性。請(qǐng)參考以下代碼: int main() cout<<"n測(cè)試?yán)^承中的多態(tài)性(Virtual? Or not?):n'; BaseFly *pBase; BirdFly *pBird=new Bird
19、Fly; pBascpBird; cout<<"nBirdFly->n" pBase->Fly(); DragonFly * pDragon=new DragonFly; pBase=pDragon; cout<<"nDragonFly->n" pBase->Fly(); PlaneFly * pPlane=new PlaneFly; pBasepPlane; cout<<"nPlaneFly->n" pBase->Fly(); return 0, (3) 將Ba
20、seFly:Fly()聲明為virtual,在main()中定義BaseFly的指針:*pBase,依次分別指向UirdFly、DragonFly和P1aneFly,并調(diào)用各類的Fly()函數(shù),體會(huì)虛函數(shù)作用。 BaseFly * pBasenew BaseFly; BirdFly *pBird=new BirdFly;pBasepBird;程序代碼:#include<iostream>using namespace std;class BaseFly public: void Fly()cout<<"n-CIass BaseFly:Fly()-n"
21、 class BirdFly:public BaseFly public: void Fly() cout<<"n-class BirdFly:Fly()-n" ; class DragonFly:public BaseFly public:void Fly()cout<<"n-Class DragonFly:Fly()-n" class PlaneFly:public BaseFly public: void Fly()cout<<"n-Class PlaneFly:Fly()-n" ; int
22、main() cout<<"n測(cè)試?yán)^承中的多態(tài)性(Virtual? Or not?):n" BaseFly *pBase; BirdFly *pBird=new BirdFly; pBase=pBird; cout<<"nBirdFly->n" pBase->Fly(); DragonFly * pDragon=new DragonFly; pBase=pDragon; cout<<"nDragonFly->n" pBase->Fly(); PlaneFly * pPlane
23、=new PlaneFly; pBase=pPlane; cout<<"nPlaneFly->n" pBase->Fly(); BaseFly *pBase=new BaseFly; BirdFly *pBird=new BirdFly; pBase=pBird; return 0; 6、寫一個(gè)程序,定義抽象類Container:class Container protected: double radius; public: Container(double r);/抽象類Container的構(gòu)造函數(shù) virtual double surface_
24、area()=0;/純虛函數(shù)surface_area virtual double volume()=0;/純虛函數(shù)volume ;【要求】建立3個(gè)繼承Container的派生類:Sphere(球體)、Cylinder(圓柱體)、Cube(正方體),讓每一個(gè)派生類都包含虛函數(shù)surface_area()和volume(),分別用來球體、圓柱體和正方體的表面積和體積。要求寫出主程序,應(yīng)用C+的多態(tài)性,分別計(jì)算邊長(zhǎng)為6.0的正方體、半徑為5.0的球體,以及半徑為5.0和高為6.0的圓柱體的表面積和體積。#include<iostream>using namespace std;class Container protected: double radius; public: Container() Container(double r)/抽象類Container的構(gòu)造函數(shù) radius = r; virtual double surface_area()=0;/純虛函數(shù)surface_area virtual double volume()=0;/純虛函數(shù)volume ;class Sphere:pu
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度果樹種植土地托管承包與農(nóng)村金融創(chuàng)新合作協(xié)議
- 2025年度汽車維修行業(yè)安全生產(chǎn)責(zé)任簡(jiǎn)易合同
- 二零二五年度高科技研發(fā)項(xiàng)目勞務(wù)合同風(fēng)險(xiǎn)評(píng)估書
- 二零二五年度健康醫(yī)療合伙投資公司股權(quán)合作協(xié)議
- 二零二五年度智能制造合同履行流程監(jiān)督與執(zhí)行協(xié)議
- 二零二五年度文化藝術(shù)交流正規(guī)藝術(shù)家合作協(xié)議
- 二零二五年度倆孩子撫養(yǎng)權(quán)及財(cái)產(chǎn)分割協(xié)議確保子女未來
- 二零二五年度旅游行業(yè)返利分成合同
- 2025年度長(zhǎng)租公寓租賃合同風(fēng)險(xiǎn)評(píng)估與應(yīng)對(duì)策略
- 2025年南京貨運(yùn)從業(yè)資格證考試試題答案
- 《算法與數(shù)字生活》 教學(xué)設(shè)計(jì)
- 組織行為學(xué)(對(duì)外經(jīng)濟(jì)貿(mào)易大學(xué))智慧樹知到答案章節(jié)測(cè)試2023年
- 產(chǎn)品過程特殊特性初始清單(示例)
- 部編人教版小學(xué)五年級(jí)道德與法治下冊(cè)全冊(cè)完整課件ppt
- GB/Z 17625.6-2003電磁兼容限值對(duì)額定電流大于16A的設(shè)備在低壓供電系統(tǒng)中產(chǎn)生的諧波電流的限制
- GB/T 14643.4-2009工業(yè)循環(huán)冷卻水中菌藻的測(cè)定方法第4部分:土壤真菌的測(cè)定平皿計(jì)數(shù)法
- 注塑成型工藝培訓(xùn)資料合集課件
- 山財(cái)自考審計(jì)學(xué)37作業(yè)(已填好答案)
- rg-wall1600系列下一代防火墻命令手冊(cè)
- 土地整治項(xiàng)目工程量計(jì)算規(guī)則(2013-8-1實(shí)施)
- 儲(chǔ)罐基礎(chǔ)允許偏差項(xiàng)目復(fù)測(cè)記錄
評(píng)論
0/150
提交評(píng)論