0. 问题:C++中是否允许一个类继承自多个父类?
C++支持编写多重继承的代码:
(1) 一个子类可以拥有多个父类
(2) 子类拥有所有父类的成员变量
(3) 子类继承所有父类的成员函数
(4) 子类对象可以当作任意父类对象使用
多重继承的本质与单继承相同
1. 多重继承问题一:通过多重继承得到的对象可能拥有 “不同的地址”
编程说明:多重继承问题一:通过多重继承得到的对象可能拥有 “不同的地址”
#include <iostream>
#include <string>
using namespace std;
class BaseA
{
int ma;
public:
BaseA(int a)
{
ma = a;
}
int getA()
{
return ma;
}
};
class BaseB
{
int mb;
public:
BaseB(int b)
{
mb = b;
}
int getB()
{
return mb;
}
};
class Derived : public BaseA, public BaseB // 多重继承
{
int mc;
public:
Derived(int a, int b, int c) : BaseA(a) , BaseB(b)
{
mc = c;
}
int getC()
{
return mc;
}
};
int main()
{
Derived d(1, 2, 3);
cout << d.getA() << endl;
cout << d.getB() << endl;
cout << d.getC() << endl;
cout << endl;
BaseA* pa = &d;
BaseB* pb = &d;
cout << "pa->getA() = " << pa->getA() << endl;
cout << "pb->getB() = " << pb->getB() << endl;
cout << endl;
void* ppa = pa;
void* ppb = pb;
if( ppa == ppb)
{
cout << "pa == pb" << endl;
}
else
{
cout << "pa != pb" << endl;
}
cout << "pa = " << pa << endl;
cout << "pb = " << pb << endl;
return 0;
}
输出结果:
1
2
3
pa->getA() = 1
pb->getB() = 2
pa != pb
pa = 0xbfd49424
pb = 0xbfd49428
总结: 通过多重继承得到的对象可能拥有不同的地址!!!
2. 多重继承问题二:多重继承可能产生冗余的成员
编程说明:多重继承问题二:多重继承可能产生冗余的成员
#include <iostream>
#include <string>
using namespace std;
class People
{
string mName;
int mAge;
public:
People(string name, int age)
{
mName = name;
mAge = age;
}
void print()
{
cout << "Name = " << mName << ", "
<< "Age = " << mAge << endl;
}
};
class Teacher : public People
{
public:
Teacher(string name, int age) : People(name, age)
{
}
};
class Student : public People
{
public:
Student(string name, int age) : People(name, age)
{
}
};
class Doctor : public Teacher, public Student
{
public:
Doctor(string name, int age) : Teacher(name, age), Student(name, age)
{
}
};
int main()
{
Doctor d("jacob", 30);
// d.print(); // error: request for member 'print()' is ambiguous
d.Teacher::print();
d.Student::print();
return 0;
}
输出结果:
Name = jacob, Age = 30
Name = jacob, Age = 30
总结: 当多重继承关系出现闭合时将产生数据冗余的问题!!!
解决方案:虚继承
(1) 虚继承能够解决数据冗余问题
(2) 中间层父类不再关心顶层父类的初始化,由子类直接初始化
(3) 最终子类必须直接调用顶层父类的构造函数
编程说明:虚继承
#include <iostream>
#include <string>
using namespace std;
class People
{
string mName;
int mAge;
public:
People(string name, int age)
{
mName = name;
mAge = age;
}
void print()
{
cout << "Name = " << mName << ", "
<< "Age = " << mAge << endl;
}
};
class Teacher : virtual public People
{
public:
Teacher(string name, int age) : People(name, age)
{
}
};
class Student : virtual public People
{
public:
Student(string name, int age) : People(name, age)
{
}
};
class Doctor : public Teacher, public Student
{
public:
Doctor(string name, int age) : Teacher(name, age), Student(name, age), People(name, age)
{
}
};
s
int main()
{
Doctor d("jacob", 30);
d.print();
return 0;
}
输出结果:
Name = jacob, Age = 30
虚继承在技术上解决了多重继承的冗余问题,但是在大型项目开发中,使用虚继承时需要查找父类时是困难的,开发的复杂性增加。
在架构设计中,需要继承时,无法确定使用直接继承还是虚继承。
在工程项目开发中,不使用多重继承
3. 小结
- C++支持多重继承的编程方式
- 多重继承容易带来问题:
(1) 可能出现同一个对象的地址不同的情况
(2) 虚继承可以解决数据冗余的问题
(3) 虚继承的使得架构师设计可能出现问题