多继承
多继承(multiple inheritance)可能是面向对象编程技术中最惹人争议的功能了。 这个概念乍看起来很简单,但它可能引起一些难以预料的后果。 因此,JAVA 和 C# 等面向对象编程语言大都只支持多继承的最简单的版本。
什么时候需要用到多继承?
只要你遇到的问题无法只用一个”是一个”关系来描述的时候,就是多继承出场的时候。 举个栗子:在学校里有老师和学生,他们都是人(Person),我们可以用”老师是人”和”学生是人”语法来描述这种情况。 从面相对象编程角度上来看,我么应该创建一个名为 Person 的基类和两个名为 Teacher 和 Student 的子类,后两者是从前者继承来的。问题来了:有一部分学生还教课挣钱(助教),该怎么办?酱紫就存在了既是老师又是学生的复杂关系,也就是同时存在着两个”是一个”关系。我们需要写一个 TeschingStudent 类让它同时继承 Teacher 类和 Student 类,换句话说,就是需要使用多继承。基本语法:
class TeachingStudent : public Student, public Teacher
{ … }
下边我们写个示例演示一下这个多继承的模型!要求:创建一个由 Person, Teacher, Student 和 TeachingStudent 构成的类层次结构。Example:example.cpp
#include <iostream>
#include <string>
class Person
{
public:
Person(std::string theName);
void introduce();
protected:
std::string name;
};
class Teacher : public Person
{
public:
Teacher(std::string theName, std::string theClass);
void teach();
void introduce();
protected:
std::string classes;
};
class Student : public Person
{
public:
Student(std::string theName, std::string theClass);
void attendClass();
void introduce();
protected:
std::string classes;
};
class TeachingStudent : public Student, public Teacher
{
public:
TeachingStudent(std::string theName, std::string classTeaching, std::string classAttending);
void introduce();
};
Person::Person(std::string theName)
{
name = theName;
}
void Person::introduce()
{
std::cout << "大家好,我是" << name << "。\n\n";
}
Teacher::Teacher(std::string theName, std::string theClass) : Person(theName)
{
classes = theClass;
}
void Teacher::teach()
{
std::cout << name << "教" << classes << "。\n\n";
}
void Teacher::introduce()
{
std::cout << "大家好,我是" << name << ", 我教" << classes << "。\n\n";
}
Student::Student(std::string theName, std::string theClass) : Person(theName)
{
classes = theClass;
}
void Student::attendClass()
{
std::cout << name << "加入" << classes << "学习。\n\n";
}
void Student::introduce()
{
std::cout << "大家好,我是" << name << ", 我在" << classes << "学习。\n\n";
}
TeachingStudent::TeachingStudent(std::string theName,
std::string classTeaching,
std::string classAttending)
: Teacher(theName, classTeaching), Student(theName, classAttending)
{
}
void TeachingStudent::introduce()
{
std::cout << "大家好,我是" << Student::name << "。我教" << Teacher::classes << ", ";
std::cout << "同时我在" << Student::classes << "学习。\n\n";
}
int main()
{
Teacher teacher("小甲鱼", "C++入门班");
Student student("迷途羔羊", "C++入门班");
TeachingStudent teachingStudent("丁丁", "C++入门班", "C++进阶班");
teacher.introduce();
teacher.teach();
student.introduce();
student.attendClass();
teachingStudent.introduce();
teachingStudent.teach();
teachingStudent.attendClass();
return 0;
}
一些需要注意的地方:
- 在使用多继承的时候,一定要特别注意继承了基类的多少个副本。
- 在使用多继承的时候,最安全最简明的做法是从没有任何属性且只有抽象方法的类开始继承。
- 按照上边这么做可以让你远离后代子类可能拥有好几个基类属性的问题。 这样的类又叫做接口(interface)。

浙公网安备 33010602011771号