C++ Function Overriding

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.

Example

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

snippet
#include 
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...
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +