1.什么时候需要用到多继承呢?
只要你遇到的问题无法只用一个“是一个”关系来描述的时候,就是多继承出场的时候。
2.多继承定义
A为一个大类,B C是A的自己,那么BC就是A的子类;而存在一个类D,有B的步分也有C的部分,则D可以利用多继承来继承B与C类。
模版:
class C:public A,public B
{
//anything
};
class D: public A, private B, protected C
{
//类D新增加的成员
};
D 是多继承形式的派生类,它以公有的方式继承 A 类,以私有的方式继承 B 类,以保护的方式继承 C 类。D 根据不同的继承方式获取 A、B、C 中的成员,确定它们在派生类中的访问权限。并且可以变通。
3.特殊情况
命名冲突:当两个或多个基类中有同名的成员时,如果直接访问该成员,就会产生命名冲突,编译器不知道使用哪个基类的成员。这个时候需要在成员名字前面加上类名和域解析符::,以显式地指明到底使用哪个类的成员,消除二义性。下文代码中会注释标出~
子类方法覆盖:若子类和基类存在着相同的方法,子类会覆盖基类中的方法,导致实际编译的时候执行的只有子类中的方法,需要注意;可利用虚方法来减少此类情况造成的困扰。
4.多继承的实际地位
多继承容易让代码逻辑复杂、思路混乱,一直备受争议,中小型项目中较少使用,后来的 Java、C#、PHP 等干脆取消了多继承。不过有一说一,c++既然没有取消就学着吧,技多不压身嘛~
例子:
要求:创建一个由Person, Teacher, Student和TeachingStudent构成的类层次结构.
#include <iostream>
using namespace std;
class Person
{
public:
Person(string theName);//定义输入结构,虽然用不到
void introduce();
protected:
string name;
};
Person::Person(string theName)
{
name = theName;//定义 reference,避免报错
}
void Person::introduce()//此处的introduce会被下文子类中的相同方法覆盖,所以此条内的内容不会显示
{
cout << "我是" << name << endl;
cout << "反正也看不见,随便写点什么也无所谓吧!" << endl;
cout <<"-------------------------以上是person类的introduce" << endl;
}
class Teacher:public Person
{
public:
Teacher(string theName, string theClass);//定义输入结构
void teach();
void introduce();
protected:
string classes;
};
Teacher::Teacher(string theName, string theClass):Person(theName)
{
classes = theClass;//定义 reference,避免报错
}
void Teacher::teach()
{
cout << name << " teaching " << classes << endl;
cout <<"-------------------------以上是person子类teacher的teach" << endl;
}
void Teacher::introduce()
{
cout << "Hallo everyone,I am " << name << ",teaching" << classes << endl;
cout <<"-------------------------以上是perso子类teacher的introduce" << endl;
}
class Student:public Person
{
public:
Student(string theName, string theClass);//定义输入结构
void attendClass();
void introduce();
protected:
string classes;
};
Student::Student(string theName, string theClass):Person(theName)
{
classes = theClass;//定义 reference,避免报错
}
void Student::attendClass()
{
cout << name << " join in the" << classes << "learning~"<< endl;
cout <<"-------------------------以上是person子类studen的attendclass" << endl;
}
void Student::introduce()
{
cout << "Hi,I am " << name << ",a student learning in" << classes << endl;
cout <<"-------------------------以上是person子类studen的introduce" << endl;
}
class TeachingStudent:public Student,public Teacher
{
public:
TeachingStudent(string theName, string classTeaching, string classAttending);//定义输入结构
void introduce();
};
TeachingStudent::TeachingStudent(string theName,string classTeaching,string classAttending):Teacher(theName, classTeaching)
,Student(theName, classAttending)
{
//定义 reference,避免报错
}
void TeachingStudent::introduce()
{
cout << "hallo,I am " << Student::name << ".I am also a tutor teaching" << Teacher::classes << ".";
cout << "\nSametime,I am studying in" << Student::classes << "class" << endl;
cout <<"-------------------------以上是多继承tutor的introduce" << endl;
//name和classess命名冲突,用::消除二义性
}
int main()
{
Teacher teacher("A teacher", "class NO.1");
Student student("B student", "class No.1");
TeachingStudent teachingStudent("C tutor", "class NO.1", "class No.plus");
teacher.introduce();
teacher.teach();
student.introduce();
student.attendClass();
teachingStudent.introduce();
teachingStudent.teach();//多继承引用非本类中的函数,下同
teachingStudent.attendClass();
return 0;
}