C/C++中虛基類詳解及其作用介紹
概述
虛基類 (virtual base class) 是用關(guān)鍵字 virtual 聲明繼承的父類.
多重繼承的問題
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
存在的問題:
- A::a 和 B::a 是 N 類成員的拷貝
- A::a 和 B::a 占用不同的空間
虛基類
我們希望繼承間接共同基類時(shí)只保留一份成員, 所以虛基類就誕生了. 當(dāng)基類通過多條派生路徑被一個(gè)派生類繼承時(shí), 該派生類只繼承該基類一次.
語法:
class 派生類名: virtual 繼承方式 基類名
初始化
通過構(gòu)造函數(shù)的初始化表對(duì)虛擬類進(jìn)行初始化. 例如:
N 類:
class N { public: int n; N(int n) : n(n) {}; };
A 類:
class A : virtual public N { public: A(int n) : N(n) {}; };
B 類:
class B : virtual public N { public: B(int n) : N(n) {}; };
C 類:
class C: public A, public B{ public: C(int n) : N(n), A(n), B(n){}; };
例子
Person 類:
#ifndef PROJECT5_PERSON_H #define PROJECT5_PERSON_H #include <iostream> #include <string> using namespace std; class Person { protected: string name; char gender; public: Person(string n, char g) : name(n), gender(g) {} void display() { cout << "name: " << name << endl; cout << "gender: " << gender << endl; } }; #endif //PROJECT5_PERSON_H
Student 類:
#ifndef PROJECT5_STUDENT_H #define PROJECT5_STUDENT_H #include <string> #include "Person.h" using namespace std; class Student : virtual public Person { protected: double score; public: Student(string n, char g, double s) : Person(n, g), score(s) {}; }; #endif //PROJECT5_STUDENT_H
Teacher 類:
#ifndef PROJECT5_TEACHER_H #define PROJECT5_TEACHER_H #include <string> #include "Person.h" using namespace std; class Teacher : virtual public Person { protected: string title; public: Teacher(string n, char g, string t) : Person(n, g), title(t) {}; }; #endif //PROJECT5_TEACHER_H
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 n, char g, double s, string t, double w) : Person(n, g), Student(n, g, s), Teacher(n, g, t), wage(w) {}; void display() { Person::display(); cout << "score: " << score << endl; cout << "title: " << title << endl; cout << "wages: " << wage << endl; }; }; #endif //PROJECT5_GRADUATE_H
main:
#include <iostream> #include "Graduate.h" using namespace std; int main() { Graduate grad1("小白",'f',89.5,"教授",1234.5); grad1.display(); return 0; }
輸出結(jié)果:
name: 小白
gender: f
score: 89.5
title: 教授
wages: 1234.5
總結(jié)
- 使用多重繼承時(shí)要十分小心, 否則會(huì)進(jìn)場(chǎng)出現(xiàn)二義性問題
- 不提倡在程序中使用多重繼承
- 只有在比較簡(jiǎn)單和不易出現(xiàn)二義性的情況或?qū)嵲诒匾獣r(shí)才使用多重繼承
- 能用單一繼承解決的問題就不要使用多重繼承
到此這篇關(guān)于C/C++中虛基類詳解及其作用介紹的文章就介紹到這了,更多相關(guān)C++虛基類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QT實(shí)戰(zhàn)之實(shí)現(xiàn)圖片瀏覽系統(tǒng)
這篇文章主要介紹了如何利用QT編寫一個(gè)圖片瀏覽系統(tǒng),可以支持自動(dòng)播放,左右拖動(dòng)切換,點(diǎn)擊列表切換,點(diǎn)擊按鈕切換等功能,感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04C語言不使用strcpy函數(shù)如何實(shí)現(xiàn)字符串復(fù)制功能
這篇文章主要給大家介紹了關(guān)于C語言不使用strcpy函數(shù)如何實(shí)現(xiàn)字符串復(fù)制功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02