C/C++中多重繼承詳解及其作用介紹
概述
多重繼承 (multiple inheritance): 一個(gè)派生類有兩個(gè)或多個(gè)基類, 派生類從兩個(gè)或多個(gè)基類中繼承所需的屬性. C++ 為了適應(yīng)這種情況, 允許一個(gè)派生類同時(shí)繼承多個(gè)基類. 這種行為稱為多重繼承.

優(yōu)缺點(diǎn)
優(yōu)點(diǎn)
- 自然地做到了對(duì)單繼承的擴(kuò)展
- 可以繼承多個(gè)類的功能
缺點(diǎn)
- 結(jié)構(gòu)復(fù)雜化
- 優(yōu)先順序模糊
- 功能沖突
聲明多重繼承的方法
格式
多重繼承的格式:
派生類構(gòu)造函數(shù)名(總形式參數(shù)表列):
基類1構(gòu)造函數(shù)(實(shí)際參數(shù)表列),
基類2構(gòu)造函數(shù)(實(shí)際參數(shù)表列),
基類3構(gòu)造函數(shù)(實(shí)際參數(shù)表列)
{
派生類中新增數(shù)成員據(jù)成員初始化語句
}
例子
Teacher 類:
#ifndef PROJECT5_TEACHER_H
#define PROJECT5_TEACHER_H
#include <string>
using namespace std;
class Teacher {
protected:
string name;
int age;
string title;
public:
Teacher(string n, int a, string t);
void display_teacher();
};
#endif //PROJECT5_TEACHER_H
Teacher.cpp:
#include <iostream>
#include "Teacher.h"
using namespace std;
Teacher::Teacher(string n, int a, string t) : name(n), age(a), title(t) {}
void Teacher::display_teacher() {
cout << "Teacher name: " << name << endl;
cout << "age: " << age << endl;
cout << "title: " << title << endl;
}
Student 類:
#ifndef PROJECT5_STUDENT_H
#define PROJECT5_STUDENT_H
#include <string>
using namespace std;
class Student {
protected:
string name;
char gender;
double score;
public:
Student(string n, char g, double s);
void display_student();
};
#endif //PROJECT5_STUDENT_H
Student.cpp:
#include <iostream>
#include "Student.h"
using namespace std;
Student::Student(string n, char g, double s) : name(n), gender(g), score(s) {}
void Student::display_student() {
cout << "Student name: " << name << endl;
cout << "gender: " << gender << endl;
cout << "score: " << score << endl;
}
Graduate 類:
#ifndef PROJECT5_GRADUATE_H
#define PROJECT5_GRADUATE_H
#include "Teacher.h"
#include "Student.h"
#include <string>
using namespace std;
class Graduate : public Teacher, public Student{
private:
double wage;
public:
Graduate(string t_n, int t_a, string t_t, string s_n, char s_g, double s_s);
void display_graduate();
};
#endif //PROJECT5_GRADUATE_H
Graduate.cpp:
#include "Graduate.h"
Graduate::Graduate(string t_n, int t_a, string t_t, string s_n, char s_g, double s_s) :
Teacher(t_n, t_a, t_t),
Student(s_n, s_g, s_s) {}
void Graduate::display_graduate() {
display_teacher();
display_student();
}
main:
#include <iostream>
#include "Graduate.h"
using namespace std;
int main() {
Graduate graduate1("王叔叔", 18, "隔壁老王", "我是小白呀", 'f', 99);
graduate1.display_graduate();
return 0;
}
輸出結(jié)果:
Teacher name: 王叔叔 age: 18 title: 隔壁老王 Student name: 我是小白呀 gender: f score: 99
二義性
二義性 (Ambiguity) 指在多重繼承中, 兩個(gè)基類中的數(shù)據(jù)成員名相同.

二義性在派生類中的解決方法:
- 在標(biāo)識(shí)符前用類名做前綴: Teacher::name 和 Student::name
- 基類和派生類需要有一個(gè)完整的設(shè)計(jì), 不能隨意而為
兩個(gè)基類有同名成員

A 類:
#ifndef PROJECT5_A_H
#define PROJECT5_A_H
#include <iostream>
using namespace std;
class A {
public:
int num;
void display() {cout << "A's num:" << num << endl;};
};
#endif //PROJECT5_A_H
B 類:
#ifndef PROJECT5_B_H
#define PROJECT5_B_H
#include <iostream>
using namespace std;
class B {
public:
int num;
void display() {cout << "B's num:" << num << endl;};
};
#endif //PROJECT5_B_H
C 類:
#ifndef PROJECT5_C_H
#define PROJECT5_C_H
#include <iostream>
#include "A.h"
#include "B.h"
using namespace std;
class C: public A, public B{
public:
int c;
void display() {cout << c << endl;};
};
#endif //PROJECT5_C_H
main:
#include <iostream>
#include "C.h"
using namespace std;
int main() {
C c1;
c1.A::num = 1; // 用基類名限定
c1.B::num = 2; // 用基類名限定
c1.A::display();
c1.B::display();
return 0;
}
輸出結(jié)果:
A's num:1 B's num:2
錯(cuò)誤的寫法
#include <iostream>
#include "C.h"
using namespace std;
int main() {
C c1;
c1.num = 1;
c1.display();
return 0;
}
基類和派生類有同名成員
A 類:
class A {
public:
int num;
void display() {cout << "A's num:" << num << endl;};
};
B 類:
class B {
public:
int num;
void display() {cout << "B's num:" << num << endl;};
};
C 類:
class C: public A, public B{
public:
int num;
void display() {cout << "C's num:" << num << endl;};
};
main:
int main() {
C c1;
c1.num = 3;
c1.A::num = 1;
c1.B::num = 2;
c1.display();
c1.A::display();
c1.B::display();
return 0;
}
輸出結(jié)果:
C's num:3 A's num:1 B's num:2
同名覆蓋:
- 基類的同名成員在派生類中被屏蔽, 成為 "不可見"的
- 對(duì)成員函數(shù), 限于函數(shù)名和參數(shù)個(gè)數(shù)相同, 類型相匹配. 若只有函數(shù)名相同而參數(shù)不同, 屬于函數(shù)重載
兩個(gè)基類從同一個(gè)基類派生
N 類:
class N {
public:
int a;
void display(){
cout << "A::a=" << a <<endl;
}
};
A 類:
class A : public N {
public:
int a1;
};
B 類:
class B : public N {
public:
int a2;
};
C 類:
class C: public A, public B{
public:
int a3;
void display() {cout << "a3=" << a3 << endl;};
};
main:
int main() {
C c1;
// 合法訪問
c1.A::a = 3;
c1.A::display();
return 0;
}
輸出結(jié)果:
A::a=3
到此這篇關(guān)于C/C++中多重繼承詳解及其作用介紹的文章就介紹到這了,更多相關(guān)C++多重繼承內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中程序環(huán)境和預(yù)處理的詳細(xì)圖文講解
這篇文章主要給大家介紹了關(guān)于C語言中程序環(huán)境和預(yù)處理的相關(guān)資料,我們寫的C語言代碼,從運(yùn)行,到在屏幕上生成結(jié)果,經(jīng)歷了比較復(fù)雜的過程,需要的朋友可以參考下2023-02-02
C++ abs函數(shù)實(shí)際應(yīng)用詳解
本文我們來講C++的abs函數(shù)以及實(shí)戰(zhàn)運(yùn)用,C++中的abs函數(shù)。在C++中使用abs函數(shù)要注意存在兩種版本,一種是在stdlmb.h中定義的版本,另一個(gè)是在cmath頭文件中定義的。夷實(shí)上在stdlib.h文件是C的函數(shù),而cmath中的是C++版本2022-08-08
C++設(shè)計(jì)模式編程中使用Bridge橋接模式的完全攻略
這篇文章主要介紹了C++設(shè)計(jì)模式編程中使用Bridge橋接模式的完全攻略,Bridge將抽象部分與它的實(shí)現(xiàn)部分分離,使它們都可以獨(dú)立地變化需要的朋友可以參考下2016-03-03
OpenCV實(shí)現(xiàn)圖像的直線檢測(cè)
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)圖像直線檢測(cè)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
使用VS Code進(jìn)行Qt開發(fā)的實(shí)現(xiàn)
這篇文章主要介紹了使用VS Code進(jìn)行Qt開發(fā)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

