>

Thursday, 17 December 2015

Call Derived Class method from Base Class

class Base
{
public:
virtual void f1(){cout<<"Base F1"<<endl;}
void f2(){f1();}
};
class Derived:public Base
{
public:
void f1(){cout<<"derived F1"<<endl;}
};
int main()
{
Base *b;
Derived d;
b = &d;
d.f2();
return 0;
}
To call a method in derived class from Base class,declare the method in derived Class as virtual in base class and call that method using upcasted pointer of derived class.

0 comments:

Post a comment