前一节的 student 示例程序看起来似乎已经解决了问题,但它存在着一些隐患。 首先,在 TeachingStudent 类的 introduce() 方法里,我们不得不明确地告诉编译器应该使用哪一个属性。 这对于 classes 属性来说是应该的,因为教一门课和上一门课有着本质的区别,而作为常识,助教生教的课程和他学的课程不可能一样!
但是我们再深入考虑下,既然在 TeachingStudent 对象里可以继承两个不同的 classes 属性,那它是不是应该有两个不同的 name 属性呢? 答案:是!事实上,TeachingStudent 还真可以有两个不同的名字,这肯定不是我们在设计这个类继承模型时所预期的:Example1.cpp
TeachingStudent 类继承自 Teacher 和 Student 两个类,因而继承了两组 Person 类的属性,这在某些时候完全有道理,例如 classes 属性。但它也有可能引起麻烦,例如发生在 name 属性身上的情况。
#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 theName1, std::string theName2, std::string classTeaching, std::string classAttending); void introduce(); }; Person::Person(std::string theName) { name = theName; } void Person::introduce() { std::cout << "大家好,我是" << name << "。 "; } Teacher::Teacher(std::string theName, std::string theClass) : Person(theName) { classes = theClass; } void Teacher::teach() { std::cout << name << "教" << classes << "。 "; } void Teacher::introduce() { std::cout << "大家好,我是" << name << ", 我教" << classes << "。 "; } Student::Student(std::string theName, std::string theClass) : Person(theName) { classes = theClass; } void Student::attendClass() { std::cout << name << "加入" << classes << "学习。 "; } void Student::introduce() { std::cout << "大家好,我是" << name << ", 我在" << classes << "学习。 "; } TeachingStudent::TeachingStudent(std::string theName1, std::string theName2, std::string classTeaching, std::string classAttending) : Teacher(theName1, classTeaching), Student(theName2, classAttending) { } void TeachingStudent::introduce() { std::cout << "大家好,我是" << Student::name << "。我教" << Teacher::classes << ", "; std::cout << "同时我在" << Student::classes << "学习。 "; } 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; }
结果:
大家好,我是小甲鱼, 我教C++入门班。 小甲鱼教C++入门班。 大家好,我是迷途羔羊, 我在C++入门班学习。 迷途羔羊加入C++入门班学习。 大家好,我是丹丹。我教C++入门班, 同时我在C++进阶班学习。 丁丁教C++入门班。 丹丹加入C++进阶班学习。 请按任意键继续. . .
TeachingStudent 类继承自 Teacher 和 Student 两个类,因而继承了两组 Person 类的属性,这在某些时候完全有道理,例如 classes 属性。但它也有可能引起麻烦,例如发生在 name 属性身上的情况。
C++ 发明者也想到了这部分的冲突,因此为此提供了一个功能可以解决这个问题:虚继承(virtual inheritance) 通过虚继承某个基类,就是在告诉编译器:从当前这个类再派生出来的子类只能拥有那个基类的一个实例。虚继承的语法:
class Teacher : virtual public Person { … }
这样做我们的问题就解决了:让 Student 和 Teacher 类都虚继承自 Person 类,编译器将确保从 Student 和 Teacher 类再派生出来的子类只能拥有一份 Person 类的属性!
栗子修改:Example2.cpp
#include <iostream> #include <string> class Person { public: Person(std::string theName); void introduce(); protected: std::string name; }; class Teacher : virtual public Person//虚继承 { public: Teacher(std::string theName, std::string theClass); void teach(); void introduce(); protected: std::string classes; }; class Student : virtual 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 << "。 "; } Teacher::Teacher(std::string theName, std::string theClass) : Person(theName) { classes = theClass; } void Teacher::teach() { std::cout << name << "教" << classes << "。 "; } void Teacher::introduce() { std::cout << "大家好,我是" << name << ", 我教" << classes << "。 "; } Student::Student(std::string theName, std::string theClass) : Person(theName) { classes = theClass; } void Student::attendClass() { std::cout << name << "加入" << classes << "学习。 "; } void Student::introduce() { std::cout << "大家好,我是" << name << ", 我在" << classes << "学习。 "; } TeachingStudent::TeachingStudent(std::string theName, std::string classTeaching, std::string classAttending) : Teacher(theName, classTeaching), Student(theName, classAttending), Person(theName)//继承基类名字 { } void TeachingStudent::introduce() { std::cout << "大家好,我是" << name << "。我教" << Teacher::classes << ", "; std::cout << "同时我在" << Student::classes << "学习。 "; } 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; }