




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第八章多態性
8-1什么叫做多態性?在C++中是如何實現多態的?
解:
多態是指同樣的消息被不同類型的對象接收時導致完全不同的行為,是對類的特定成員函數的再抽象。C++
支持的多態有多種類型,重載(包括函數重載和運算符重載)和虛函數是其中主要的方式。
8-2什么叫做抽象類?抽象類有何作用?抽象類的派生類是否一定要給出純虛函數的實現?
解:
帶有純虛函數的類是抽象類。抽象類的主要作用是通過它為一個類族建立一個公共的接口,使它們能夠更
有效地發揮多態特性。抽象類聲明了一組派生類共同操作接口的通用語義,而接口的完整實現,即純虛函
數的函數體,要由派生類自己給出。但抽象類的派生類并非一定要給出純虛函數的實現,如果派生類沒有
給出純虛函數的實現,這個派生類仍然是一個抽象類。
8-3聲明一個參數為整型,無返回值,名為fnl的虛函數。
解:
virtualvoidfnl(int);
8-4在C++中,能否聲明虛構造函數?為什么?能否聲明虛析構函數?有何用途?
解:
在C++中,不能聲明虛構造函數,多態是不同的對象對同一消息有不同的行為特性,虛函數作為運行過程
中多態的基礎,主要是針對對象的,而構造函數是在對象產生之前運行的,因此虛構造函數是沒有意義的;
可以聲明虛析構函數,析構函數的功能是在該類對象消亡之前進行一些必要的清理工作,如果一個類的析
構函數是虛函數,那么,由它派生而來的所有子類的析構函數也是虛函數。析構函數設置為虛函數之后,
在使用指針引用時可以動態聯編,實現運行時的多態,保證使用基類的指針就能夠調用適當的析構函數針
對不同的對象進行清理工作。
8-5實現重載函數Double(x),返回值為輸入參數的兩倍;參數分別為整型、長整型、浮點型、雙精度型,
返回值類型與參數一樣。
解:
源程序:
#include<iostream.h>
intDouble(int);
longDouble(long);
floatDouble(float);
doubleDouble(double);
intmain()
intmylnt=6500;
longmyLong=65000;
floatmyFloat=6.5F;
doublemyDouble=6.5e20;
intdoubledint;
longdoubledLong;
floatdoubledFloat;
doubledoubledDouble;
cout?"mylnt:“?mylnt?"\n”;
cout?"myLong:"?myLong?"\n”;
cout?"myFloat:H?myFloat?"\nn;
cout?"myDouble:?myDouble?"\n";
doubledint=Double(mylnt);
doubledLong=Double(myLong);
doubledFloat=Double(myFloat);
doubledDouble=Double(myDouble);
cout?"doubledint:"?doubledint?u\n";
cout?"doubledLong:n?doubledLong?"\n";
cout?''doubledFloat:M?doubledFloat?n\nn;
cout?"doubledDouble:11?doubledDouble?"\n";
return0;
)
intDouble(intoriginal)
(
cout?"InDouble(int)\n";
return2*original;
)
longDouble(longoriginal)
{
cout?"InDouble(long)\nn;
return2*original;
)
floatDouble(floatoriginal)
{
cout?"InDouble(float)\nn;
return2*original;
)
doubleDouble(doubleoriginal)
(
cout?"InDouble(double)\nn;
return2*original;
}
程序運行輸出:
mylnt:6500
myLong:65000
myFloat:6.5
myDouble:6.5e+20
InDouble(int)
InDouble(long)
InDouble(float)
InDouble(double)
Doubledint:13000
DoubledLong:130000
DoubledFloat:13
DoubledDouble:1.3e+21
8-6定義一個Rectangle類,有長itsWidth>寬itsLength等屬性,重載其構造函數Rectangle。和Rectangle(int
width,intlength)o
解:
源程序:
#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()
{
RectangleRecti;
cout?nRectlwidth:"?Recti.GetWidth()?endl;
cout?nRectllength:"?Rectl.GetLength()?end);
intaWidth,aLength;
cout?”Emerawidth:";
cin?aWidth;
cout?"\nEnteralength:";
cin?aLength;
RectangleRect2(aWidth,aLength);
cout?n\nRect2width:M?Rect2.GetWidth()?endl;
cout?"Rect2length:n?Rect2.GetLength()?endl;
return0;
)
程序運行輸出:
Rectiwidth:5
Rectilength:10
Enterawidth:20
Enteralength:50
Rect2width:20
Rect2length:50
8-7定義計數器Counter類,對其重載運算符+。
解:
源程序:
typedefunsignedshortUSHORT;
#include<iostream.h>
classCounter
(
public:
Counter();
Counter(USHORTinitialValue);
-Counter(){}
USHORTGetItsVal()const{returnitsVai;}
voidSetItsVal(USHORTx){itsVai=x;}
Counteroperator+(constCounter&);
private:
USHORTitsVai;
);
Counter::Counter(USHORTinitialValue):
itsVal(initialValue)
Counter::Counter():
itsVal(O)
{
)
CounterCounter::operator+(constCounter&rhs)
(
returnCounter(itsVal+rhs.GetltsValO);
}
intmain()
CountervarOne(2),varTwo(4),varThree;
varThree=varOne+varTwo;
cout?nvarOne:"?varOne.GetItsVal()?endl;
cout?"varTwo:M?varTwo.GetItsVal()?endl;
cout?"varThree:*'?varThree.GetItsVal()?endl;
return0;
)
程序運行輸出:
varOne:2
varTwo:4
varThree:6
8-5定義一個哺乳動物Mammal類,再由此派生出狗Dog類,二者都定義Speak。成員函數,基類中定義
為虛函數,定義一個Dog類的對象,調用Speak函數,觀察運行結果。
解:
源程序:
#include<iostream.h>
classMammal
(
public:
Mammal(){cout?”Mammalconstructor..An";}
?Mammal。{cout?"Mammaldestructor...\n";}
virtualvoidSpeak()const{cout?"Mammalspeak!\n'*;}
);
classDog:publicMammal
(
public:
Dog(){cout?"DogConstructor...\nu;}
?Dog(){cout?"Dogdestructor...\n";}
voidSpeak()const{cout?MWoof!\n";}
);
intmain()
(
Mammal*pDog=newDog;
pDog->Speak();
return0;
)
程序運行輸出:
Mammalconstructor...
Dogconstructor...
Woof!
Dogdestructor...
Mammaldestructor...
8-6
定義一個Shape抽象類,在此基礎上派生出Rectangle和Circle,二者都有GetArea。函數計算對象的面積,
GetPerim()函數計算對象的周長。
解:
源程序:
#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),itsWi出h(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.5
TheperimeteroftheCircleis31.4
TheareaoftheRectangleis24
TheperimeteroftheRectangleis20
8-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=%his;
++*this;
returntemp;
)
Point&Point::operator-()
(
_x-;
return*this;
)
PointPoint:operator—(int)
(
Pointtemp=*this;
—*this;
returntemp;
}
voidmain()
(
PointA;
cout?"A的值為:"?A.x()?",M?A.y()?endl;
A++;
cout?”A的值為:n?A.x()?n,H?A.y()?endl;
++A;
cout?"A的值為:n?A.x()?“,"vvA.y()?endl;
A--;
cout?"A的值為:"?A.x()?",M?A,y()?endl;
—A;
cout?"A的值為:"?A.x()?*',n?A.y()?endl;
)
程序運行輸出:
A的值為:0,0
A的值為:1,1
A的值為:2,2
A的值為:1,1
A的值為:0,0
8-8
定義一個基類BaseClass,從它派生出類DerivedClass,BaseClass有成員函數fn1()、fn2(),fnl()是虛函數,
DerivedClass也有成員函數fnl()、fn2(),在主程序中定義一個DerivedClass的對象,分別用BaseClass和
DerivedClass的指針來調用fnl()>fn2(),觀察運行結果。
解:
#include<iostream.h>
classBaseClass
(
public:
virtualvoidfnl();
voidfn2();
);
voidBaseClass::fnl()
(
cout?”調用基類的虛函數fnl()u?endl;
)
voidBaseClass::fn2()
cout?"調用基類的非虛函數fn2()"?endl;
classDerivedClass:publicBaseClass
(
public:
voidfnl();
voidfn2();
);
voidDerivedClass::fnl()
(
cout?”調用派生類的函數fnl()u?endl;
)
voidDerivedClass::fn2()
(
cout?”調用派生類的函數fn2()u?endl;
)
voidmain()
(
DerivedClassaDerivedClass;
DerivedClass*pDerivedClass=&aDerivedClass;
BaseClass*pBaseClass=&aDerivedClass;
pBaseClass->fnl();
pBaseClass->fn2();
pDerivedClass->fn1();
pDerivedClass->fn2();
)
程序運行輸出:
調用派生類的函數fnl()
調用基類的非虛函數fn2()
調用派生類的函數fnl()
調用派生類的函數fn2()
8-9
定義一個基類BaseClass,從它派生出類DerivedClass,BaseClass中定義虛析構函數,在主程序中將一個
DerivedClass的對象地址賦給一個BaseClass的指針,觀察運行過程。
解:
#include<iostream.h>
classBaseClass{
public:
virtual-BaseClass(){
cout?"?BaseClass。"<<endl;
);
classDerivedClass:publicBaseClass{
public:
?DerivedClass。{
cout?"-DerivedClassO1'?endl;
)
);
voidmain()
(
BaseClass*bp=newDerivedCla
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年血氧飽和度分析儀項目建議書
- 國際游戲市場拓展與本土化運營策略調整合同
- 農業產業股權投資協議(SPA)-精準農業技術應用
- 電商平臺網店債權債務清理及代償協議
- 烘焙行業品牌授權保密補充合同
- 高端論壇私人保鏢住宿與參會安全合同
- 精美影視作品全網獨播權授權合同
- 八大浪費培訓
- 藝術替身薪酬保密協議及隱私保護服務條款
- Web前端開發技術項目教程(HTML5 CSS3 JavaScript)(微課版) 課件 6.1任務引入 制作非遺項目申報指南區域
- 2025-2030年中國緩釋和和控釋肥料行業市場現狀供需分析及投資評估規劃分析研究報告
- 2025年河北省秦皇島市海港區中考一模數學試卷(原卷版+解析版)
- 衛生法律法規的試題及答案
- 2025年注冊測繪師考試測繪地理信息數據處理與應用試題
- 2025屆湖北省黃岡市黃州中學高考生物三模試卷含解析
- 二手車貨車合同協議書
- 2024-2025部編版小學道德與法治二年級下冊期末考試卷及答案
- 測井試題及答案完整版
- 人格性格測試題及答案
- 2025-2030年中國電子變壓器市場運行前景及投資價值研究報告
- 山東某年產10萬噸甲醇工程施工組織設計(土建 安裝)
評論
0/150
提交評論