#include <iostream>
#include<cstring>
using namespace std;
class Base{
public:
void fun(){
cout<<"this"<<endl;
}
protected:
int a;
};
class Base1:public virtual Base{ //
public:
// void fun(int a){
// cout<<"This is Base1."<<endl;
// }
protected:
// int base1,common;
int b;
};
class Base2:public virtual Base{
public:
// void fun(){
// cout<<"This is Base2."<<endl;
// }
protected:
// int base2;
// int common;
int c;
};
class Derived:public Base1,public Base2{
public:
// void setBase1(int b1){base1=b1;}
// void setBase2(int b2){base2=b2;}
// void setCommon(int c){Base1::common=Base2::common=c;}
// void show(){cout<<base1<<" "<<base2<<endl;}
// 改造 void fun(){
// cout<<"This is derived."<<endl;
// }
void setA(int _a){a=_a;}
void show(){cout<<a<<""<<a<<endl;}
protected:
int derived;//扩充
};
int main()
{
Derived derived;
// derived.Base1::fun();
// derived.Base2::fun();//多继承冲突时,加类名;
// derived.setBase1(10);
// derived.setBase2(20);
// derived.show();
derived.fun();//不在同一个类里面不能构成重载,无法避免多继承冲突;
}
多继承虚函数
最新推荐文章于 2024-08-11 17:35:45 发布