40. Function Overriding

By | September 24, 2021

If derived class defines same function as defined in its base class, it is known as function overriding in C++. It is used to achieve runtime polymorphism. It enables you to provide specific implementation of the function which is already provided by its base class.

Let’s see a simple example of Function overriding in C++. In this example, we are overriding the eat() function.

#include <iostream>  
using namespace std;  
class Animal {  
    public:  
void eat(){    
cout<<“Eating…”;    
    }      
};   
class Dog: public Animal    
{    
 public:  
 void eat()    
    {    
       cout<<“Eating bread…”;    
    }    
};  
int main(void) {  
   Dog d = Dog();    
   d.eat();  
   return 0;  
}  

Output:

Eating bread...

Leave a Reply

Your email address will not be published. Required fields are marked *