[C++] 類的繼承 特性及實現


面向對象程序設計中最重要的一個概念是繼承。繼承允許我們依據另一個類來定義一個類,這使得創建和維護一個應用程序變得更容易。這樣做,也達到了重用代碼功能和提高執行效率的效果。

當創建一個類時,您不需要重新編寫新的數據成員和成員函數,只需指定新建的類繼承了一個已有的類的成員即可。這個已有的類稱為基類,新建的類稱為派生類

繼承代表了 is a 關系。例如,哺乳動物是動物,狗是哺乳動物,因此,狗是動物,等等。

基類 & 派生類

一個類可以派生自多個類,這意味着,它可以從多個基類繼承數據和函數。定義一個派生類,我們使用一個類派生列表來指定基類。類派生列表以一個或多個基類命名,形式如下:

class derived-class: access-specifier base-class

其中,訪問修飾符 access-specifier 是 public、protectedprivate 其中的一個,base-class 是之前定義過的某個類的名稱。如果未使用訪問修飾符 access-specifier,則默認為 private。

假設有一個基類 ShapeRectangle 是它的派生類,如下所示:

實例

  1   2 //jet.h / jet.cpp  //派生自基類airplan 繼承airplan的接口和數據
  3 #pragma once
  4 #ifndef __jet_h__
  5 #define __jet_h__
  6 #include <iostream>
  7 #include "airplane.h"
  8 namespace apdance
  9 {
 10 class jet : public airplane //繼承自airplane 
 11 {
 12 public:
 13     jet();
 14     ~jet();
 15     void set_weapons(int w);
 16     int get_weapons();
 17         
 18 private:
 19     int weapons;
 20 };
 21  22 }
 23 #endif
 24  25 #include "stdafx.h"
 26 #include <iostream>
 27 #include "jet.h"
 28  29 namespace apdance
 30 {
 31     jet::jet()
 32     {
 33         weapons = 1;
 34         std::cout << "jet take off!\n";
 35     }
 36     jet::~jet()
 37     {
 38         std::cout << "jet down!may day may day!";
 39     }
 40     void jet::set_weapons(int w)
 41     {
 42         weapons = w;
 43     }
 44     int jet::get_weapons()
 45     {
 46         return weapons;
 47     }
 48 }//namespace apdance
 49  50  51 //airplan.h / airplan.cpp 
 52 #pragma once
 53 #ifndef __AIRPLANE_H__
 54 #define __AIRPLANE_H__
 55 #include <iostream>
 56 namespace apdance //定義命名空間
 57 {
 58 class airplane //創建基類airplane
 59 {
 60 public:
 61     airplane()
 62     {
 63         wheels = 3;
 64         wings = 2;
 65         engines = 1;
 66         std::cout << "對象已創建\n";
 67     }
 68     ~airplane()
 69     {
 70         std::cout << "對象已刪除\n";
 71     }
 72 public:
 73     void set_wings(int w);
 74     int get_wings();
 75     void set_wheels(int w);
 76     int get_wheels();
 77     void set_engines(int w);
 78     int get_engines();
 79     void fly();
 80 private:
 81     int wings;
 82     int wheels;
 83     int engines;
 84 };
 85 }//namespace
 86 #endif // !___AIRPLANE_H__
 87 #include "stdafx.h"
 88 #include <iostream>
 89 #include "airplane.h"
 90 namespace apdance
 91 {
 92 void airplane::set_wings(int w)
 93 {
 94     wings = w;
 95 }
 96 int airplane::get_wings()
 97 {
 98     return wings;
 99 }
100 void airplane::set_wheels(int w)
101 {
102     wheels = w;
103 }
104 int airplane::get_wheels()
105 {
106     return wheels;
107 }
108 void airplane::set_engines(int w)
109 {
110     engines = w;
111 }
112 int airplane::get_engines()
113 {
114     return engines;
115 }
116 void airplane::fly()
117 {
118     std::cout << "iam fly now!\n";
119 }
120 }//namespace
121 122 123 // cpp_learning.cpp : 定義控制台應用程序的入口點。 測試 沒問題
124 #include "stdafx.h"
125 #include "iostream"
126 #include "airplane.h"
127 #include "jet.h"
128 int main()
129 {
130     apdance::jet x;
131     x.set_engines(100);
132     std::cout << x.get_engines() << '\n';
133     return 0;
134 }

 

訪問控制和繼承

派生類可以訪問基類中所有的非私有成員。因此基類成員如果不想被派生類的成員函數訪問,則應在基類中聲明為 private。

我們可以根據訪問權限總結出不同的訪問類型,如下所示:

訪問 public protected private
同一個類 yes yes yes
派生類 yes yes no
外部的類 yes no no

一個派生類繼承了所有的基類方法,但下列情況除外:

  • 基類的構造函數、析構函數和拷貝構造函數。

  • 基類的重載運算符。

  • 基類的友元函數。

繼承類型

當一個類派生自基類,該基類可以被繼承為 public、protectedprivate 幾種類型。繼承類型是通過上面講解的訪問修飾符 access-specifier 來指定的。

我們幾乎不使用 protectedprivate 繼承,通常使用 public 繼承。當使用不同類型的繼承時,遵循以下幾個規則:

  • 公有繼承(public):當一個類派生自公有基類時,基類的公有成員也是派生類的公有成員,基類的保護成員也是派生類的保護成員,基類的私有成員不能直接被派生類訪問,但是可以通過調用基類的公有保護成員來訪問。

  • 保護繼承(protected): 當一個類派生自保護基類時,基類的公有保護成員將成為派生類的保護成員。

  • 私有繼承(private):當一個類派生自私有基類時,基類的公有保護成員將成為派生類的私有成員。

多繼承

多繼承即一個子類可以有多個父類,它繼承了多個父類的特性。

C++ 類可以從多個類繼承成員,語法如下:

class <派生類名>:<繼承方式1><基類名1>,<繼承方式2><基類名2>,…
{
<派生類類體>
};

其中,訪問修飾符繼承方式是 public、protectedprivate 其中的一個,用來修飾每個基類,各個基類之間用逗號分隔,如上所示。現在讓我們一起看看下面的實例:

實例


#include <iostream>
 
using namespace std;
 
// 基類 Shape
class Shape 
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};
 
// 基類 PaintCost
class PaintCost 
{
   public:
      int getCost(int area)
      {
         return area * 70;
      }
};
 
// 派生類
class Rectangle: public Shape, public PaintCost
{
   public:
      int getArea()
      { 
         return (width * height); 
      }
};
 
int main(void)
{
   Rectangle Rect;
   int area;
 
   Rect.setWidth(5);
   Rect.setHeight(7);
 
   area = Rect.getArea();
   
   // 輸出對象的面積
   cout << "Total area: " << Rect.getArea() << endl;
 
   // 輸出總花費
   cout << "Total paint cost: $" << Rect.getCost(area) << endl;
 
   return 0;
}

當上面的代碼被編譯和執行時,它會產生下列結果:

Total area: 35
Total paint cost: $2450


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM