實(shí)驗(yàn)13-虛函數(shù)(共20頁)_第1頁
實(shí)驗(yàn)13-虛函數(shù)(共20頁)_第2頁
實(shí)驗(yàn)13-虛函數(shù)(共20頁)_第3頁
實(shí)驗(yàn)13-虛函數(shù)(共20頁)_第4頁
實(shí)驗(yàn)13-虛函數(shù)(共20頁)_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上實(shí)驗(yàn)13 虛函數(shù)牛旭艷 智能二班 一、實(shí)驗(yàn)?zāi)康?、復(fù)習(xí)運(yùn)算符重載。2、了解虛基類的作用和用法。3、進(jìn)一步理解多態(tài)性的概念。4、了解虛函數(shù)的作用及使用方法。5、了解靜態(tài)關(guān)聯(lián)和動(dòng)態(tài)關(guān)聯(lián)的概念和用法。6、了解純虛函數(shù)和抽象類的概念和用法。二、實(shí)驗(yàn)內(nèi)容1.定義Point類,有坐標(biāo)x,y兩個(gè)成員變量,利用友元函數(shù)對(duì)Point類重載“+”運(yùn)算符,實(shí)現(xiàn)對(duì)坐標(biāo)值的改變。具體要求如下:(1)編寫程序定義Point類,在類中定義整型的私有成員變量x,y;(2)在類中定義兩個(gè)友元函數(shù),分別重載前置+和后置+;(3)編寫主函數(shù)測試。注意函數(shù)有無返回值的區(qū)別,以及返回值是否帶有&應(yīng)用符

2、號(hào)。2.定義Point類,有坐標(biāo)x,y兩個(gè)成員變量,利用成員函數(shù)對(duì)Point類重載“+”運(yùn)算符,實(shí)現(xiàn)對(duì)坐標(biāo)值的改變。具體要求如下:(1)編寫程序定義Point類,在類中定義整型的私有成員變量x,y;(2)定義成員函數(shù)Point operator+(); Point operator+(int);以實(shí)現(xiàn)對(duì)Point類重載“+”運(yùn)算符,分別重載前置+和后置+;(3)編寫主函數(shù)測試。3.定義一個(gè)分?jǐn)?shù)類,通過重載運(yùn)算符實(shí)現(xiàn)分?jǐn)?shù)的四則運(yùn)算、求負(fù)運(yùn)算和賦值運(yùn)算。其中,要求加法“+” 和減法“-”用友元函數(shù)實(shí)現(xiàn)重載,其他運(yùn)算符用成員函數(shù)實(shí)現(xiàn)重載。4.定義一個(gè)車(vehicle)基類,有Run、Stop等成員

3、函數(shù),由此派生出自行車(bicycle)類、汽車(motorcar)類,從bicycle和motorcar派生出摩托車(motorcycle)類,它們都有Run、Stop等成員函數(shù)。具體要求如下:(1)編寫程序定義一個(gè)車(vehicle)基類,有Run、Stop等成員函數(shù);(2)由車(vehicle)基類派生出自行車(bicycle)類、汽車類(motorcar),從bicycle類和motorcar類派生出摩托車類(motorcycle),它們都有Run、Stop等成員函數(shù)。(3)在main()函數(shù)中定義vehicle、bicycle、motorcar、motorcycle的對(duì)象,調(diào)用其Ru

4、n()、Stop()函數(shù),觀察其執(zhí)行情況。(4)分別用vehicle類型的指針來調(diào)用幾個(gè)對(duì)象的成員函數(shù),看看能否成功(提示:把Run、Stop定義為虛函數(shù))。5.編寫程序,定義抽象基類Container,由此派生出2個(gè)派生類球體類Sphere,圓柱體類Cylinder,分別用虛函數(shù)分別計(jì)算表面積和體積。(1)球體的表面積為:,球體的體積為; 圓柱表面積為: 2R(h+R) ,圓柱體的體積R2h。(2)定義相應(yīng)的對(duì)象,編寫主函數(shù)測試。6.分別聲明Teacher(教師)類和Cadre(干部)類,采用多重繼承方式由這兩個(gè)類派生出新類Teacher_Cadre(教師兼干部)。要求:在兩個(gè)基類中都包含一

5、部分相同名字的數(shù)據(jù)成員name(姓名),age(年齡)和成員函數(shù)display()。在Teacher類中還包含數(shù)據(jù)成員title(職稱),在Cadre類中還包含數(shù)據(jù)成員post(職務(wù)),在Teacher_Cadre(教師兼干部)中還包含數(shù)據(jù)成員wages(工資)。在派生類Teacher_Cadre的成員函數(shù)show中調(diào)用Teacher類中的display函數(shù),輸出姓名、年齡、職稱,然后再用cout語句輸出職務(wù)與工資。7編寫程序,定義抽象基類Shape(形狀),由它派生出3個(gè)派生類:Circle(圓形) 、Rectangle(矩形)和Triangle(三角形),用虛函數(shù)Show()分別顯示各種圖

6、形的相關(guān)信息,并計(jì)算顯示所有圖形的總面積。8編寫程序,定義抽象基類Shape(形狀),由它派生出3個(gè)派生類: Circle(圓形)、Rectangle(矩形)和Square(正方形),用虛函數(shù)ShowArea()分別顯示各種圖形的面積,并計(jì)算顯示所有圖形的總面積。要求用基類指針數(shù)組,使它的每一個(gè)元素指向一個(gè)派生類對(duì)象。3、 實(shí)驗(yàn)程序及結(jié)果1、#include<iostream>using namespace std;class pointprivate:int x,y;public:point(int,int);friend void operator+(point&);f

7、riend void operator+(point&,int);void display(); ;point:point(int a=0,int b=0):x(a),y(b)void point:display()cout<<"x="<<x<<'t'<<"y="<<y;void operator+(point& A)+A.x; +A.y;void operator+(point& A,int)+A.x; +A.y;int main()point A(1,

8、2);+A;cout<<"+A之后:"A.display();A+;cout<<'n'<<"A+之后:"A.display(); return 0;2、#include<iostream>using namespace std;class pointprivate:int x,y;public:point(int,int);point& operator+();point operator+(int);void display(); ;point:point(int a=0,int

9、b=0):x(a),y(b)void point:display()cout<<"x="<<x<<'t'<<"y="<<y;point& point:operator+()+x; +y;return *this;point point:operator+(int)+x; +y;return *this;int main()point A(1,2);+A;cout<<"+A之后:"A.display();A+;cout<<'

10、;n'<<"A+之后:"A.display();cout<<'n'return 0;3、#include<iostream.h>class fenshuint fz,fm;public:fenshu(int i=0,int j=0);void huajian();void setData()cout<<"輸入分子:"<<endl;cin>>fz;cout<<"輸入分母:"<<endl;cin>>fm;fr

11、iend fenshu operator+(fenshu &a,fenshu &b);friend fenshu operator-(fenshu &a,fenshu &b);fenshu operator*(fenshu &a);fenshu operator/(fenshu &a);fenshu &operator=(fenshu &);void show()huajian();if(fz%fm=0)cout<<(fz/fm)<<endl;elsecout<<fz<<"

12、/"<<fm<<endl;fenshu:fenshu(int i,int j)fz=i;fm=j;void fenshu:huajian()int t;while(fm=0)cout<<"分母不能為零,請(qǐng)重新輸入:"<<endl;setData();if(fz<=fm)t=fz;elset=fm;for(int i=2;i<=t;)if(fz%i=0&&fm%i=0) fz=fz/i; fm=fm/i;elsei+;fenshu operator+(fenshu &a,fenshu

13、 &b)fenshu temp;temp.fz=a.fz*b.fm+b.fz*a.fm;temp.fm=a.fm*b.fm;return temp;fenshu operator-(fenshu &a,fenshu &b)fenshu temp;temp.fz=a.fz*b.fm-b.fz*a.fm;temp.fm=a.fm*b.fm;return temp;fenshu fenshu:operator *(fenshu &a)fenshu temp;temp.fz=a.fz*fz;temp.fm=a.fm*fm;return temp;fenshu fensh

14、u:operator /(fenshu &a)fenshu temp;temp.fz=a.fz*fm;temp.fm=a.fm*fz;return temp;fenshu &fenshu:operator=(fenshu &a)fz=a.fz;fm=a.fm;return *this;void main()fenshu A,B,A1,A2,A3,A4,A5;A.setData();A.huajian();A.show();cout<<endl;B.setData();B.huajian();B.show();cout<<endl;A1=A+B;A

15、1.show();A2=A-B;A2.show();A3=A*B;A3.show();A4=A/B;A4.show();A5=A;A5.show();4、#include<iostream>using namespace std;class vehicledouble speed;public:vehicle(int a):speed(a)virtual void Run();virtual void Stop();void vehicle:Run()cout<<"speed="<<speed<<'t'void

16、 vehicle:Stop()cout<<"speed=1"class bicycle:virtual public vehicleprivate:double speed;public:virtual void Run();virtual void Stop();bicycle(int a):vehicle(a) speed=a;void bicycle:Run()cout<<"speed="<<speed<<'t'void bicycle:Stop()cout<<"

17、speed=2"class motorcar:virtual public vehicleprivate:double speed;public:virtual void Run();virtual void Stop();motorcar(int a):vehicle(a)speed=a;void motorcar:Run()cout<<"speed="<<speed<<'t'void motorcar:Stop()cout<<"speed=3"class motorcycle:

18、public bicycle, public motorcarprivate:double speed;public:void Run();void Stop();motorcycle(int a):vehicle(a),bicycle(a),motorcar(a)speed=a;void motorcycle:Run()cout<<"speed="<<speed<<'t'void motorcycle:Stop()cout<<"speed=4"int main() vehicle A(10

19、0),*p; bicycle AB(90); motorcar AC(80); motorcycle ABC(70);cout<<"A:"A.Run();A.Stop();cout<<'n'<<"AB:"AB.Run();AB.Stop();cout<<'n'<<"AC:"AC.Run();AC.Stop();cout<<'n'<<"ABC:" ABC.Run();ABC.Stop()

20、;cout<<'n'<<"用指針調(diào)用:"cout<<'n'<<"A:"p=&A;p->Run();p->Stop(); cout<<'n'<<"AB:"p=&AB;p->Run();p->Stop();cout<<'n'<<"AC:"p=&AC;p->Run();p->Stop(); cout<

21、;<'n'<<"ABC:"p=&ABC;p->Run();p->Stop();cout<<'n'<<"感謝使用本程序哦!"return 0;5、#include<iostream>using namespace std;class Containervirtual double area()=0;virtual double volume()=0;public:virtual void show()=0;class Sphere:public Con

22、tainerprivate:double r;public: Sphere(double a):r(a)double area()return 4*3.14*r*r;double volume()return (double)4/3*3.14*r*r*r;void show()cout<<"球面積為:"<<area()<<'n' cout<<"球體積為:"<<volume()<<'n'class Cylinder:public Containerpri

23、vate:double r,h;public: Cylinder(double a=0,double b=0):r(a),h(b)double area()return 2*3.14*r*(h+r);double volume()return 3.14*r*r*h;void show()cout<<"圓柱體面積為:"<<area()<<'n' cout<<"圓柱體體積為:"<<volume()<<'n'int main() Container *pr;

24、 Sphere A(1); pr=&A; pr->show(); Cylinder B(1,2); pr=&B; pr->show(); cout<<"感謝使用程序哦!"<<'n' return 0;6、#include<iostream>#include<string>using namespace std;class teacherprivate:string name;string title;int age;public: teacher(string N,string T,

25、int A):name(N),title(T),age(A)void display()cout<<"姓名:"<<name<<'t'<<"年齡:"<<age<<'t'<<"職稱:"<<title;class Cadreprivate:string name;int age;public:string post;Cadre(string N,string P,int A):name(N),post(P),age

26、(A)void display()cout<<"職務(wù):"<<post;class Teacher_Cadre:public teacher,public Cadre private:int wage;public: Teacher_Cadre(string N,string T,int A,string P,int W):teacher(N,T,A),Cadre(N,P,A)wage=W;void show()teacher:display();cout<<'n'<<"職務(wù):"<<

27、;post<<'t'<<"工資:"<<wage<<'n'int main()Teacher_Cadre A("袁東志","C+",20,"校長",5000);A.show();cout<<'n'return 0;7、#include<iostream>using namespace std;class Shapepublic: virtual void show()=0;class Circle:

28、public Shapeprivate:double r;public:Circle(double a):r(a) void show() cout<<"圓的半徑為:"<<r<<",面積為:"<<3.14*r*r<<'n' ;class Rectangle:public Shapeprivate:double a,b;public:Rectangle(double l,double h):a(l),b(h)void show()if(a>=b)cout<<&qu

29、ot;矩形的長為:"<<a<<",寬為:"<<b;else cout<<"矩形的長為:"<<b<<",寬為:"<<a;cout<<",面積為:"<<a*b<<'n'class Square:public Shapeprivate:double e;public:Square(double a):e(a)void show()cout<<"正方形的邊長

30、為:"<<e<<",面積為:"<<e*e<<'n'int main() Shape *pr; Circle A(2.0); pr=&A; pr->show(); Rectangle B(2,3); pr=&B; pr->show(); Square C(3); pr=&C; pr->show(); cout<<'n' return 0;8、#include<iostream>using namespace std;class Shapepublic: static double sumarea; static void output() cout<<"總面積為:"<<sumarea<<'n' virtual void

溫馨提示

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