繼承 (inheritance) 為物件導向程式設計 (object-oriented programming) 的特性之一,衍生類別 (derived class) 可以承接基礎類別 (base class) 的 public 或 protected 成員 (member) ,使之具有共通的 public 或 protected 成員。

衍生類別也常被稱為子類別 (subclass) ,基礎類別也常被稱為父類別 (superclass) 。

「繼承」是物件導向程式設計中一個重要也是核心的觀念,動詞原文為 inherit 。雖然 inherit 在英文的意思泛指從什麼得到什麼,用作中文「繼承」說得通,也能用作「遺傳」。然而中文的「繼承」隱含某物不再,另物將起的意思,譬如「我繼承某某的精神」,雖然某某不見得已死,未來將要付出努力的卻是我而非某某。因而這裡的意思中文用「遺傳」比較恰當,子代會從親代遺傳某些生物特性,子代與親代也會並存一段時間,這就沒有某物不再的意含了。然而這裡我們仍沿用程式設計常用的「繼承」一詞。

一張圖可以解釋Inheritance

為什麼用到繼承 (inheritance)?

假設你要寫個三個角色:

正常做法是你會弄一個個角色的Class,然後寫上他的function。但有些角色因為會有大量一樣的function(例如Talk()Walk()),寫起來要copy and paste很多次,很麻煩。

這時侯我們就要用到Inheritance(繼承)。

先留意:
在這個例子,數學老師,足球員,生意人都是人類(Person),可以說話(Talk)和走路(Walk)。

我們可以弄一個Superclass (Base Class) 給他們。

基礎類別 & 衍生類別

C++允許讓一個class繼承另一個class,例如有A Class,您可以使用下面的方式來讓B Class繼承:

繼承的class稱為子類別(subclass) ,被繼承的class稱為父類別(Superclass).

1
2
3
4
5
6
7
8
9
10
11
class B : public A{

public:

//如果你的class要有constructor:
B(int x) : A(x){ // 繼承下的Constructor
// code here
}

// code here
};

示範

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Person 
{
//code here
};

class MathsTeacher : public Person
{
//code here
};

class Footballer : public Person
{
//code here
};

在上面的示例中,Person是Superclass,而MathsTeacherFootballer則是從Person衍生的。

Derived class (subclass) 與Class的聲明一起出現,後跟著冒號,關鍵字public和base class (superclass)的名稱。

1
2
3
4
5
6
7
8
9
10
11
//base class
class superclass
{
//code here
};

//derived class
class subclass : public superclass //留意:public 你可以轉成 protected 或 private, 看你是在什麼情況用。不加的話默認為 private.
{
//code here
};

由於MathsTeacherFootballer是從Person衍生的(Person是base class, MathsTeach是Person的derived class),因此可以從它們access Person的所有data member(數據成員)和member function (成員功能)。

實例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
class Person
{
public:
string profession;
int age;
Person(): profession("unemployed"), age(16) { }
void display()
{
cout << "My profession is: " << profession << endl;
cout << "My age is: " << age << endl;
walk();
talk();
}
void walk() { cout << "I can walk." << endl; }
void talk() { cout << "I can talk." << endl; }
};
// MathsTeacher class is derived from base class Person.
class MathsTeacher : public Person
{
public:
void teachMaths() { cout << "I can teach Maths." << endl; }
};
// Footballer class is derived from base class Person.
class Footballer : public Person
{
public:
void playFootball() { cout << "I can play Football." << endl; }
};
int main()
{
MathsTeacher teacher;
teacher.profession = "Teacher";
teacher.age = 23;
teacher.display();
teacher.teachMaths();
Footballer footballer;
footballer.profession = "Footballer";
footballer.age = 19;
footballer.display();
footballer.playFootball();
return 0;
}

多重繼承 (Multiple Inheritance)

C++允許讓衍生類別同時直接繼承兩個以上的父類別,例如有A、B兩個類別,您可以使用下面的方式來讓C類別直接繼承這兩個類別:

1
2
3
4
5
6
7
8
9
10
11
class C : public A, public B {

public:

//如果你的class要有constructor:
C(int x) : A(x), B(x) { // 多重繼承下的Constructor
// code here
}

// code here
};

Reference :
C++ 快速導覽 - 類別 繼承
C++ 继承
不要用父子關係去理解 C++ Inheritance
C++ Inheritance
Basic inheritance in C++
多重繼承(一)
C+±-多重繼承
C++多重繼承