C++ //多继承语法 C++中允许一个类继承多个类
1 //多继承语法 C++中允许一个类继承多个类 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 6 class Base1 7 { 8 public: 9 Base1() 10 { 11 m_A = 100; 12 } 13 int m_A; 14 }; 15 16 class Base2 17 { 18 public: 19 Base2() 20 { 21 m_A = 200; 22 } 23 int m_A; 24 }; 25 26 //子类 需要继承Base1 和 Base2 27 //语法:class 子类 :继承方式 父类1 逗号,继承方式 父类2...... 28 class Son :public Base1, public Base2 29 { 30 public: 31 Son() 32 { 33 m_C = 300; 34 m_D = 400; 35 } 36 int m_C; 37 int m_D; 38 }; 39 40 void test01() 41 { 42 Son s; 43 cout << "sizeof Son = " << sizeof(s) << endl; //16 44 45 //当父类中出现同名成员 需要加作用域区分 46 cout << "Base1-m_A = " << s.Base1::m_A << endl; 47 cout << "Base2-m_A = " << s.Base2::m_A << endl; 48 } 49 int main() 50 { 51 52 test01(); 53 54 55 system("pause"); 56 57 return 0; 58 59 }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15114438.html