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

下載本文檔

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

文檔簡(jiǎn)介

1、實(shí)驗(yàn)三 虛函數(shù)與多態(tài)、純虛函數(shù)  一.實(shí)驗(yàn)?zāi)康?. 在掌握繼承與派生關(guān)系的基礎(chǔ)上,進(jìn)一步理解虛函數(shù)與多態(tài)性的關(guān)系,實(shí)現(xiàn)運(yùn)行時(shí)的多態(tài)。 2. 學(xué)會(huì)定義和使用純虛函數(shù) 二、實(shí)驗(yàn)內(nèi)容1范例:了解"單接口,多方法"的概念?,F(xiàn)有稱(chēng)為figure的基類(lèi),存放了各二維對(duì)象(三角形、矩形和圓形三個(gè)類(lèi))的各維數(shù)據(jù),set_dim()設(shè)置數(shù)據(jù),是標(biāo)準(zhǔn)成員函數(shù)。show_area()為虛函數(shù),因?yàn)橛?jì)算各對(duì)象的面積的方法是不同的?!境绦颉?include < iostream > using namespace std;class figureprotected:d

2、ouble x,y;public:void set_dim(double i,double j=0) x=i; y=j; virtual void show_area() cout<<"No area computation defined for this class.n"class triangle:public figurepublic:void show_area() cout<<"Triangle with height "<< x<<" and base "<<

3、y<<" has an area of "<< x*0.5*y<< endl; ;class square:public figurepublic:void show_area() cout<<"Square with dimensions "<< x<<" and "<< y<<" has an area of "<< x*y<< endl; ;class circle:public figur

4、epublic: void show_area() cout<<"Circle with radius "<< x<<" has an area of "<<3.14159*x*x<< endl; ;int main()figure *p; triangle t; square s;circle c;p=&t;p->set_dim(10.0,5.0);p->show_area();p=&s;p->set_dim(10.0,5.0);p->show_area

5、();p=&c; p->set_dim(10.0);p->show_area(); return 0;  【要求】(1) 建立工程,錄入上述程序,調(diào)試運(yùn)行并記錄運(yùn)行結(jié)果。 (2) 修改上述程序,將virtual void show_area()中的virtual去掉,重新調(diào)試運(yùn)行觀察結(jié)果有何變化?為什么?在不使用關(guān)鍵字virtual后,基類(lèi)指針p對(duì)show-area的訪問(wèn)p->show_area()沒(méi)有針對(duì)所指對(duì)象的類(lèi)型調(diào)用不同的函數(shù),而是直接根據(jù)p的類(lèi)型調(diào)用了基類(lèi)的成員函數(shù)show-area。(3) 修改上述程序入口函數(shù),使其動(dòng)態(tài)建立三角形、矩形和圓形3個(gè)

6、對(duì)象,通過(guò)基類(lèi)指針訪問(wèn)這3個(gè)對(duì)象,然后釋放這3個(gè)對(duì)象。#include < iostream > using namespace std;class figureprotected:double x,y;public:void set_dim(double i,double j=0) x=i; y=j; virtual void show_area() cout<<"No area computation defined for this class.n"class triangle:public figurepublic:void show_are

7、a() cout<<"Triangle with height "<< x<<" and base "<< y<<" has an area of "<< x*0.5*y<< endl; ;class square:public figurepublic:void show_area() cout<<"Square with dimensions "<< x<<" and "&

8、lt;< y<<" has an area of "<< x*y<< endl; ;class circle:public figurepublic: void show_area() cout<<"Circle with radius "<< x<<" has an area of "<<3.14159*x*x<< endl; ;int main()figure *p;triangle *p1;square *p2;circle *

9、p3;p1=new triangle;p2=new square;p3=new circle;p=p1;p->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) 修改類(lèi)定義中的析構(gòu)函數(shù),使之適應(yīng)用戶動(dòng)態(tài)定義對(duì) 2、使用純虛函數(shù)和抽象類(lèi)對(duì)實(shí)驗(yàn)二中的題1進(jìn)行改進(jìn)。 #include < iostream >using

10、 namespace std;class figureprotected:double x,y;public:void set_dim(double i,double j=0) x=i; y=j; virtual void show_area() cout<<"No area computation defined for this class.n"/在此處將基類(lèi)的析構(gòu)函數(shù)聲明為虛析構(gòu)就OK了virtual figure()/;class triangle:public figurepublic:void show_area() cout<<&quo

11、t;Triangle with height "<< x<<" and base "<< y<<" has an area of "<< x*0.5*y<< endl; ;class square:public figurepublic:void show_area() cout<<"Square with dimensions "<< x<<" and "<< y<<&qu

12、ot; has an area of "<< x*y<< endl; ;class circle:public figurepublic: void show_area() cout<<"Circle with radius "<< x<<" has an area of "<<3.14159*x*x<< endl; ;int main()figure *p; triangle t; square s;circle c;p=&t;p->set_di

13、m(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; 2、編程:自定義一個(gè)抽象類(lèi)Element,提供顯示、求面積等公共接口(虛函數(shù)),派生出Point、Line、Circle等圖形元素類(lèi),并重新定義(override)這些虛函數(shù),完成各自的任務(wù)。在這里,Element是抽象基類(lèi),它不能提供具體的顯示操作,應(yīng)將其成員函數(shù)定義為純虛函數(shù)。只有采用指向基類(lèi)的指針或?qū)?lèi)的引用進(jìn)

14、行調(diào)用,實(shí)現(xiàn)的才是動(dòng)態(tài)綁定,完成運(yùn)行時(shí)的多態(tài)性。#include < iostream > using namespace std;const double PI=3.14159;class Elementpublic:virtual const char* Name() const =0;virtual double Area() const =0;class Point:public Elementprotected:double x,y;public:Point(double xv=0,double yv=0):x(xv),y(yv)virtual const char* N

15、ame() 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 double Area() constreturn

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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)論