Overriding in Inheritance is used when you use a already defined method from a super class in a sub class, but in a different way than how the method was originally designed in the super class. A child class inherits the data members and member functions of parent class, but when you want to override a functionality in the child class then you can use function overriding.
Function Overriding. When the base class and derived class have member functions with exactly the same name, same return-type, and same arguments list, then it is said to be function overriding.
This C++ program demonstrates Function Overriding using a base class base and a derived class derived.
#include<iostream> using namespace std; //Function Overriding in c++ class base { protected: int a,b; public: void add() { cout<<"\nEnter 2 Nos : "<<endl; cin>>a>>b; cout<<"Total : "<<a+b<<endl; } }; class derived:public base { private : int c; public: void add() { cout<<"\nEnter 3 Nos : "<<endl; cin>>a>>b>>c; cout<<"Total : "<<a+b+c<<endl; } }; int main() { base b; b.add(); derived d; d.add(); return 0; }
Enter 2 Nos : 12 25 Total : 37 Enter 3 Nos : 67 4 34 Total : 105To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions