inheritance,菱形继承, 虚继承,virtual
//菱形继承 ||||||| 虚继承
#include <iostream>
using namespace std;
class R
{
int r;
public:
R(int i=0):r(i)
{}
virtual void printOn()
{
cout<<"r = "<<r<<endl;
}
};
class B:public virtual R
{
int b;
public:
B(int i,int j):R(i),b(i){}
void printOn()
{
cout<<"b = "<<b<<endl;
R::printOn();
}
};
class C:public virtual R
{
int c;
public:
C(int i,int j):R(i),c(j){}
void printOn()
{
cout<<"c = "<<c<<endl;
R::printOn();
}
};
class D:public B,public C
{
int d;
public:
D(int i ,int j,int k,int y):R(j),B(i,j),C(j,k),d(y)
{
}
void printOn()
{
cout<<"d = "<<d<<endl;
R::printOn();
B::printOn();
C::printOn();
}
void f()
{
cout<<"d = "<<d<<endl;
R::printOn();
C::printOn();
}
};
int main()
{
D dd(2,3,4,5);
C cc(9,9);
B bb(6,5);
R * rr;
dd.printOn();
cout<<"==="<<endl;
cc.printOn();
bb.printOn();
rr=&cc;
rr->printOn();
return 0;
}