/* Test of polymorphism as described in Gaddis */ /* Try this program with and without the word virtual in class A and explain what is happening and which is the least surprising */ #include using namespace std; class A{ public: // virtual void doit(){ cout << "A:: doit();\n"; } void it() { cout << "A:: it();\n"; doit(); /* which doit() is this? is it A::doit() or B::doit()?*/ } }; class B: public A{ public: void doit(){ cout << "B:: doit();\n"; } }; int main() { B b; b.doit(); b.it(); /* uses A::it() */ /* and if doit() is not virtual we get A::doit() not B::doit()*/ }