C++允许一个类继承多个类

    语法:
    class 子类 :继承方式 父类1,继承方式 父类2 …

    多继承可能会引起父类中有同名成员出现,需要加作用域区分

    C++实际开发中不建议用多继承

    1. //多继承语法
    2. class Base1
    3. {
    4. public:
    5. Base1()
    6. {
    7. m_A = 100;
    8. }
    9. int m_A;
    10. };
    11. class Base2
    12. {
    13. public:
    14. Base2()
    15. {
    16. m_B = 200;
    17. m_A = 200;
    18. }
    19. int m_B;
    20. int m_A;
    21. };
    22. //子类 需要继承Base1和Base2
    23. //语法:class 子类 :继承方式 父类1,继承方式 父类2 ...
    24. class Son :public Base1, public Base2
    25. {
    26. public:
    27. Son()
    28. {
    29. m_C = 300;
    30. m_D = 400;
    31. }
    32. int m_C;
    33. int m_D;
    34. };
    35. void test01()
    36. {
    37. Son s;
    38. cout << "sizeof Son = " << sizeof(s) << endl;
    39. //当父类中出现同名成员,需要加作用域区分
    40. //cout << "m_A" << s.m_A << endl;
    41. cout << "Base1 m_A = " << s.Base1::m_A << endl;
    42. cout << "Base2 m_A = " << s.Base2::m_A << endl;
    43. }
    44. int main()
    45. {
    46. test01();
    47. system("pause");
    48. return 0;
    49. }