---- 整理自狄泰软件唐佐林老师课程
C++是否允许一个类继承自多个父类?


#include
#include 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;}void print(){cout << "ma = " << getA() << ", "<< "mb = " << getB() << ", "<< "mc = " << mc << endl;}
};int main()
{cout << "sizeof(Derived) = " << sizeof(Derived) << endl; // 12Derived d(1, 2, 3);d.print();cout << "d.getA() = " << d.getA() << endl;cout << "d.getB() = " << d.getB() << endl;cout << "d.getC() = " << 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* paa = pa;void* pbb = pb;if( paa == pbb ){cout << "Pointer to the same object!" << endl; }else{cout << "Error" << endl;}cout << "pa = " << pa << endl;cout << "pb = " << pb << endl;cout << "paa = " << paa << endl;cout << "pbb = " << pbb << endl; return 0;
}


通过 多重继承 得到的对象可能拥有 “不同的地址”


Doctor中有两个m_name、m_age
#include
#include using namespace std;class People
{string m_name;int m_age;
public:People(string name, int age){m_name = name;m_age = age;}void print(){cout << "Name = " << m_name << ", "<< "Age = " << m_age << 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){}
};int main()
{Doctor d("Delphi", 33);d.print();return 0;
}

当多重继承关系出现闭合时将产生数据冗余的问题

虚继承能够解决 数据冗余 问题
中间层父类 不再关心顶层父类的初始化(中间层父类不调用父类构造函数)
最终子类 必须直接调用 顶层父类的构造函数
问题:当架构设计中需要继承时,无法确定使用直接继承还是虚继承
多重继承可能产生 多个虚函数表

编程实验:多重继承问题三
#include
#include using namespace std;class BaseA
{
public:virtual void funcA(){cout << "BaseA::funcA()" << endl;}
};class BaseB
{
public:virtual void funcB(){cout << "BaseB::funcB()" << endl;}
};class Derived : public BaseA, public BaseB
{};int main()
{Derived d;BaseA* pa = &d;BaseB* pb = &d;BaseB* pbe = (BaseB*)pa; // oops!!BaseB* pbc = dynamic_cast(pa);cout << "sizeof(d) = " << sizeof(d) << endl;cout << "Using pa to call funcA()..." << endl;pa->funcA();cout << "Using pb to call funcB()..." << endl;pb->funcB();cout << "Using pbc to call funcB()..." << endl;pbc->funcB();cout << endl;cout << "pa = " << pa << endl;cout << "pb = " << pb << endl;cout << "pbe = " << pbe << endl;cout << "pbc = " << pbc << endl;return 0;
}

需要进行强制类型转换时,C++中推荐使用新式类型转换关键字
解决方案:dynamic_cast



#include
#include using namespace std;class Base
{
protected:int mi;
public:Base(int i){mi = i;}int getI(){return mi;}bool equal(Base* obj){return (this == obj);}
};class Interface1
{
public:virtual void add(int i) = 0;virtual void minus(int i) = 0;
};class Interface2
{
public:virtual void multiply(int i) = 0;virtual void divide(int i) = 0;
};class Derived : public Base, public Interface1, public Interface2
{
public:Derived(int i) : Base(i){}void add(int i){mi += i;}void minus(int i){mi -= i;}void multiply(int i){mi *= i;}void divide(int i){if( i != 0 ){mi /= i;}}
};int main()
{Derived d(100);Derived* p = &d;Interface1* pInt1 = &d;Interface2* pInt2 = &d;cout << "p->getI() = " << p->getI() << endl; // 100pInt1->add(10);pInt2->divide(11);pInt1->minus(5);pInt2->multiply(8);cout << "p->getI() = " << p->getI() << endl; // 40cout << endl;cout << "pInt1 == p : " << p->equal(dynamic_cast (pInt1)) << endl;cout << "pInt2 == p : " << p->equal(dynamic_cast (pInt2)) << endl;return 0;
}

