I am trying to understand the concept of virtual function in C++ and I read it online but I am not able to understand why the below program output is 2 instead of 1? Can anyone explain?
Class A { int a; public: A() { a = 1; } virtual void show() { cout <<a; } }; Class B: public A { int b; public: B() { b = 2; } virtual void show() { cout <<b; } }; int main() { A *pA; B oB; pA = &oB; pA->show(); return 0; }
1actually?virtualkeywords and you'll have what you want: 1.