【第四版課后習(xí)題答案】第08章多態(tài)性_第1頁
【第四版課后習(xí)題答案】第08章多態(tài)性_第2頁
【第四版課后習(xí)題答案】第08章多態(tài)性_第3頁
【第四版課后習(xí)題答案】第08章多態(tài)性_第4頁
【第四版課后習(xí)題答案】第08章多態(tài)性_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

多態(tài)性8-1什么叫做多態(tài)性?在C++中是如何實現(xiàn)多態(tài)的?解:多態(tài)是指同樣的消息被不同類型的對象接收時導(dǎo)致完全不同的行為,是對類的特定成員函數(shù)的再抽象。C++支持的多態(tài)有多種類型,重載(包括函數(shù)重載和運算符重載)和虛函數(shù)是其中主要的方式。8-2什么叫做抽象類?抽象類有何作用?抽象類的派生類是否一定要給出純虛函數(shù)的實現(xiàn)?解:帶有純虛函數(shù)的類是抽象類。抽象類的主要作用是通過它為一個類族建立一個公共的接口,使它們能夠更有效地發(fā)揮多態(tài)特性。抽象類聲明了一組派生類共同操作接口的通用語義,而接口的完整實現(xiàn),即純虛函數(shù)的函數(shù)體,要由派生類自己給出。但抽象類的派生類并非一定要給出純虛函數(shù)的實現(xiàn),如果派生類沒有給出純虛函數(shù)的實現(xiàn),這個派生類仍然是一個抽象類。8-3聲明一個參數(shù)為整型,無返回值,名為fn1的虛函數(shù)。解:virtualvoidfn1(int);8-4在C++中,能否聲明虛構(gòu)造函數(shù)?為什么?能否聲明虛析構(gòu)函數(shù)?有何用途?解:在C++中,不能聲明虛構(gòu)造函數(shù),多態(tài)是不同的對象對同一消息有不同的行為特性,虛函數(shù)作為運行過程中多態(tài)的基礎(chǔ),主要是針對對象的,而構(gòu)造函數(shù)是在對象產(chǎn)生之前運行的,因此虛構(gòu)造函數(shù)是沒有意義的;可以聲明虛析構(gòu)函數(shù),析構(gòu)函數(shù)的功能是在該類對象消亡之前進行一些必要的清理工作,如果一個類的析構(gòu)函數(shù)是虛函數(shù),那么,由它派生而來的所有子類的析構(gòu)函數(shù)也是虛函數(shù)。析構(gòu)函數(shù)設(shè)置為虛函數(shù)之后,在使用指針引用時可以動態(tài)聯(lián)編,實現(xiàn)運行時的多態(tài),保證使用基類的指針就能夠調(diào)用適當(dāng)?shù)奈鰳?gòu)函數(shù)針對不同的對象進行清理工作。8-5實現(xiàn)重載函數(shù)Double(x),返回值為輸入?yún)?shù)的兩倍;參數(shù)分別為整型、長整型、浮點型、雙精度型,返回值類型與參數(shù)一樣。解:源程序:#include<iostream.h>intDouble(int);longDouble(long);floatDouble(float);doubleDouble(double);intmain(){intmyInt=6500;longmyLong=65000;floatmyFloat=6.5F;doublemyDouble=6.5e20;intdoubledInt;longdoubledLong;floatdoubledFloat;doubledoubledDouble;cout<<"myInt:"<<myInt<<"\n";cout<<"myLong:"<<myLong<<"\n";cout<<"myFloat:"<<myFloat<<"\n";cout<<"myDouble:"<<myDouble<<"\n";doubledInt=Double(myInt);doubledLong=Double(myLong);doubledFloat=Double(myFloat);doubledDouble=Double(myDouble);cout<<"doubledInt:"<<doubledInt<<"\n";cout<<"doubledLong:"<<doubledLong<<"\n";cout<<"doubledFloat:"<<doubledFloat<<"\n";cout<<"doubledDouble:"<<doubledDouble<<"\n";return0;}intDouble(intoriginal){cout<<"InDouble(int)\n";return2*original;}longDouble(longoriginal){cout<<"InDouble(long)\n";return2*original;}floatDouble(floatoriginal){cout<<"InDouble(float)\n";return2*original;}doubleDouble(doubleoriginal){cout<<"InDouble(double)\n";return2*original;}程序運行輸出:myInt:6500myLong:65000myFloat:6.5myDouble:6.5e+20InDouble(int)InDouble(long)InDouble(float)InDouble(double)DoubledInt:13000DoubledLong:130000DoubledFloat:13DoubledDouble:1.3e+218-6定義一個Rectangle類,有長itsWidth、寬itsLength等屬性,重載其構(gòu)造函數(shù)Rectangle。和Rectangle(intwidth,intlength)。解:源程序:#include<iostream.h>classRectangle{public:Rectangle();Rectangle(intwidth,intlength);~Rectangle(){}intGetWidth()const{returnitsWidth;}intGetLength()const{returnitsLength;}private:intitsWidth;intitsLength;};Rectangle::Rectangle(){itsWidth=5;itsLength=10;}Rectangle::Rectangle(intwidth,intlength){itsWidth=width;itsLength=length;}intmain(){RectangleRect1;cout<<"Rect1width:"<<Rect1.GetWidth()<<endl;cout<<"Rect1length:"<<Rect1.GetLength()<<endl;intaWidth,aLength;cout<<"Enterawidth:";cin>>aWidth;cout<<"\nEnteralength:";cin>>aLength;RectangleRect2(aWidth,aLength);cout<<"\nRect2width:"<<Rect2.GetWidth()<<endl;cout<<"Rect2length:"<<Rect2.GetLength()<<endl;return0;}程序運行輸出:Rect1width:5Rect1length:10Enterawidth:20Enteralength:50Rect2width:20Rect2length:508-7定義計數(shù)器Counter類,對其重載運算符+。解:源程序:typedefunsignedshortUSHORT;#include<iostream.h>classCounter{public:Counter();Counter(USHORTinitialValue);~Counter(){}USHORTGetItsVal()const{returnitsVal;}voidSetItsVal(USHORTx){itsVal=x;}Counteroperator+(constCounter&);private:USHORTitsVal;};Counter::Counter(USHORTinitialValue):itsVal(initialValue){}Counter::Counter():itsVal(0){}CounterCounter::operator+(constCounter&rhs){returnCounter(itsVal+rhs.GetItsVal());}intmain(){CountervarOne(2),varTwo(4),varThree;varThree=varOne+varTwo;cout<<"varOne:"<<varOne.GetItsVal()<<endl;cout<<"varTwo:"<<varTwo.GetItsVal()<<endl;cout<<"varThree:"<<varThree.GetItsVal()<<endl;return0;}程序運行輸出:varOne:2varTwo:4varThree:68-5定義一個哺乳動物Mammal類,再由此派生出狗Dog類,二者都定義Speak()成員函數(shù),基類中定義為虛函數(shù),定義一個Dog類的對象,調(diào)用Speak函數(shù),觀察運行結(jié)果。解:源程序:#include<iostream.h>classMammal{public:Mammal(){cout<<"Mammalconstructor...\n";}~Mammal(){cout<<"Mammaldestructor...\n";}virtualvoidSpeak()const{cout<<"Mammalspeak!\n";}};classDog:publicMammal{public:Dog(){cout<<"DogConstructor...\n";}~Dog(){cout<<"Dogdestructor...\n";}voidSpeak()const{cout<<"Woof!\n";}};intmain(){Mammal*pDog=newDog;pDog->Speak();return0;}程序運行輸出:Mammalconstructor...Dogconstructor...Woof!Dogdestructor...Mammaldestructor...8-6定義一個Shape抽象類,在此基礎(chǔ)上派生出Rectangle和Circle,二者都有GetArea()函數(shù)計算對象的面積,GetPerim()函數(shù)計算對象的周長。解:源程序:#include<iostream.h>classShape{public:Shape(){}~Shape(){}virtualfloatGetArea()=0;virtualfloatGetPerim()=0;};classCircle:publicShape{public:Circle(floatradius):itsRadius(radius){}~Circle(){}floatGetArea(){return3.14*itsRadius*itsRadius;}floatGetPerim(){return6.28*itsRadius;}private:floatitsRadius;};classRectangle:publicShape{public:Rectangle(floatlen,floatwidth):itsLength(len), itsWidth(width){};~Rectangle(){};virtualfloatGetArea(){returnitsLength*itsWidth;}floatGetPerim(){return2*itsLength+2*itsWidth;}virtualfloatGetLength(){returnitsLength;}virtualfloatGetWidth(){returnitsWidth;}private:floatitsWidth;floatitsLength;};voidmain(){Shape*sp;sp=newCircle(5);cout<<"TheareaoftheCircleis"<<sp->GetArea()<<endl;cout<<"TheperimeteroftheCircleis"<<sp->GetPerim()<<endl;deletesp;sp=newRectangle(4,6);cout<<"TheareaoftheRectangleis"<<sp->GetArea()<<endl;cout<<"TheperimeteroftheRectangleis"<<sp->GetPerim()<<endl;deletesp;}程序運行輸出:TheareaoftheCircleis78.5TheperimeteroftheCircleis31.4TheareaoftheRectangleis24TheperimeteroftheRectangleis208-7對Point類重載++(自增)、--(自減)運算符解:#include<iostream.h>classPoint{public:Point&operator++();Pointoperator++(int);Point&operator--();Pointoperator--(int);Point(){_x=_y=0;}intx(){return_x;}inty(){return_y;}private:int_x,_y;};Point&Point::operator++(){_x++;_y++;return*this;}PointPoint::operator++(int){Pointtemp=*this;++*this;returntemp;}Point&Point::operator--(){_x--;_y--;return*this;}PointPoint::operator--(int){Pointtemp=*this;--*this;returntemp;}voidmain(){PointA;cout<<"A的值為:"<<A.x()<<","<<A.y()<<endl;A++;cout<<"A的值為:"<<A,x()<<","<<A,y()<<endl;++A;cout<<"A的值為:"<<A,x()<<","<<A,y()<<endl;A--;cout<<"A的值為:"<<A,x()<<","<<A,y()<<endl;--A;cout<<"A的值為:"<<A,x()<<","<<A,y()<<endl;}程序運行輸出:A的值為:0,0A的值為:1,1A的值為:2,2A的值為:1,1A的值為:0,08-8定義一個基類BaseClass,從它派生出類DerivedClass,BaseClass有成員函數(shù)fn1()、fn2(),fn1()是虛函數(shù),DerivedClass也有成員函數(shù)fn1()、fn2(),在主程序中定義一個DerivedClass的對象,分別用BaseClass和DerivedClass的指針來調(diào)用fn1()、fn2(),觀察運行結(jié)果。解:#include<iostream.h>classBaseClass{public:virtualvoidfn1();voidfn2();};voidBaseClass::fn1(){cout<<"調(diào)用基類的虛函數(shù)fn1()"<<endl;}voidBaseClass::fn2(){cout<<"調(diào)用基類的非虛函數(shù)fn2()"<<endl;}classDerivedClass:publicBaseClass{public:voidfn1();voidfn2();};voidDerivedClass::fn1(){cout<<"調(diào)用派生類的函數(shù)fn1()"<<endl;}voidDerivedClass::fn2(){cout<<"調(diào)用派生類的函數(shù)fn2()"<<endl;}voidmain(){DerivedClassaDerivedClass;DerivedClass*pDerivedClass=&aDerivedClass;BaseClass*pBaseClass=&aDerivedClass;pBaseClass->fn1();pBaseClass->fn2();pDerivedClass->fn1();pDerivedClass->fn2();}程序運行輸出:調(diào)用派生類的函數(shù)fn1()調(diào)用基類的非虛函數(shù)fn2()調(diào)用派生類的函數(shù)fn1()調(diào)用派生類的函數(shù)fn2()8-9定義一個基類BaseClass,從它派生出類DerivedClass,BaseClass中定義虛析構(gòu)函數(shù),在主程序中將一個DerivedClass的對象地址賦給一個BaseClass的指針,觀察運行過程。解:#include<iostream.h>classBaseClass{public:virtual~BaseClass(){cout<<"~BaseClass()"<<endl;}};classDerivedClass:publicBaseClass{public:~DerivedClass(){cout<<"~DerivedClass()"<<endl;}};voidmain(){BaseClass*bp=newDerivedClas

溫馨提示

  • 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. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論