C/C++中多重继承详解及其作用介绍
程序员文章站
2022-03-22 11:17:16
目录概述多重继承 (multiple inheritance): 一个派生类有两个或多个基类, 派生类从两个或多个基类中继承所需的属性. c++ 为了适应这种情况, 允许一个派生类同时继承多个基类....
概述
多重继承 (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++多重继承内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!