Function Overriding in C++

Function Overriding in C++

Function overriding in C++ is defined as the same function is defined in the derived class and the base class. The function of the derived class is executed only if call this function using the object of the derived class.

 

Program of function overriding in C++

#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

 

Read more, Inheritance access control in C++

Share this post

Leave a Reply

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