+ 1

Overloading virtual function

class A{public: void f(){cout<<"A";}}; class B:public A{public: void f(){cout<<"B";}}; class C:public B{public: void f(){cout<<"C";}}; int main(){B *p = new C; p->f();} prints B. but when virtual void f() is declared in place of void f() in A, all else the same, prints C. would appreciate insight into this matter..thx

28th Nov 2017, 6:39 AM
Hansun Lee
1 Odpowiedź
+ 1
https://stackoverflow.com/questions/2391679/why-do-we-need-virtual-functions-in-c acc to the discussions here..seems like virtual keyword enables runtime(question here) polymorphism
30th Nov 2017, 6:13 AM
Hansun Lee