• 39 多重继承


    1 问题

    • C++ 中是否允许一个类继承自多个父类?

    2 C++ 中的多重继承

    • C++ 支持编写多重继承

      • 一个子类可以拥有多个父类
      • 子类拥有所有父类的成员变量
      • 子类继承所有父类的成员函数
      • 子类对象可以当作任意父类对象使用
    • 多重继承的语法规则

      • 多重继承的本质与单继承相同
      class Derived : public BaseA,
                      public BaseB,
                      public BaseC
      {
          //...
      };
      
    • 示例1:多重继承问题一

      • Demo

        #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 = " << mc << 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;
            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;
        }
        
      • 编译运行

        sizeof(Derived) = 12
        ma = 1, mb = 2, mc = 3
        d.getA() = 1
        d.getB() = 2
        d.getC() = 3
        
        pa->getA() = 1
        pa->getB() = 2
        
        Error
        pa = 0xbff39374
        pb = 0xbff39378
        paa = 0xbff39374
        pbb = 0xbff39378
        

    3 多重继承的问题

    • 问题一:通过多重继得到的对象可能拥有不同的地址

      • 解决方案:无!
    • 问题二:多重继承可能产生冗余的成员

      • 当多重继承关系出现闭合时,将产生数据冗余的问题
      • 解决方案:虚函数
      • 虚继承能够解决数据冗余的问题
      • 自建层父类不再关心顶层父类的初始化
      • 最终子类必须直接调用顶层父类的构造函数
  • 相关阅读:
    [转载]各种计算机语言的经典书籍
    [转载]VC 常用快捷键
    [转载]Visual Studio中的debug和release版本的区别
    [转载]Visual C++开发工具与调试技巧整理
    [转载]一个游戏程序员的学习资料
    [转载]C++资源之不完全导引(完整版)
    [转载]一个图形爱好者的书架/白话说学计算机图形学
    [摘录]这几本游戏编程书籍你看过吗?
    Oracle分析函数的使用
    [C/C++]C++下基本类型所占位数和取值范围
  • 原文地址:https://www.cnblogs.com/bky-hbq/p/13903943.html
Copyright © 2020-2023  润新知