Method Overriding or Function Overriding in C++

Method Overriding or Function Overriding in C++

In this tutorial you will come to know all about method overriding in C++,  method overloading in C++, method hiding in C++, and more. 

These are also called function overriding, function overloading and function hiding in C++.

1. More than one functions in same class with same name but different parameter list is called method overloading or function overloading.

2. When base class and derived class both have same function name and same argument list, then it is called function overriding in C++. It is used for achieving run-time polymorphism.

3. When base class and derived class having the same function (or method) name but different parameter list, it is called method hiding or function hiding.


For Example:- method overloading or function overloading in c++
#include<iostream.h>
using namespace std;
class Sum
{
private:
int num1, num2, num3;

public:
Sum() //constructor having 0 argument.
{
num1 = 0;
num2 = 0;
num3 = 0;
}

Sum(int n1, int n2, int n3) //constructor having 3 arguments.
{
num1 = n1;
num2 = n2;
num3 = n3;
}

Sum(int n1, int n2) //constructor having 2 arguments.
{
num1 = n1;
num2 = n2;
}
};

int main()
{
Sum S1; //constructor with no argument called.
Sum S2(2,5); //constructor with 2 arguments called.
Sum S3(1,8,11); //constructor with 3 arguments called.
}


Here, 3 constructors are of same name but different argument list. So, it is called constructor overloading in C++ and also method or function overloading.


For Example:- method overriding or  function overriding in c++
#include<iostream.h>  
using namespace std;
class Mammal
{
public:
void eat()
{
cout<<"Eating...";
}
};

class Man: public Mammal
{
public:
void eat() //method overriding or function overriding.
{
cout<<"Eating brown rice...";
}
};

int main()
{
Man M;
M.eat(); //Output will be Eating brown rice...
return 0;
}


For Example:- method hiding or function hiding in c++
#include<iostream.h>
using namespace std;
class Number
{
private:
int num1;

public:
void getNum()
{
num1 = 0;
}
};

class BigNumber: public Number
{
private:
int num;

public:
void getNum(int n1) //method hiding or function hiding.
{
num = n1;
}
};

int main()
{
BigNumber B1;
B1.getNum(); //there is an error in this line.
B1.getNum(11); //getNum of BigNumber class will execute and 11 will assign to num variable of object B1.
}


Here, you may ask that why there is an error in line "B1.getNum();" This is because C++ compiler finds the method named getNum() in derived class and hence it did not go to base class to search for the method.

C++ compiler feels that there is an argument in the getNum method but in the line of code "B1.getNum(); " there is no argument and hence it shows an error.


Points to Remember:-

1. C++ compiler will not go to search for the method (or function) in base class if it find the method in derived class.

2. For example, in the above code, C++ compiler found the method named getNum() in derived class and did not go to search it in the base class.


When to use Method Overriding or Function Overriding?

1. If you want to change or modify any method or function of parent class then you should override function in the child class.

2. You should also know about virtual function and late binding for better understanding of function overriding.


<< Previous                                                                                      Next >>
Previous
Next Post »