// Program indended to illustrate and discuss the rules of polymorphism in C++ // when we make use of private and public base classes (with multiple inheritance). // Answers in the next program - or per revealing of trailing comments. #include #include using namespace std; class B { private: int b; public: B(int b): b(b){} void Bop(){ cout << "Bop()" << endl; } }; class C { private: int c; public: C(int c): c(c){} void Cop(){ cout << "Cop()" << endl; } }; class D : private B, public C { private: int d; public: D(int b, int c, int d): B(b), C(c), d(d){} }; // Which are legal? We care about the static types of x, y, z, v, and w. Reveal... int f(){ B *x = new D(1,2,3); // Error: B is a private base. The D-object is not a B-object. C *y = new D(1,2,3); // OK: C is a public base. The D-object is a C-object. D *z = new D(1,2,3); // OK, of course. D *v = new B(1); // Error: Against rules of polymorphism. D *w = new C(1); // Error: Against rules of polymorphism. } int main(){ f(); }