C++是支持多继承的
语法: class 子类:继承方式 父类1,继承方式 父类2…
多继承可能会引发父类中有同名成员出现,需要加作用域区分,C++实际开发中不建议用多继承
class Base1
{
public :
Base1()
{
p_a = 100;
}
int p_a;
};
class Base2
{
public:
Base2()
{
p_b = 200;
}
int p_b;
};
class Base3
{
public:
Base3()
{
p_c = 200;
}
int p_c;
};
class Son : public Base2, public Base1, Base3
{
public:
Son()
{
p_c = 400;
p_d = 500;
}
int p_c;
int p_d;
};
//多继承容易产生成员同名的情况
//通过使用类名作用域可以区分调用哪一个基类的成员
void test01()
{
Son s;
cout << "sizeof Son = " << sizeof(s) << endl;
cout << s.Base1::p_a << endl;
cout << s.Base2::p_b << endl;
}
int main() {
test01();
system("pause");
return 0;
}