// virtinh.cc demonstrate multiple inheritance with 'virtual' // design requirement needs only one copy of A in D // compare this to multinh.cc class A // base class for B and C { // indirect class for D public: int p, q; }; class B : virtual public A // single inheritance, virtual { public: int q, r; // now have A::p A::q B::q B::r }; class C : virtual public A // another single inheritance, virtual { public: int q, s; // now have A::p A::q C::q C::s }; class D : public B, public C // multiple inheritance, only one A { // class inherited because B, C virtuals public: int q, t; // now have A::p A::q B::q B::r }; // C::q C::s D::q D::t #include using namespace std; int main() { D stuff; // eight (8) integers stuff.p = 1; // the local p in A stuff.A::q = 2; // the local q in A note four (4) q's stuff.B::q = 3; // the local q in B stuff.C::q = 4; // the local q in C stuff.q = 5; // the local q in D stuff.r = 6; // from B unambiguous stuff.s = 7; // from C unambiguous stuff.t = 8; // from D unambiguous cout << stuff.p << stuff.A::q << stuff.B::q << stuff.C::q << stuff.D::q << stuff.r << stuff.s << stuff.t << '\n'; return 0; // prints 12345678 to show eight variables stored }