前一节的student示例程序看起来似乎已经解决了问题,但它存在着一些隐患。
首先,在TeachingStudent类的introduce()方法里,我们不得不明确告诉编译器应该使用哪一个属性。
这对于classes属性来说是应该的,因为教一门课和上一门课有点本质的区别,而作为常识,助教生教的课程和他学的课程不可能一样。但是既然在TeachingStudent对象里可以继承两个不同的classes属性,那它是不是应该有两个不同的name属性呢?
答案是肯定的,TeachingStudent可以有两个不同的名字,但这肯定不是我们在设计这个类继承模型时所预期的。
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(string theName);
void introduce();
protected:
string name;
};
class Teacher:public Person
{
public:
Teacher(string theName, string theClass);
void teach();
void introduce();
protected:
string classes;
};
class Student:public Person
{
public:
Student(string theName, string theClass);
void attendClass();
void introduce();
protected:
string classes;
};
class TeachingStudent:public Student, public Teacher
{
public:
TeachingStudent(string theName1, string theName2, string classTeaching, string classAttending);
void introduce();
};
Person::Person(string theName)
{
name = theName;
}
void Person::introduce()
{
cout << "大家好,我是" << name << endl;
}
Teacher::Teacher(string theName, string theClass):Person(theName)
{
classes = theClass;
}
void Teacher::teach()
{
cout << name << "教" << classes << endl;
}
void Teacher::introduce()
{
cout << "大家好,我是" << name << ",我教" << classes << endl;
}
Student::Student(string theName, string theClass):Person(theName)
{
classes = theClass;
}
void Student::attendClass()
{
cout << name << "加入" << classes << "学习.\n";
}
void Student::introduce()
{
cout << "大家好,我是" << name << ",我在" << classes << "学习.\n";
}
TeachingStudent::TeachingStudent(string theName1,
string theName2,
string classTeaching,
string classAttending)
:Teacher(theName1, classTeaching),
Student(theName2, classAttending)
{
}
void TeachingStudent::introduce()
{
cout << "大家好,我是" << Student::name << ",我教" << Teacher::classes << ",";
cout << "同时我在" << Student::classes << "学习.\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;
}
TeachingStudent类继承自Teacher和Student两个类,因而继承了两组Person类的属性,这在某些时候完全有道理,例如classes属性.但它也有可能引起麻烦,例如发生在name属性身上的情况。
因此提供了一个功能可以解决这个问题:虚继承 (virtual inheritance)
通过虚继承某个基类,就是在告诉编译器:从当前这个类再派生出来的子类只能拥有那个基类的一个实例
语法:
class Teacher:virtual public Person
{...}
这样做就解决了: 让Student和Teacher类都虚继承自Person类,编译器将确保从Student和Teacher类再派生出来的子类只能拥有一份Person类的属性。
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(string theName);
void introduce();
protected:
string name;
};
class Teacher:virtual public Person //改动
{
public:
Teacher(string theName, string theClass);
void teach();
void introduce();
protected:
string classes;
};
class Student:virtual public Person //改动
{
public:
Student(string theName, string theClass);
void attendClass();
void introduce();
protected:
string classes;
};
class TeachingStudent:public Student, public Teacher
{
public:
TeachingStudent(string theName, string classTeaching, string classAttending);
void introduce();
};
Person::Person(string theName)
{
name = theName;
}
void Person::introduce()
{
cout << "大家好,我是" << name << endl;
}
Teacher::Teacher(string theName, string theClass):Person(theName)
{
classes = theClass;
}
void Teacher::teach()
{
cout << name << "教" << classes << endl;
}
void Teacher::introduce()
{
cout << "大家好,我是" << name << ",我教" << classes << endl;
}
Student::Student(string theName, string theClass):Person(theName)
{
classes = theClass;
}
void Student::attendClass()
{
cout << name << "加入" << classes << "学习.\n";
}
void Student::introduce()
{
cout << "大家好,我是" << name << ",我在" << classes << "学习.\n";
}
TeachingStudent::TeachingStudent(string theName,
string classTeaching,
string classAttending)
:Teacher(theName, classTeaching),
Student(theName, classAttending),
Person(theName) //改动
{
}
void TeachingStudent::introduce()
{
cout << "大家好,我是" << name << ",我教" << Teacher::classes << ",";
cout << "同时我在" << Student::classes << "学习.\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;
}
有点没理解。