Function Overloading in C++

What is Function Overloading in C++?

Namaste, My name is Nitin Kumar and I am here to write a blog on the topic of function overloading in C++. We will discuss all the important factors of the function overloading in c++. We will discuss all about function overloading why it is used where it is used when it is used etc.

So lets began our one of the most important topic of the C++ function overloading.

Function Overloading in C++

 

function overloading in C++

function overloading in C++

 

As you can see the image there are three boxes the first box contain 1(one), the second one contains 2(two) and the third one contains 3(three). Note that there are three boxes the name of all the box is the same. The Function overloading is work on same this process. The functions with the same name in a program are known as Function overloading in C++ must be the same name as the name of all the box is same.

Overloading means to assign multiple meanings to a single name or operator or any symbol.

 

In simple language, we can say that the function overloading in c++ is nothing else of the same name of the functions in the program with the different argument list.

 

C++ allows you to specify more than one definition for a function name in the same scope, which is known as function overloading in c++ respectively.

What is Function overloading in C++?

In Function overloading, the name of all the functions must be the same. But the parameter list must be different. (number of the parameter list, number of the parameter list, types of parameter list and order of parameter list must be different).

 

Now the questions come in your mind that how does compiler knows that you have called that specific function which you wanted to call. It is most important to know that when you call an overloaded function, the compiler determines the most appropriate definition to use, by comparing the argument types(parameter list) which you have used to call the function with the parameter types specified in the definitions.

 

Overload Resolution

The process to select the function which you have called it is known as Overload resolution.

Function Overloading in C++

You can have the same definition for the same function in the same scope. The definition part of the function must be different.

The syntax for Declaring Function Overloading in C++

 

return_type function_name()

{

body part;

}

return_type function_name(data_type1 variable 1)

{

body part;

}

return_type function_name(data_type1 variable1, data_type2 variable2)

{

body part;

}

 

Example of Function Overloading in C++

Write a program to enter a value in integer, float and in character and print that value using Function overloading.

                                          INPUT

#include <iostream>
#include<conio.h>
int main()
{
print(300);
print(3.10);
print("three");
return 0;
}
void print(int i)
{
cout << " Here is int " << i;
}
void print(double  f)
{
cout << " Here is float " << f;
}
void print(char* c)
{
cout << " Here is char* " << c;
}

    Output 

Here is int 300 
Here is float 3.10
Here is char* three
Write a program to compute the absolute value.

                                     INPUT

#include <iostream.h>
#include<conio.h>int main()
{
int a = -5;
float b = 5.5;cout << “Absolute value =”<<absolute(a);
cout << “Absolute value =”<<absolute(b);
return 0;
}int absolute(int var);
int absolute(int var)
{
if (var < 0)
var = -var;
return var;
}float absolute(float var);
float absolute(float var)
{
if (var < 0.0)
var = -var;
return var;}
        OUTPUT
Absolute value = 5
Absolute value = 5.5

Write a program to find an area of rectangle, circle, and triangle by using function overloading in C++.

                                        INPUT

#include<iostream>
using namespace std;
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
{
int s,l,b;
float r,bs,ht;
cout<<“Enter side of a square:”;
cin>>s;
cout<<“Enter length and breadth of a rectangle:”;
cin>>l>>b;
cout<<“Enter the radius of the circle:”;
cin>>r;
cout<<“Enter the base and height of triangle:”;
cin>>bs>>ht;
cout<<“Area of square is”<<area(s);
cout<<“\nArea of rectangle is “<<area(l,b);
cout<<“\nArea of circle is “<<area(r);
cout<<“\nArea of triangle is “<<area(bs,ht);
}
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
}
float area(float bs,float ht)
{
return((bs*ht)/2);
}
             OUTPUT

 

Sample Input
Enter side of a square:2
Enter length and breadth of a rectangle:3 6
Enter the radius of the circle:3
Enter the base and height of triangle:4 4
Sample Output
Area of square is4
Area of a rectangle is 18
Area of a circle is 28.26
Area of a triangle is 8

 

Thanks to visiting our web motabro.com. Hope you have got your all the answers regarding Function overloading in c++. I have tried to explain to you by using images and examples. I have given three examples to show function overloading in c++. Whereas much possible to me to explain I have tried to give best to you.

If you liked then please don’t forget to comment your questions., and if not then please don’t forget to suggest to me to get improve in this article so that I will give more best then this.

Speak Your Mind

*