版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、-. z實驗三虛函數(shù)與多態(tài)、純虛函數(shù)一.實驗?zāi)康?. 在掌握繼承與派生關(guān)系的根底上,進一步理解虛函數(shù)與多態(tài)性的關(guān)系,實現(xiàn)運行時的多態(tài)。2. 學(xué)會定義和使用純虛函數(shù)二、實驗容1例:了解單接口,多方法的概念。現(xiàn)有稱為figure的基類,存放了各二維對象三角形、矩形和圓形三個類的各維數(shù)據(jù),set_dim()設(shè)置數(shù)據(jù),是標準成員函數(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)試運行并記錄運行結(jié)果。修改上述程序,將virtual void show_area()中的virtua
4、l去掉,重新調(diào)試運行觀察結(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進展改良。#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展調(diào)用,實現(xiàn)的才是動態(tài)綁定,完成運行時的多態(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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 砂漿廠安全生產(chǎn)制度范本
- 農(nóng)產(chǎn)品生產(chǎn)檔案記錄制度
- 衛(wèi)生室安全生產(chǎn)工作制度
- 鄭煤集團生產(chǎn)技術(shù)部制度
- 外包單位安全生產(chǎn)制度
- 塑料袋生產(chǎn)車間管理制度
- 瓦檢隊文明生產(chǎn)責(zé)任制度
- 質(zhì)檢部安全生產(chǎn)責(zé)任制度
- 年度安全生產(chǎn)目標管理制度
- 亞克力制品加工廠生產(chǎn)流程制度
- 2024數(shù)控機床主軸可靠性加速試驗技術(shù)規(guī)范
- 水庫除險加固工程施工組織設(shè)計
- 質(zhì)量信得過班組培訓(xùn)課件
- 材料進場檢驗記錄表
- DL∕T 1768-2017 旋轉(zhuǎn)電機預(yù)防性試驗規(guī)程
- 復(fù)方蒲公英注射液在銀屑病中的應(yīng)用研究
- 網(wǎng)絡(luò)直播創(chuàng)業(yè)計劃書
- 大學(xué)任課老師教學(xué)工作總結(jié)(3篇)
- 3D打印增材制造技術(shù) 課件 【ch01】增材制造中的三維模型及數(shù)據(jù)處理
- 醫(yī)院保潔應(yīng)急預(yù)案
- 化工設(shè)備培訓(xùn)
評論
0/150
提交評論