菱形继承
菱形继承:指有一个基类被两个不同的类所继承,且存在一个类继承于这两个类而形成的一种菱形关系,故称菱形继承。如下图所示:
如上图,Person类由Student类和Staff类继承,且有一个Stu_Staf类继承这两个类。代码结构会是这样的:
class Person
{
public:
string name;
string id;
int age;
string sex;
Person(string na,string idd,int n,string se):name(na),id(idd),age(n),sex(se)
{
cout << "Person()" << endl;
}
~Person() { cout << "~Person()" << endl; }
void print()
{
cout << "name:" << name << endl;
cout << "id:" << id << endl;
cout << "age:" << age << endl;
cout << "sex:" << sex << endl;
}
};
class Student:public Person
{
public:
int stu_id;
string school;
Student(string na, string idd, int n,string se,int sid,string sc):Person(na,idd,n,se),stu_id(sid),school(sc)
{
cout << "Student()" << endl;
}
~Student() { cout << "~Student()" << endl; }
void print()
{
Person::print();
cout << "stu_id:" << stu_id << endl;
cout << "school:" << school << endl;
}
};
class Staff :public Person
{
public:
int staff_id;
string positon;
Staff(string na, string idd, int n,string se, int sid, string sc) :Person(na, idd, n,se), staff_id(sid), positon(sc)
{
cout << "Staff()" << endl;
}
~Staff() { cout << "~Staff()" << endl; }
void print()
{
Person::print();
cout << "stu_id:" << staff_id << endl;
cout << "school:" << positon << endl;
}
};
class Stu_staf :public Student, public Staff
{
public:
Stu_staf(string na, string idd, int n,int sid, string sc, int tid, string po) :
Student(na, idd, n, "男", sid, sc), Staff(na, idd, n, "女", tid, po)
{
cout << "Stu_staf()" << endl;
}
~Stu_staf() { cout << "~Stu_staf()" << endl; }
void print()
{
Student::print();
Staff::print();
}
};
int main()
{
Stu_staf st("lisa", "610134571", 18, 19310671, "xaufe", 9101, "程序员");
st.print();
return 0;
}
在上面的示例中,我专门将Student的性别设置为男,Staff的性别设置为女,则会出现一个Stu_staf实例的属性二义性的问题:
并且,当我们想对Person类的属性进行赋值时,就会出现属性冲突,因为编译器并不清楚你要给Student类继承Person中的age赋值还是Staff类继承Person的age赋值,因此编译器会直接不通过,如下:
因为此时,Stu_staf类的内存分布是这样的:
可以看到,一个Stu_staf类对象是有两份Person类成员数据的,因此,编译器无法判断你需要使用的是哪一个,故会出现冲突。
那么如何解决这个问题呢?这就引入了虚继承。(上一篇博客中已经讲过它的定义了,就不赘述了)
具体来看看怎么解决这个问题:
class Person
{
public:
string name;
string id;
int age;
string sex;
Person(string na,string idd,int n,string se):name(na),id(idd),age(n),sex(se)
{
cout << "Person()" << endl;
}
~Person() { cout << "~Person()" << endl; }
void print()
{
cout << "name:" << name << endl;
cout << "id:" << id << endl;
cout << "age:" << age << endl;
cout << "sex:" << sex << endl;
}
};
class Student:virtual public Person
{
public:
int stu_id;
string school;
Student(string na, string idd, int n,string se,int sid,string sc):Person(na,idd,n,se),stu_id(sid),school(sc)
{
cout << "Student()" << endl;
}
~Student() { cout << "~Student()" << endl; }
void print()
{
Person::print();
cout << "stu_id:" << stu_id << endl;
cout << "school:" << school << endl;
}
};
class Staff :virtual public Person
{
public:
int staff_id;
string positon;
Staff(string na, string idd, int n,string se, int sid, string sc) :Person(na, idd, n,se), staff_id(sid), positon(sc)
{
cout << "Staff()" << endl;
}
~Staff() { cout << "~Staff()" << endl; }
void print()
{
Person::print();
cout << "stu_id:" << staff_id << endl;
cout << "school:" << positon << endl;
}
};
class Stu_staf :public Student, public Staff
{
public:
Stu_staf(string na, string idd, int n,int sid, string sc, int tid, string po) :Person(na, idd, n, "男"),
Student(na, idd, n, "女", sid, sc), Staff(na, idd, n, "女", tid, po)
{
cout << "Stu_staf()" << endl;
}
~Stu_staf() { cout << "~Stu_staf()" << endl; }
void print()
{
Student::print();
Staff::print();
}
};
这个时候,他会依照最早的顺序来进行构造,且不会出现二义性问题和冲突,Person类的属性也可以进行赋值了:
菱形继承问题就很好的解决了。