#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;
};
//子类 需要继承Base1和Base2
class Son :public Base1, public Base2
{
public:
Son()
{
m_C = 300;
m_D = 400;
}
int m_C;
int m_D;
};
void test01()
{
Son s;
cout << "sizeof Son+" << sizeof(Son) << endl;
//当父类中出现同名成员,需要加作用域区分
//错误示例:
// cout << "m_A=" << s.m_A << endl;
//正确示例:
cout << "Base1::m_A=" << s.Base1::m_A << endl;
cout << "Base2::m_A=" << s.Base2::m_A << endl;
}
int main()
{
test01();
return 0;
}
子类继承多个父类时,当父类中有同名成员属性的时候,在访问父类成员属性时要加 作用域
启用
会出现
此时需要加作用域区分