Function Overloading In C++ | Function Overloading Explained With Real Life Examples | Function Overloading In OOPs

 Function Overloading In C++ 



What is function overloading?

Function overloading means creating duplicates of the same function but it will be different from that function. WHAT ??. I know you don't understand. So let me explain this concept by a real-life example. So think about your mother, your mother is a woman but she plays many roles like sometimes she is your mother, sometimes she plays the role of wife, sometimes she plays the role of daughter, etc. So your mom is overloading to the different roles she overloads different roles every time.

So same function overloading means to overload properties of different functions.





And here is the official definition of function overloading - 

Function overloading is a feature in C++ where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading.


Here in C++ Function overloading mean you overload some other function with the same name but the difference is in the number of parameters and its typing.


Here how you can overload a function in C++ - 


#include <iostream>
using namespace std;


int sum(int a,int b) //Function with 2 int type parameter
    {
      return a+b;      
    }

//overloaded function
int sum(int a, int b, int c) //Same function with 3 int type parameter
{
    return a + b +c;
}

//overloaded function
float sum(float a) //same function with 1 float type parameter
{
    return a;
}

//etc.
int main()
{
    cout<<sum(1,5)<<endl;
    cout<<sum(112,235,23)<<endl;
    cout<<sum(12.44)<<endl;
    return 0;
}



Output - 

6

370

12.44

Comments

Popular posts from this blog

Constructor In C++ | Constructor Explained With Real Life Example

Destructor In C++ | Destructor Explained With Real Life Examples

Array Of Objects In C++ | Array Of Objects Explained With Real Life Example