Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

C++ 多重繼承入門:教師兼幹部類別的設計與同名成員處理

編輯:關於 Android 程式設計

多重繼承是 C++ 裡威力很強、也很容易被誤用的特性。下面這個經典範例——「既是教師、又是幹部」的人——剛好把它的要點與陷阱都涵蓋了:如何同時繼承兩個基類、如何在建構子中依序初始化、以及當兩個基類有同名成員時該怎麼消歧義。

一、兩個基類

/*
 * Copyright (c) 2013, 煙台大學計算機學院
 * All rights reserved.
 * 檔本名稱:test.cpp
 * 作者:樊露露
 * 完成日期:2013 年 5 月 27 日
 * 版本號:v1.0
 */
#include <iostream>
#include <string>
using namespace std;

class Teacher
{
public:
    Teacher(string n, int a, char s, string ad, string t, string ti);
    void display();
private:
    string name;
    int age;
    char sex;
    string add;
    string tel;
    string title;   // 職稱
};

Teacher::Teacher(string n, int a, char s, string ad, string t, string ti)
    : name(n), age(a), sex(s), add(ad), tel(t), title(ti) { }

void Teacher::display()
{
    cout << "name:" << name << endl;
    cout << "age:" << age << endl;
    cout << "sex:" << sex << endl;
    cout << "title:" << title << endl;
    cout << "address:" << add << endl;
    cout << "tel:" << tel << endl;
}

Teacher 用初始化列表完成所有成員的賦值,這是 C++ 較推薦的寫法,對於 const 成員與參考成員更是唯一選擇。

class Cadre
{
public:
    Cadre(string n, int a, char s, string ad, string t, string p);
    void display();
private:
    string name;
    int age;
    char sex;
    string add;
    string tel;
    string post;    // 職務
};

Cadre::Cadre(string n, int a, char s, string ad, string t, string p)
{
    name = n;
    age = a;
    sex = s;
    add = ad;
    tel = t;
    post = p;
}

void Cadre::display()
{
    cout << "name:" << name << endl;
    cout << "age" << age << endl;
    cout << "sex:" << sex << endl;
    cout << "post:" << post << endl;
    cout << "address:" << add << endl;
    cout << "tel:" << tel << endl;
}

這裡刻意用另一種寫法(在建構子body中逐一賦值)作對照。兩者結果相同,但前者更簡潔。

二、派生類:同時繼承 Teacher 與 Cadre

class Teacher_Cadre : public Teacher, public Cadre
{
public:
    Teacher_Cadre(string n, int a, char s, string ad, string t,
                  string ti, string p, int w);
    void show();
private:
    string name;
    int age;
    char sex;
    string add;
    string tel;
    int wages;
};

Teacher_Cadre::Teacher_Cadre(string n, int a, char s, string ad, string t,
                             string ti, string p, int w)
    : Teacher(n, a, s, ad, t, ti), Cadre(n, a, s, ad, t, p), wages(w) { }

void Teacher_Cadre::show()
{
    Teacher::display();
    // cout << "post:" << Cadre::post << endl;   // post 是 private,無法直接存取
    cout << "wages:" << wages << endl;
}

這段程式碼裡有兩個關鍵細節值得說明。

建構子的初始化順序。派生類建構時,會依「類別宣告順序」而非「初始化列表的書寫順序」呼叫各基類建構子,因此 Teacher 一定先於 Cadre 被建構,最後才初始化自己的 wages。

同名成員的消歧義。Teacher 與 Cadre 各自都有 name、age、sex、add、tel 與 display()。如果直接寫 te_ca.name,編譯器無從判斷你指的是哪一份,會報 ambiguous 錯誤;解法就是加上作用域解析運算子,寫成 Teacher::name 或 Cadre::name。show() 中呼叫 Teacher::display() 正是這個道理。至於被註解掉的那行,是因為 post 屬於 Cadre 的 private 成員,即使寫成 Cadre::post 也依然存取不到——這與歧義問題是兩回事。

另外可以注意到,Teacher_Cadre 自己也重新宣告了一份 name、age 等成員,這些會把基類的同名成員「遮蔽」起來,通常是不必要的設計,實務上建議移除,改用基類的 protected 成員或提供存取器。

三、主程式

int main()
{
    Teacher_Cadre te_ca("Wang-li", 50, 'f',
                        "135 Beijing Road,Shanghai",
                        "(021)61234567",
                        "president", "prof.", 1534);

    te_ca.show();
    return 0;
}

注意建構子實參數目與順序:第 6 個參數 "president" 會傳給 Teacher 的 title,第 7 個 "prof." 則傳給 Cadre 的 post。撰寫這類多參數建構子時,強烈建議改用具名的設定方式或引入 setter,單靠位置傳值非常容易出錯。

四、什麼時候該用多重繼承

多重繼承在 C++ 中之所以名聲不佳,是因為它會帶來「菱形繼承」問題:若 Teacher 與 Cadre 都繼承自同一個 Person,那麼 Teacher_Cadre 中就會存在兩份 Person 子物件,需要以 virtual 繼承化解。因此實務上的建議是:優先考慮組合(把 Teacher 與 Cadre 作為成員變數),或改用介面(純虛擬基類)來表達「能同時具備多種身分」的概念。只有在這些能力真的需要共用同一份實作、且繼承層次清楚的情況下,多重繼承才是合適的工具。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved