c程序設(shè)計(jì)教程羅建軍 22080-00ch12_第1頁
c程序設(shè)計(jì)教程羅建軍 22080-00ch12_第2頁
c程序設(shè)計(jì)教程羅建軍 22080-00ch12_第3頁
c程序設(shè)計(jì)教程羅建軍 22080-00ch12_第4頁
c程序設(shè)計(jì)教程羅建軍 22080-00ch12_第5頁
已閱讀5頁,還剩45頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、國家級精品課程網(wǎng)站(本書配套教學(xué)網(wǎng)站)4812.1 多態(tài)性概述第12章 多態(tài)性12.2 派生類對象替換基類對象12.3 虛函數(shù)12.4 抽象類12.5 運(yùn)算符重載程序設(shè)計(jì)舉例11.1 多態(tài)性概述多態(tài)性面向?qū)ο蟪绦蛟O(shè)計(jì)的關(guān)鍵技術(shù)之一。利用多態(tài)性技術(shù),可以調(diào)用同名函數(shù),實(shí)現(xiàn)完全不同的功能分類:(1) 編譯時(shí)的多態(tài)性:通過函數(shù)的重載和運(yùn)算符的重載來實(shí)現(xiàn)的。(2) 運(yùn)行時(shí)的多態(tài)性:是指在程序執(zhí)行前,無法根據(jù)函數(shù)名和參數(shù)來確定該調(diào)用哪一個(gè) 函數(shù),必須在程序執(zhí)行過程中,根據(jù)執(zhí)行的具體情況來動態(tài)地確定。它是通過類繼承關(guān)系和虛函數(shù)來實(shí)現(xiàn)的編譯時(shí)多態(tài)性函

2、數(shù)重載(兔子逃生)偽代碼描述:class兔子public:void逃生(老鷹a)“兔子蹬鷹”; void逃生(狼b)“動如脫兔”;基類和派生類的同名成員class 寵 物public:void speak() cout zzz; ;class 貓: public 寵物public:void speak() cout miao!miao!; ;class 狗: public寵物public:void speak() coutspeak();11.2 派生類對象替換基類對象原則凡是基類對象出現(xiàn)的場合都可以用公有派生類對象取代對象替換常用的形式(1) 派生類對象給基類對象賦值(2) 派生類對象可以初

3、始化基類對象的引用(3) 可以令基類對象的指針指向派生類對象,即將派生類對象的地址傳遞給基類指針例12-1 派生類對象替換基類對象#include using namespace std;classPet/基類public:void Speak() coutHow does a pet speak ?endl; ;classCat : public Pet/派生類public:void Speak() coutmiao!miao!endl; ;classDog : public Pet/派生類public:void Speak() coutwang!wang!Speak();p1 = &dog

4、1;/用Dog類對象地址給基類指針賦值p1-Speak();Pet&p4 = cat1;/以Cat類對象初始化Pet類引用p4 .Speak(); return 0;11.3 虛函數(shù)一、虛函數(shù)的定義定義:在某基類中聲明為virtual并在一個(gè)或多個(gè)派生類中被重新定義的 成員函數(shù)語法:virtual 函數(shù)返回類型 函數(shù)名(參數(shù)表)用途:函數(shù)體實(shí)現(xiàn)多態(tài)性,通過指向派生類的基類指針,訪問派生類中同名覆 蓋成員函數(shù),例12-2虛函數(shù)實(shí)現(xiàn)多態(tài)性#include using namespace std;class Pet/基類public:virtual void Speak() coutHow does

5、 a pet speak ?endl; ;class Cat : public Pet/派生類public:virtual;void Speak() coutmiao!miao!endl; class Dog : public Pet/派生類public:virtual;void Speak() coutwang!wang!Speak();p1 = &dog1;/用Dog類對象地址給基類指針賦值p1-Speak();Pet&p4 = cat1;/以Cat類對象初始化Pet類引用p4 .Speak();return 0;二、虛函數(shù)的使用限制1、應(yīng)通過指針或引用調(diào)用虛函數(shù),而不要以對象名調(diào)用虛函數(shù)

6、說明語句2、在派生類中重定義的基類虛函數(shù)仍為虛函數(shù), 同時(shí)可以省略virtual關(guān)鍵字3、不能定義虛構(gòu)造函數(shù),可以定義虛析構(gòu)函數(shù)1、純虛函數(shù)11.4 抽象類(1) 是指沒有在基類中定義的虛函數(shù)(2) 虛函數(shù)的具體實(shí)現(xiàn)依賴于不同的派生類。(3) 定義純虛函數(shù)的一般格式為:virtual返回類型函數(shù)名(參數(shù)表)=0;純虛函數(shù)沒有實(shí)現(xiàn)部分,不能產(chǎn)生對象,所以含有純虛函數(shù)的類是抽象類。(1) 定義2、抽象類至少包含一個(gè)純虛函數(shù)的類稱為抽象類(2) 使用抽象類的要求抽象類不能實(shí)例化抽象類只作為基類被繼承可以定義指向抽象類的指針或引用例12-3抽象寵物類的實(shí)現(xiàn)#include #include using

7、 namespace std;class Pet/基類char int charName20; Age; Color12;public:charType10;Pet(char *,int ,char *);char* GetName() return Name; intGetAge() return Age;char* GetColor() return Color; virtual void Speak() = 0;virtual void GetInfo();Pet:Pet(char *name,int age,char *color) strcpy(Name,name); Age=age

8、; strcpy(Color,color); strcpy(Type,pet);class Cat : public Pet/派生類public:Cat(char *name,int age, char *color):Pet(name,age,color) void Speak() cout Sound of speak : miao!miao!endlendl; void GetInfo();void Cat:GetInfo()cout The cats name : GetName()endl; cout The cats age : GetAge() endl; cout The ca

9、ts color: GetColor()endl;class Dog : public Pet/派生類public:Dog(char *name,int age, char*color):Pet(name,age,color) void Speak() cout Sound of speak : wang!wang!endlendl; void GetInfo();void Dog:GetInfo()cout The dogs name : GetName()endl;cout The dogs age: GetAge() endl;cout The dogs color: GetColor(

10、)GetInfo();p1-Speak(); delete p1;p1 = new Dog(BenBen,2,Black);/動態(tài)生成Dog類對象p1-GetInfo();p1-Speak(); delete p1; return 0;12.5 運(yùn)算符重載1、重載運(yùn)算符主要用于對類的對象的操作2、重載運(yùn)算符使得運(yùn)算符適用的對象類型得到了拓寬3、重載運(yùn)算符可以通過創(chuàng)建運(yùn)算符函數(shù)實(shí)現(xiàn),創(chuàng)建operator函數(shù)格式如下: : operator ()例12-4定義一個(gè)復(fù)數(shù)類,并重載加法運(yùn)算符以適應(yīng)對復(fù)數(shù)運(yùn)算的要求。#include using namespace std; class Complexd

11、ouble real, imag; public:Complex(double r = 0, double i = 0): real(r), imag(i) double Real()return real;double Imag()return imag; Complex operator +(Complex&); Complex operator +(double); Complex operator =(Complex);Complex Complex:operator + (Complex &c)/ 重載運(yùn)算符Complex temp; temp.real = real+c.real;

12、temp.imag = imag+c.imag;returntemp;Complex Complex:operator + (double d) / 重載運(yùn)算符 Complex temp;temp.real = real+d; temp.imag = imag; return temp;Complex Complex:operator = (Complex c)/ 重載運(yùn)算符real = c.real;imag = c.imag; return *this;/ 測試主函數(shù)int main()Complex c1(3,4),c2(5,6),c3;cout cout c1 = c1.Real()

13、+j c1.Imag() endl; c2 = c2.Real() +j c2.Imag() endl;c3 = c1+c2;cout c3 = c3.Real() +j c3.Imag() endl;c3 = c3+6.5;cout c3 + 6.5 = c3.Real() +j c3.Imag() endl;return 0;C+中不允許重載的運(yùn)算符運(yùn)算符運(yùn)算符名稱禁止重載的理由? :三目條件運(yùn)算符C+中沒有定義三目運(yùn)算符的語法.成員操作符為保證成員操作符對成員訪問的安全性:作用域操作符該操作符右操作數(shù)不是表達(dá)式sizeof類型字長操作符該操作符的操作數(shù)為類型名,不是表達(dá)式程序設(shè)計(jì)舉例例1

14、2-5 抽象寵物類的另一種用法/Example12-5 抽象寵物類#include using namespace std; class Petpublic:virtual void Speak()=0; void ShowMe();/基類;void Pet:ShowMe()cout我的聲音:; Speak();class Cat : public Pet/派生類public:virtual void Speak() coutmiao!miao!endl; ;class Dogpublic: public Pet/派生類virtual void Speak() coutwang!wang!en

15、dl; ;void main()Cat cat1; Dog dog1;cat1.ShowMe(); dog1.ShowMe();例12-6從例11-5的Point、Circle類中抽象出基類Shape,研究抽象類和具體類的接口和實(shí)現(xiàn)。/ shape.h文件定義抽象基類Shape#ifndef SHAPE_H#define SHAPE_H #include using namespace std; class Shapepublic:virtual double Area() const return 0.0; / 純虛函數(shù),在派生類中重載virtual void PrintShapeName(

16、) const = 0;virtual void Print() const = 0;#endif/ point.h文件 定義類Point#ifndef POINT_H #define POINT_H #include shape.hclass Point : public Shapeint x, y;/點(diǎn)的x和y坐標(biāo)public:Point( int = 0, int = 0 ); / 構(gòu) 造 函 數(shù)void SetPoint( int, int ); / 設(shè) 置 坐 標(biāo)int GetX() return x; / 取x坐標(biāo)int GetY() return y; / 取y坐標(biāo)virtual

17、 void PrintShapeName() const cout Point: ; virtual void Print() const;/輸出點(diǎn)的坐標(biāo);#endif/ Point.cpp文件 Point類的成員函數(shù)定義#include using namespace std;#include point.hPoint:Point( int a, int b ) SetPoint( a, b ); void Point:SetPoint( int a, int b )x = a;y = b;void Point:Print() const cout x , y ;/ circle.h定義類

18、Circle#ifndef CIRCLE_H #define CIRCLE_H#include using namespace std;#include point.hclass Circle : public Point double radius;public:Circle(int x = 0, int y = 0 ,double r = 0.0); void SetRadius( double );/設(shè)置半徑double GetRadius() const;/取半徑virtual double Area() const;/計(jì)算面積virtual void Print() const;/輸

19、出圓心坐標(biāo)和半徑virtual void PrintShapeName() const cout = 0 ? r : 0 ); double Circle:GetRadius() const return radius; double Circle:Area() const return 3.14159 * radius * radius; void Circle:Print() constcout Center = ;Point:Print();cout ; Radius = radius endl;/ Rectangle.h文件定義類Rectangle#ifndef RECTANGLE_H

20、 #define RECTANGLE_H#include using namespace std;#include point.hclass Rectangle : public Pointdouble length, width; public:Rectangle(int x=0, int y=0, double l=0.0, double w=0.0); void SetLength( double );/設(shè)置長度void SetWidth( double );/設(shè)置寬度double GetLength() const;/ 取 長 度 double GetWidth() const;/取寬

21、度virtual double Area() const;/計(jì)算面積virtual void Print() const;/輸出坐標(biāo)和尺寸virtual void PrintShapeName() const cout = 0 ? l : 0 ); void Rectangle:SetWidth( double w ) width = ( w = 0 ? w : 0 ); double Rectangle:GetLength() const return length; double Rectangle:GetWidth() const return width; double Rectang

22、le:Area() const return length * width; void Rectangle:Print() constcout Left Top Vertex = ; Point:Print();cout ; Length = length , Wigth = width endl;/ Example12-6.cpp文件 演示圖形類#include using namespace std;#include #include #include #includeshape.hpoint.h circle.h rectangle.hvoid virtualViaPointer( const Shape * );void virtualViaReference( const Shape & );int main()/創(chuàng)建point、circle、rectangle對象Point point(30,50);Circle circle(120,80,10.0); Rectang

溫馨提示

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

評論

0/150

提交評論