// multinh.cc demonstrate multiple inheritance // design requirement is that B and C have their own A // compare this to virtinh.cc class A // base class for B and C { // indirect class for D public: int p, q; }; class B : public A // single inheritance { public: int q, r; // now have A::p A::q B::q B::r void f(int a){ A::q = a;} // because A::q won't be visible int g(){ return A::q;} }; class C : public A // another single inheritance { public: int q, s; // now have A::p A::q C::q C::s void f(int a){ A::q = a;} int g(){ return A::q;} }; class D : public B, public C // multiple inheritance { public: int q, t; // now have AB::p AB::q B::q B::r }; // AC::p AC::q C::q C::s // D::q D::t #include using namespace std; int main() { D stuff; // ten (10) integers stuff.B::p = 1; // really in A, ambiguous stuff.C::p = 2; // really in A, picked p from A in C // stuff.B::A::q = 3; // can not get to this one as object stuff.B::f(3); // set via a function in B // stuff.C::A::q = 4; // can not get to this one as object stuff.C::f(4); // set via a function in C stuff.B::q = 5; // the local q in B stuff.C::q = 6; // the local q in C stuff.D::q = 7; // the local q in D stuff.r = 8; // from B unambiguous stuff.s = 9; // from C unambiguous stuff.t = 10; // from D unambiguous cout << stuff.B::p << stuff.C::p << stuff.B::g() << stuff.C::g() << stuff.B::q << stuff.C::q << stuff.D::q << stuff.r << stuff.s << stuff.t << endl; return 0; // prints 12345678910 to show ten variables stored } // // functions are needed to get/set some variables // this is due to lack of syntax in present C++