2026/4/28 6:18:21
网站建设
项目流程
概述
多重继承 (multiple inheritance): 一个派生类有两个或多个基类, 派生类从两个或多个基类中继承所需的属性. C++ 为了适应这种情况, 允许一个派生类同时继承多个基类. 这种行为称为多重继承.
![]()
优缺点
优点
缺点
声明多重继承的方法
格式
多重继承的格式:
1 2 3 4 5 6 7 | 派生类构造函数名(总形式参数表列):
基类1构造函数(实际参数表列),
基类2构造函数(实际参数表列),
基类3构造函数(实际参数表列)
{
派生类中新增数成员据成员初始化语句
}
|
例子
Teacher 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #ifndef PROJECT5_TEACHER_H
#define PROJECT5_TEACHER_H
#include <string>
usingnamespacestd;
classTeacher {
protected:
string name;
intage;
string title;
public:
Teacher(string n,inta, string t);
voiddisplay_teacher();
};
#endif //PROJECT5_TEACHER_H
|
Teacher.cpp:
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream>
#include "Teacher.h"
usingnamespacestd;
Teacher::Teacher(string n,inta, string t) : name(n), age(a), title(t) {}
voidTeacher::display_teacher() {
cout <<"Teacher name: "<< name << endl;
cout <<"age: "<< age << endl;
cout <<"title: "<< title << endl;
}
|
Student 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #ifndef PROJECT5_STUDENT_H
#define PROJECT5_STUDENT_H
#include <string>
usingnamespacestd;
classStudent {
protected:
string name;
chargender;
doublescore;
public:
Student(string n,charg,doubles);
voiddisplay_student();
};
#endif //PROJECT5_STUDENT_H
|
Student.cpp:
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream>
#include "Student.h"
usingnamespacestd;
Student::Student(string n,charg,doubles) : name(n), gender(g), score(s) {}
voidStudent::display_student() {
cout <<"Student name: "<< name << endl;
cout <<"gender: "<< gender << endl;
cout <<"score: "<< score << endl;
}
|
Graduate 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #ifndef PROJECT5_GRADUATE_H
#define PROJECT5_GRADUATE_H
#include "Teacher.h"
#include "Student.h"
#include <string>
usingnamespacestd;
classGraduate :publicTeacher,publicStudent{
private:
doublewage;
public:
Graduate(string t_n,intt_a, string t_t, string s_n,chars_g,doubles_s);
voiddisplay_graduate();
};
#endif //PROJECT5_GRADUATE_H
|
Graduate.cpp:
1 2 3 4 5 6 7 8 9 10 | #include "Graduate.h"
Graduate::Graduate(string t_n,intt_a, string t_t, string s_n,chars_g,doubles_s) :
Teacher(t_n, t_a, t_t),
Student(s_n, s_g, s_s) {}
voidGraduate::display_graduate() {
display_teacher();
display_student();
}
|
main:
1 2 3 4 5 6 7 8 9 10 | #include <iostream>
#include "Graduate.h"
usingnamespacestd;
intmain() {
Graduate graduate1("王叔叔", 18,"隔壁老王","我是小白呀",'f', 99);
graduate1.display_graduate();
return0;
}
|
输出结果:
1 2 3 4 5 6 | Teacher name: 王叔叔
age: 18
title: 隔壁老王
Student name: 我是小白呀
gender: f
score: 99
|
二义性
二义性 (Ambiguity) 指在多重继承中, 两个基类中的数据成员名相同.
![]()
二义性在派生类中的解决方法:
- 在标识符前用类名做前缀: Teacher::name 和 Student::name
- 基类和派生类需要有一个完整的设计, 不能随意而为
两个基类有同名成员
![]()
A 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #ifndef PROJECT5_A_H
#define PROJECT5_A_H
#include <iostream>
usingnamespacestd;
classA {
public:
intnum;
voiddisplay() {cout <<"A's num:"<< num << endl;};
};
#endif //PROJECT5_A_H
|
B 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #ifndef PROJECT5_B_H
#define PROJECT5_B_H
#include <iostream>
usingnamespacestd;
classB {
public:
intnum;
voiddisplay() {cout <<"B's num:"<< num << endl;};
};
#endif //PROJECT5_B_H
|
C 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #ifndef PROJECT5_C_H
#define PROJECT5_C_H
#include <iostream>
#include "A.h"
#include "B.h"
usingnamespacestd;
classC:publicA,publicB{
public:
intc;
voiddisplay() {cout << c << endl;};
};
#endif //PROJECT5_C_H
|
main:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <iostream>
#include "C.h"
usingnamespacestd;
intmain() {
C c1;
c1.A::num = 1;// 用基类名限定
c1.B::num = 2;// 用基类名限定
c1.A::display();
c1.B::display();
return0;
}
|
输出结果:
错误的写法
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream>
#include "C.h"
usingnamespacestd;
intmain() {
C c1;
c1.num = 1;
c1.display();
return0;
}
|
基类和派生类有同名成员
A 类:
1 2 3 4 5 | classA {
public:
intnum;
voiddisplay() {cout <<"A's num:"<< num << endl;};
};
|
B 类:
1 2 3 4 5 | classB {
public:
intnum;
voiddisplay() {cout <<"B's num:"<< num << endl;};
};
|
C 类:
1 2 3 4 5 | classC:publicA,publicB{
public:
intnum;
voiddisplay() {cout <<"C's num:"<< num << endl;};
};
|
main:
1 2 3 4 5 6 7 8 9 10 11 | intmain() {
C c1;
c1.num = 3;
c1.A::num = 1;
c1.B::num = 2;
c1.display();
c1.A::display();
c1.B::display();
return0;
}
|
输出结果:
1 2 3 | C's num:3
A's num:1
B's num:2
|
同名覆盖:
- 基类的同名成员在派生类中被屏蔽, 成为 "不可见"的
- 对成员函数, 限于函数名和参数个数相同, 类型相匹配. 若只有函数名相同而参数不同, 属于函数重载