C++ Function Overriding

C++ Function Overriding: It defines the same function as defined in its base class. This is known as “function overriding“. This was used for achieving run time polymorphism. It enables us to provide specific implementations of function which is already provided by the base class.

C++ Function Overriding

Function overriding is also known as “Polymorphism“. If you want to do function overriding then both the parent and child class should have the same name. And the function declared with static cannot be overridden. And also functions must have the same argument list and return type. In case if a function can’t be inherited it cannot be overridden.

Example:

#include <iostream>
using namespace std;
class Animals
{
   public:
    void sound()
     {
  cout<<"This is parent class" << endl;
     }
 };
class Dogs: public Animals
{
  public:
    void sound()
     {
        cout<<"Dogs bark" << endl;
    }
 };
int main()
{
   Dogs d;
   d.sound();
   return 0;
}

Output:



Dogs bark