inheritance - Calling overridden child method from base method in c++ -
here code:
#include <iostream> class { public: void foo() { cout<<"this base foo()"<<endl; } void callfoo() { foo(); } }; class b: public { public: void foo() { cout<<"this child foo()"<<endl; } }; int main() { b b; b.callfoo(); //output: "this base foo()" b.foo(); //output: "this child foo()" return 0; } now when call b.callfoo(), instead of calling foo() of child class b, callfoo() calls foo() of base class a. in class b have overridden foo() new implementation, still callfoo() calling base foo() instead of child foo(). please if have explanation behaviour.
in c++, need explicitly mark methods overridable adding virtual keyword: virtual void foo().
Comments
Post a Comment