




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上 實驗四:多態(tài)性一、實驗目的1、掌握運算符重載的基本方法。2、掌握友元運算符函數(shù)和成員運算符函數(shù)的使用方法及兩者之間的不同。3、學習虛函數(shù)的定義與使用方法。4、了解靜態(tài)多態(tài)性和動態(tài)多態(tài)性。5、學習使用虛函數(shù)和繼承實現(xiàn)動態(tài)多態(tài)性。二、試驗內(nèi)容1、編寫一個程序,要求:(1)生明一個類Complex(復數(shù)類),定義類Complex的兩個對象c1和c2,對象c1通過構(gòu)造函數(shù)直接指定復數(shù)的實部和虛部(類私有數(shù)據(jù)成員為double類型:real和imag)為2.5及3.7,對象c2通過構(gòu)造函數(shù)直接指定復數(shù)的實部和虛部為4.2及6.5;(2)定義友元運算符重載函數(shù),它以c1、c2對
2、象為參數(shù),調(diào)用該函數(shù)時能返回兩個復數(shù)對象相加操作;(3)定義成員函數(shù)print,調(diào)用該函數(shù)時,以格式“real+imag i”輸出當前對象的實部和虛部,例如:對象的實部和虛部分別是4.2和6.5,則調(diào)用print函數(shù)輸出格式為:4.2+6.5 i;(4)編寫主程序,計算出復數(shù)對象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
3、Complex operator+(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.
4、print();getchar();2、編寫一個程序,其中設計一個時間類Time,用來保存時、分、秒等私有數(shù)據(jù)成員,通過重載操作符“+”實現(xiàn)兩個時間的相加。要求將小時范圍限制在大于等于0,分鐘范圍限制在059分,秒鐘范圍限制在059秒。提示:時間類Time的參考框架如下:class Time public: Time(int h=0,int m=0,int s=0);/構(gòu)造函數(shù) Time operator+(Time &);/運算符重載函數(shù),實現(xiàn)兩個時間的相加 void disptime();/顯示時間函數(shù) private: int hours,minutes,seconds;代碼:#
5、include<iostream.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)/運算符重載函數(shù),實現(xiàn)兩個時間的相加 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.
6、minutes>59)t.minutes-=60;t.hours+=1;return t; ; void disptime()/顯示時間函數(shù) cout<<this->hours<<"小時"<<this->minutes<<"分鐘"<<this->seconds<<"秒"<<endl; ; private: int hours,minutes,seconds;void main()Time t1(1,2,3);t1.disptim
7、e();Time t2(4,5,6);t2.disptime();(t1+t2).disptime();3、用友元運算符函數(shù)或成員運算符函數(shù),重載運算符“+”、“-”、“*”,實現(xiàn)對實驗二中實現(xiàn)的矩陣類的對象的加、減、乘法。#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
8、.row!=this->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->c
9、ol)cout<<"乘?不?了¢?"<<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
10、)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(int r,int c)int a = r*c;m = new inta;row = r;col = c;void print()for(int i=1;i<=row;i+)f
11、or(int j=1;j<=col;j+)cout<<m(i-1)*col+j-1<<" "cout<<endl;4、編寫一個程序,用于進行集合的和、并和交運算。例如輸入整數(shù)集合9,5,4,3,6,7和2,4,6,9,計算出他們進行集合的并、差和交運算后的結(jié)果?!咎崾尽浚?)可以用一下表達式實現(xiàn)整數(shù)集合的基本運算:s1+s2 兩個整數(shù)集合的并運算s1-s2 兩個整數(shù)集合的差運算s1*s2 兩個整數(shù)集合的交運算(2)參考以下Set類的框架,用于完成集合基本運算所需的各項功能。class Set public: Set(); void
12、input(int d);/向集合中添加一個元素 int length();/返回集合中的元素個數(shù) int getd(int i);/返回集合中位置i的元素 void display();/顯示集合的所有元素 Set operator+(Set s1);/成員運算符重載函數(shù),實現(xiàn)集合的并運算 Set operator-(Set s1);/成員運算符重載函數(shù),實現(xiàn)集合的差運算 Set operator*(Set s1);/成員運算符重載函數(shù),實現(xiàn)集合的交運算 Set operator=(Set s1);/成員運算符重載函數(shù),實現(xiàn)集合的賦值運算 protected: int len;/統(tǒng)計結(jié)合中元
13、素的個數(shù); int sMAX;/存放集合中的元素 ;#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 r
14、es(0); int flag = 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+)
15、if(s1.sj=si) 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 o
16、perator=(Set 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、下面提供一個體會繼承中的多態(tài)性和虛函數(shù)在多態(tài)性中的作用的題目。請根據(jù)提示進行實驗。定義類BaseFly,其中Fly()函數(shù)輸出特定內(nèi)容。例如: class BaseFly public:
17、void Fly()(cout<<"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<<
18、;"n-Class DragonFly:Fly()-n";) ); class PlaneFly:public BaseFly public: void Fly()cout<<"n-Class PlaneFly:Fly()-n"; ; (2)在main()函數(shù)中,用“new”關鍵字分配出以上四個類的實例,調(diào)用各個實例的Fly()函數(shù)測試多態(tài)性。請參考以下代碼: int main() cout<<"n測試繼承中的多態(tài)性(Virtual? Or not?):n'; BaseFly *pBase; BirdFly *p
19、Bird=new BirdFly; 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(); retu
20、rn 0, (3) 將BaseFly:Fly()聲明為virtual,在main()中定義BaseFly的指針:*pBase,依次分別指向UirdFly、DragonFly和P1aneFly,并調(diào)用各類的Fly()函數(shù),體會虛函數(shù)作用。 BaseFly * pBasenew BaseFly; BirdFly *pBird=new BirdFly;pBasepBird;程序代碼:#include<iostream>using namespace std;class BaseFly public: void Fly()cout<<"n-CIass BaseFly:
21、Fly()-n" 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
22、" ; int main() cout<<"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(); Plan
23、eFly * pPlane=new PlaneFly; pBase=pPlane; cout<<"nPlaneFly->n" pBase->Fly(); BaseFly *pBase=new BaseFly; BirdFly *pBird=new BirdFly; pBase=pBird; return 0; 6、寫一個程序,定義抽象類Container:class Container protected: double radius; public: Container(double r);/抽象類Container的構(gòu)造函數(shù) virtual do
24、uble surface_area()=0;/純虛函數(shù)surface_area virtual double volume()=0;/純虛函數(shù)volume ;【要求】建立3個繼承Container的派生類:Sphere(球體)、Cylinder(圓柱體)、Cube(正方體),讓每一個派生類都包含虛函數(shù)surface_area()和volume(),分別用來球體、圓柱體和正方體的表面積和體積。要求寫出主程序,應用C+的多態(tài)性,分別計算邊長為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:public C
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 山東省菏澤一中八一路校區(qū)2024-2025學年全國高三沖刺考(四)全國I卷物理試題含解析
- 2024-2025學年四川省眉山市車城中學第二學期高三期末統(tǒng)一考試英語試題含解析
- 秦皇島職業(yè)技術學院《鐵路站場及樞紐》2023-2024學年第二學期期末試卷
- 廣州國道施工方案
- 馬路磚施工方案
- 關于嬰兒背帶包的調(diào)查問卷
- 2025年食品行業(yè)資訊:全國居民消費價格指數(shù)同比下降0.7%
- 【2025年汽車產(chǎn)業(yè)布局成效初顯:12月汽車產(chǎn)銷同比增長超13%】
- 湖南省長沙市雨花區(qū)2024-2025學年高一(上)期末語文試卷
- 北京市西城區(qū)2024-2025學年高一(上)期末生物試卷(含解析)
- 企業(yè)風險分級管控與隱患排查治理雙體系落地
- 職工代表大會代表登記表(格式)
- GB/T 27731-2011衛(wèi)生用品用離型紙
- 專利交底書撰寫技巧
- 曼昆宏觀經(jīng)濟學第10版課后答案和筆記
- “一大”代表人生歷程與啟示匯總課件
- 2022年《經(jīng)濟金融基礎知識》近年真題考試題庫匯總(含答案)
- 《二手車鑒定評估與貿(mào)易》全套教學課件
- 音樂簡譜基礎知識
- 護士電子化注冊信息系統(tǒng)醫(yī)療機構(gòu)版醫(yī)療機構(gòu)快速閱讀手冊
- 【525心理輔導系列】有你的世界才精彩課件-心理健康
評論
0/150
提交評論