省流:class 子类 : 继承方式 父类1, 继承方式 父类2...{};
C++中允许一个类继承多个类
多继承可能会引发父类中有同名成员出现,需要加作用域
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #include<iostream> using namespace std;
class Base1 { public: Base1() { m_A = 100; } int m_A; };
class Base2 { public: Base2() { m_A = 200; } int m_A; };
class son :public Base1, public Base2 { public: son() { m_C = 300; m_D = 400; } int m_C; int m_D; };
int main() { son s; cout << "sizeof son= " << sizeof(s) << endl; cout << "m_A= " << s.Base1::m_A << endl; cout << "m_A= " << s.Base2::m_A << endl; return 0; }
|