What is overriding in C++?
- Redefining a base class function in the derived class to have our own implementation is referred as overriding.
- Often confused with overloading which refers to using the same function name but with a different signature. However in overriding the function signature is the same.
Demonstrate the usage of overriding
#include <iostream> using namespace std; class Base { public: virtual void myfunc() { cout << "Base::myfunc .." << endl; } }; class Derived : public Base { public: void myfunc() { cout << "Derived::myfunc .." << endl; } }; void main() { Derived d; d.myfunc(); }OUTPUT:-
Derived::myfunc ..
0 comments:
Post a Comment