C/C++中多重继承详解及其作用介绍
更新时间:2021年9月7日 00:01 点击:1498
概述
多重继承 (multiple inheritance): 一个派生类有两个或多个基类, 派生类从两个或多个基类中继承所需的属性. C++ 为了适应这种情况, 允许一个派生类同时继承多个基类. 这种行为称为多重继承.
优缺点
优点
- 自然地做到了对单继承的扩展
- 可以继承多个类的功能
缺点
- 结构复杂化
- 优先顺序模糊
- 功能冲突
声明多重继承的方法
格式
多重继承的格式:
派生类构造函数名(总形式参数表列): 基类1构造函数(实际参数表列), 基类2构造函数(实际参数表列), 基类3构造函数(实际参数表列) { 派生类中新增数成员据成员初始化语句 }
例子
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; }
输出结果:
Teacher name: 王叔叔 age: 18 title: 隔壁老王 Student name: 我是小白呀 gender: f score: 99
二义性
二义性 (Ambiguity) 指在多重继承中, 两个基类中的数据成员名相同.
二义性在派生类中的解决方法:
- 在标识符前用类名做前缀: Teacher::name 和 Student::name
- 基类和派生类需要有一个完整的设计, 不能随意而为
两个基类有同名成员
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; }
输出结果:
A's num:1 B's num:2
错误的写法
#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; }
输出结果:
C's num:3 A's num:1 B's num:2
同名覆盖:
- 基类的同名成员在派生类中被屏蔽, 成为 "不可见"的
- 对成员函数, 限于函数名和参数个数相同, 类型相匹配. 若只有函数名相同而参数不同, 属于函数重载
两个基类从同一个基类派生
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; }
输出结果:
A::a=3
到此这篇关于C/C++中多重继承详解及其作用介绍的文章就介绍到这了,更多相关C++多重继承内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
下一篇: 一文弄懂C语言如何实现单链表
相关文章
- vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
- 这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
- 整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
- 这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
- 这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
- 这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
- 这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
- 虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
- 这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
- 这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15
- 这篇文章主要介绍了C++中cin的用法详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本篇文章是对C++中的常见编译错误进行了详细的分析介绍,需要的朋友参考下...2020-04-25
- 这篇文章主要介绍了c++优先队列(priority_queue)用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25