實驗三虛函數(shù)與多態(tài)純虛函數(shù)_第1頁
實驗三虛函數(shù)與多態(tài)純虛函數(shù)_第2頁
實驗三虛函數(shù)與多態(tài)純虛函數(shù)_第3頁
實驗三虛函數(shù)與多態(tài)純虛函數(shù)_第4頁
實驗三虛函數(shù)與多態(tài)純虛函數(shù)_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、-. z實驗三虛函數(shù)與多態(tài)、純虛函數(shù)一.實驗?zāi)康?. 在掌握繼承與派生關(guān)系的根底上,進(jìn)一步理解虛函數(shù)與多態(tài)性的關(guān)系,實現(xiàn)運(yùn)行時的多態(tài)。2. 學(xué)會定義和使用純虛函數(shù)二、實驗容1例:了解單接口,多方法的概念?,F(xiàn)有稱為figure的基類,存放了各二維對象三角形、矩形和圓形三個類的各維數(shù)據(jù),set_dim()設(shè)置數(shù)據(jù),是標(biāo)準(zhǔn)成員函數(shù)。show_area()為虛函數(shù),因為計算各對象的面積的方法是不同的。【程序】#include using namespace std;class figureprotected:double *,y;public:void set_dim(double i,double

2、j=0) *=i; y=j; virtual void show_area() coutNo area putation defined for this class.n;class triangle:public figurepublic:void show_area() coutTriangle with height * and base y has an area of *0.5*y endl; ;class square:public figurepublic:void show_area() coutSquare with dimensions * and y has an are

3、a of *y endl; ;class circle:public figurepublic: void show_area() coutCircle with radius * has an area of 3.14159*set_dim(10.0,5.0);p-show_area();p=&s;p-set_dim(10.0,5.0);p-show_area();p=&c; p-set_dim(10.0);p-show_area(); return 0; 【要求】建立工程,錄入上述程序,調(diào)試運(yùn)行并記錄運(yùn)行結(jié)果。修改上述程序,將virtual void show_area()中的virtua

4、l去掉,重新調(diào)試運(yùn)行觀察結(jié)果有何變化?為什么?在不使用關(guān)鍵字virtual后,基類指針p對show-area的p-show_area()沒有針對所指對象的類型調(diào)用不同的函數(shù),而是直接根據(jù)p的類型調(diào)用了基類的成員函數(shù)show-area。修改上述程序入口函數(shù),使其動態(tài)建立三角形、矩形和圓形3個對象,通過基類指針這3個對象,然后釋放這3個對象。#include using namespace std;class figureprotected:double *,y;public:void set_dim(double i,double j=0) *=i; y=j; virtual void show

5、_area() coutNo area putation defined for this class.n;class triangle:public figurepublic:void show_area() coutTriangle with height * and base y has an area of *0.5*y endl; ;class square:public figurepublic:void show_area() coutSquare with dimensions * and y has an area of *y endl; ;class circle:publ

6、ic figurepublic: void show_area() coutCircle with radius * has an area of 3.14159*set_dim(10.0,5.0);p-show_area();p=p2;p-set_dim(10.0,5.0);p-show_area();p=p3;p-set_dim(10.0);p-show_area();delete p1;delete p2;delete p3;4修改類定義中的析構(gòu)函數(shù),使之適應(yīng)用戶動態(tài)定義對 2、使用純虛函數(shù)和抽象類對實驗二中的題1進(jìn)展改良。#include using namespace std;cla

7、ss figureprotected:double *,y;public:void set_dim(double i,double j=0) *=i; y=j; virtual void show_area() coutNo area putation defined for this class.n;/在此處將基類的析構(gòu)函數(shù)聲明為虛析構(gòu)就OK了virtual figure()/;class triangle:public figurepublic:void show_area() coutTriangle with height * and base y has an area of *0.

8、5*y endl; ;class square:public figurepublic:void show_area() coutSquare with dimensions * and y has an area of *y endl; ;class circle:public figurepublic: void show_area() coutCircle with radius * has an area of 3.14159*set_dim(10.0,5.0);p-show_area();p=&s;p-set_dim(10.0,5.0);p-show_area();p=&c; p-s

9、et_dim(10.0);p-show_area(); return 0; 2、編程:自定義一個抽象類Element,提供顯示、求面積等公共接口虛函數(shù),派生出Point、Line、Circle等圖形元素類,并重新定義override這些虛函數(shù),完成各自的任務(wù)。在這里,Element是抽象基類,它不能提供具體的顯示操作,應(yīng)將其成員函數(shù)定義為純虛函數(shù)。只有采用指向基類的指針或?qū)惖囊眠M(jìn)展調(diào)用,實現(xiàn)的才是動態(tài)綁定,完成運(yùn)行時的多態(tài)性。#include using namespace std;const double PI=3.14159;class Elementpublic:virtual c

10、onst char* Name() const =0;virtual double Area() const =0;class Point:public Elementprotected:double *,y;public:Point(double *v=0,double yv=0):*(*v),y(yv)virtual const char* Name() constreturn Point;virtual double Area() constreturn 0;class Line:public Elementprotected:double length;public:Line(double l=0):length(l)virtual const char* Name() constreturn Line;virtual double Area() constreturn 0;class Circle:public Elementprotected:double radius;public:Circle(double r):radius(r)virtual const char* Name() constreturn Circle;virtual doub

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論