天天看點

c++入門(多态and純虛函數and抽象類)

檔案名:<1> shape.h ;<2> circle.h ;<3> circle.cpp ;<4> rectangular.h ;<5> rectangular.cpp ;<6> main.cpp

1、多态

是面向對象程式設計的關鍵技術。

調用同一個函數名,可以根據需要實作不同的功能。

編譯時的多态性(函數重載);運作時的多态性(虛函數)。

2、純虛函數

在函數基類中聲明,在派生類中實作。

virtual關鍵字;聲明時須在後面加上 =0;派生類中實作時無須寫virtual。

3、抽象類

含有純虛函數的類。

在派生類實作該純虛函數後,定義抽象類對象的指針,并指向引用的子類對象

注:

可以把析構函數定義為虛函數,但不能将構造函數定義為虛函數。

因為定義為虛函數的構造函數隻是聲明了而沒有實作,沒有初始化,不能被繼承。

每個類都要有自己的構造函數,對自己進行初始化。

<1> shape.h

1 //檔案名 shape.h
  2 
  3 #ifndef SHAPE_H //防止重複包含的宏開關
  4 #define SHAPE_H
  5 
  6 #include <string>
  7 
  8 /* virtual在基類中聲明純虛函數,其實作在派生類中  */
  9 class i_Shape
 10 {
 11 public:
 12         virtual float getArea() = 0;    //純虛函數的聲明:virtual關鍵字,聲明之後寫上 =0
 13         virtual std::string getName() = 0;
 14 
 15 };
 16 
 17 #endif
           

<2> circle.h

1 //檔案名 circle.h
  2 
  3 #ifndef CIRCLE_H
  4 #define CIRCLE_H
  5 
  6 #include "shape.h"
  7 
  8 class c_Circle : public i_Shape //繼承i_Shape類
  9 {
 10 public:
 11         c_Circle(float radius); //含參數的構造函數
 12 public:
 13         virtual float getArea();        //抽象類的虛函數。此處需要關鍵字virtual,但不需要在後面加上 =0
 14         virtual std::string getName();
 15 private:
 16         float m_Radius;
 17 };
 18 
 19 #endif
           

<3> circlr.cpp

1 //檔案名 circle.cpp
  2 
  3 #include "circle.h"
  4 
  5 c_Circle::c_Circle(float radius)        //含參數的虛構函數
  6         :m_Radius(radius)       //初始化清單
  7 {
  8 
  9 }
 10 
 11 float c_Circle::getArea()       //虛函數的實作。此處無須關鍵字virtual
 12 {
 13         return 3.14 * m_Radius * m_Radius;
 14 }
 15 
 16 std::string c_Circle::getName()         //虛函數的實作。此處無須關鍵字virtual
 17 {
 18         return "circle 圓形的";
 19 }
           

<4> rectangular.h

1 //檔案名 rectangualr.h
  2 
  3 #ifndef RECTANGULAR_H   //防止重複包含的宏開關
  4 #define RECTANGULAR_H
  5 
  6 #include "shape.h"
  7 
  8 class c_Rect : public i_Shape   //繼承 i_Shape接口 interface
  9 {
 10 public:
 11         c_Rect();       //不含參數的預設構造函數
 12         ~c_Rect();      //析構函數
 13         c_Rect(float width,float length);       //含參數的構造函數
 14 public:
 15         virtual float getArea();        //virtual虛函數。。此處無須寫 =0
 16         virtual std::string getName();
 17 private:
 18         float m_Width;  //private成員
 19         float m_Length;
 20 
 21 };
 22 
 23 #endif
           

<5> rectangular.cpp

1 //檔案名 rectangular.cpp
  2 
  3 #include "rectangular.h"
  4 
  5 c_Rect::c_Rect()        //不含參數的預設構造函數
  6 {
  7 
  8 }
  9 
 10 c_Rect::~c_Rect()       //析構函數
 11 {
 12 
 13 }
 14 
 15 c_Rect::c_Rect(float width,float length)        //含參數的構造函數
 16         :m_Width(width),m_Length(length)        //初始化清單:以冒号開頭,以逗号分隔
 17 {
 18 
 19 }
 20 
 21 float c_Rect::getArea() //虛函數的實作
 22 {
 23         return m_Width * m_Length;
 24 }
 25 
 26 std::string c_Rect::getName()   //虛函數的實作
 27 {
 28         return "Rectangular 方形的";
 29 }
           

<6> main.cpp

1 //檔案名 main.cpp
  2 //
  3 #include "rectangular.h"
  4 #include "circle.h"
  5 
  6 #include <iostream>
  7 
  8 int main()
  9 {
 10         /* 定義抽象類對象i_Shape的指針p,并指向引用的子類對象c_Circle  */
 11         i_Shape* p = new c_Circle(10);
 12         std::cout<< p -> getName() << "  " << p -> getArea() <<std::endl;
 13         delete p;
 14 
 15         /* 定義抽象類對象i_Shape的指針p,并指向引用的子類對象c_Ret  */
 16         p = new c_Rect(10,20);
 17         std::cout<< p -> getName() << "  " << p -> getArea() <<std::endl;
 18 
 19         return 0;
 20 }
           

編譯、連結、執行

c++入門(多态and純虛函數and抽象類)

繼續閱讀