Function Overriding In C++

Share Your Love

Function overriding in C++ occurs when a derived class defines the same function as its base class. It’s implemented to accomplish polymorphism at runtime. It allows you to create a customised implementation of a function that is already available in the base class.

Function Overriding Example In C++:

Let’s look at a simple function overriding example in C++. We’re overriding the eat() function in this example.

#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...

Join Our Community

Join our WhatsApp Group To know more about Programming Language tips, tricks and knowledge about and how to start learning any programming language.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 366

Newsletter Updates

Enter your email address below to subscribe to our newsletter