// B5.cpp - redefine only base member functions that are virtual #include using namespace std; class B { public: void mf1() { cout << "\n B::mf1() called"; } void mf1(int i) { cout << "\n B::mf1(int i) called"; } virtual void mf2() { cout << "\n B::mf2() called"; } virtual ~B() { } }; class D : public B { public: void mf1() { cout << "\n D::mf1() called"; } // logical error void mf2() { cout << "\n D::mf2() called"; } virtual ~D() { } }; void main() { B b1; B *pB = &b1; pB->mf1(); pB->mf1(0); pB->mf2(); D d1; pB = &d1; cout << "\n-- redefining non-virtual functions breaks polymorphism"; pB->mf1(); pB->mf1(0); cout << "\n-- this next call is ok, redefined virtual function"; pB->mf2(); d1.mf1(); // works correctly //d1.mf1(5); // won't compile, hidden by D::mf1() }