Polymorphism in C++
Polymorphism means having multiple forms of one thing. We can achieve the polymorphism by function overriding. The function overriding occurs when both parent and child class have the member function with the same declaration.
The member function with the same declaration means to have the function with the same name, same return type, and same parameters.
Requirements of Function Overriding
- We must Inherit the classes.
- Function overriding cannot be done within the same class.
- Function overriding occurs when at least one derived class and one base class must be there.
- The function that is being redefined must have the exact same declaration/signatures in both parent and child classes. As discussed earlier that they must have the same name, same return type, and the same parameter list.
using namespace std;
cout << "Base class";
class Derived:public Base
cout << "Derived Class function is invoked.";
Derived Class function is invoked.