第53课.被遗弃的多重继承(上)

1.c++中的多重继承

a.c++支持编写多重继承的代码
b.一个子类可以拥有多个父类
c.子类拥有父类的成员变量
d.子类继承所有父类的成员函数
e.子类对象可以当做任意父类对象使用(退化)

语法规则:

class Derived : public BaseA,
                public BaseB,
                public BaseC
{
    // 多重继承的本质与单继承相同
}

2.多重继承的问题一

多重继承时得到的对象可能拥有不同的地址。

#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;
    }
    
    void print ()
    {
        cout << "ma = " << getA() << ", "
             << "mb = " << getB() << ", "
             << "mc = " << getC() << ", " << endl;  
    }
};

int main()
{
    cout << "sizeof(Derived) = " << sizeof(Derived) << endl;    // 12, 子类是父类的叠加
    
    Derived 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;     // 把b对象的地址给pa指针  (退化)
    BaseB* pb = &d;     // 把b对象的地址给pb指针  这里是同一个对象理论上来说,地址应该是一样的。

    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;        // 输出,这表示。pa和pb从d中获得到地址不同
    }

    cout << "pa = " << pa << endl;
    cout << "pb = " << pb << endl;
    cout << "paa = " << paa << endl;
    cout << "pbb = " << pbb << endl; 
    
    return 0;
}

解析:

 多重继承时得到的对象可能拥有不同的地址。如下图所示。
pa为BaseA指针。它得到的地址为叠加时,它的成员在子类中存储的位置。
同理:pb为BaseB指针。它的得到的地址为叠加时,它的成员在子类中的存储位置

解决办法:无

3.多重继承的问题二

多重继承可能产生冗余的成员

eg:

#include <iostream>
#include <string>

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 : 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 ("Huang", 24);
    
    d.print();
    
    // d.Teacher::print();      // 可行,分开引用
    // d.Student::print();
    
    return 0;
}

解析:这里把People这个类叠加了两次。在Tacher和Student中各自叠加两次后。在Doctor中叠加。所以会出现两个print函数。函数调用时,系统不知道到底要调用Tacher中叠加的print函数还是Student中的。

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

解决办法:虚继承

a.虚继承能够解决数据冗余问题
b.中间层父类不在关心顶层的初始化(但是还是得正常写)
c.最终子类必须直接调用顶层父类的构造函数

eg:

class People
{
};
// 因为继承关系在这里开始闭合(这里继承最顶层父类),所以在这里使用虚继承
class Tacher : virtual public People
{
};
// 因为继承关系在这里开始闭合(这里继承最顶层父类),所以在这里使用虚继承
class Student : virtual public People
{
};

class Doctor : public Tacher, public Student
{
public:
    Doctor (string name, int age) : Teacher(name, age), Student(name, age), People(name, age)
    {
    }
};
posted @ 2019-12-05 16:17  人民广场的二道贩子  阅读(340)  评论(0编辑  收藏  举报