The Wayback Machine - https://web.archive.org/web/20240624041333/https://www.geeksforgeeks.org/function-overloading-c/
Open In App

Function Overloading in C++

Last Updated : 16 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Function overloading is a feature of object-oriented programming 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. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading can be considered as an example of a polymorphism feature in C++.

If multiple functions having same name but parameters of the functions should be different is known as Function Overloading.
If we have to perform only one operation and having same name of the functions increases the readability of the program.
Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the function such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you to understand the behavior of the function because its name differs.
 

The parameters should follow any one or more than one of the following conditions for Function overloading:

  • Parameters should have a different type

add(int a, int b)
add(double a, double b)

Below is the implementation of the above discussion:

C++




#include <iostream>
using namespace std;
 
 
void add(int a, int b)
{
  cout << "sum = " << (a + b);
}
 
void add(double a, double b)
{
    cout << endl << "sum = " << (a + b);
}
 
// Driver code
int main()
{
    add(10, 2);
    add(5.3, 6.2);
 
    return 0;
}


Output

sum = 12
sum = 11.5
  • Parameters should have a different number 

add(int a, int b)
add(int a, int b, int c)

Below is the implementation of the above discussion:

C++




#include <iostream>
using namespace std;
 
void add(int a, int b)
{
  cout << "sum = " << (a + b);
}
 
void add(int a, int b, int c)
{
    cout << endl << "sum = " << (a + b + c);
}
 
// Driver code
int main()
{
    add(10, 2);
    add(5, 6, 4);
 
    return 0;
}


Output

sum = 12
sum = 15
  • Parameters should have a different sequence of parameters.

add(int a, double b)
add(double a, int b)

Below is the implementation of the above discussion:

C++




#include<iostream>
using namespace std;
 
void add(int a, double b)
{
    cout<<"sum = "<<(a+b);
} 
 
void  add(double a, int b)
{
    cout<<endl<<"sum = "<<(a+b);
} 
 
// Driver code
int main()
{
    add(10,2.5);
    add(5.5,6);
 
      return 0;
}


Output

sum = 12.5
sum = 11.5

Following is a simple C++ example to demonstrate function overloading.

CPP




#include <iostream>
using namespace std;
 
void print(int i) {
  cout << " Here is int " << i << endl;
}
void print(double  f) {
  cout << " Here is float " << f << endl;
}
void print(char const *c) {
  cout << " Here is char* " << c << endl;
}
 
int main() {
  print(10);
  print(10.10);
  print("ten");
  return 0;
}


Output

 Here is int 10
 Here is float 10.1
 Here is char* ten

C++




#include<iostream>
using namespace std;
 
void add(int a, int b)
{
    cout<<"sum ="<<(a+b);
} 
 
void  add(int a, int b,int c)
{
    cout<<endl<<"sum ="<<(a+b+c);
} 
 
 
main()
{
 
    add(10,2);
    add(5,6,4);
    return 0;
}


C++




#include<iostream>
using namespace std;
 
void add(int a, double b)
{
    cout<<"sum ="<<(a+b);
} 
void  add(double a, int b)
{
    cout<<endl<<"sum ="<<(a+b);
} 
 
main()
{
    add(10,2.5);
    add(5.5,6);
    return 0;
           }


How does Function Overloading work?

  • Exact match:- (Function name and Parameter)
  • If a not exact match is found:–

               ->Char, Unsigned char, and short are promoted to an int.

               ->Float is promoted to double

  • If no match is found:

               ->C++ tries to find a match through the standard conversion.

  • ELSE ERROR 🙁
  1. Function overloading and return type
  2. Functions that cannot be overloaded in C++
  3. Function overloading and const keyword
  4. Function Overloading vs Function Overriding in C++

Recent articles on function overloading in C++

 



Previous Article
Next Article

Similar Reads

error: call of overloaded ‘function(x)’ is ambiguous | Ambiguity in Function overloading in C++
Pre-requisite: Function Overloading in C++ Function overloading is a feature of object-oriented programming 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. Two or more functions are said to be overloaded if they differ in any of th
9 min read
Function Overloading vs Function Overriding in C++
Function Overloading (achieved at compile time) Function Overloading provides multiple definitions of the function by changing signature i.e. changing number of parameters, change datatype of parameters, return type doesn’t play any role. It can be done in base as well as derived class.Example: void area(int a); void area(int a, int b); C/C++ Code
3 min read
Overloading function templates in C++
Template: A template is a tool that reduces the efforts in writing the same code as templates can be used at those places.A template function can be overloaded either by a non-template function or using an ordinary function template. Function Overloading: In function overloading, the function may have the same definition, but with different argumen
3 min read
Advantages and Disadvantages of Function Overloading in C++
Function overloading is one of the important features of object-oriented programming. It allows users to have more than one function having the same name but different properties. Overloaded functions enable users to supply different semantics for a function, depending on the signature of functions. Advantages of function overloading are as follows
3 min read
Overloading of function-call operator in C++
In this article, we will discuss the Overloading of the function-call operators in C++. The function call operator is denoted by “()” which is used to call function and pass parameters. It is overloaded by the instance of the class known as a function object.When the function call operator is overloaded, an operator function is created that can be
3 min read
Menu-Based program to find the volume of 3D shapes using function overloading
Given the dimensions of the 3D Shapes like Cube, Cuboid, or Cylinder, the task is to find the volume of all the 3D Shapes using function overloading. Examples: Input: Cube: L = 3, Cuboid: L = 3, B = 4, H = 3, Cylinder: R = 2, H = 7Output:Volume of Cube: 27Volume of Cuboid: 36Volume of Cylinder: 176 Input: Cube: L = 2, Cuboid: L = 3, B = 2, H = 3, C
4 min read
Function overloading and const keyword
Function overloading is a feature of object-oriented programming 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. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading
5 min read
Function Overloading and float in C++
Although polymorphism is a widely useful phenomena in C++ yet it can be quite complicated at times. For instance consider the following code snippet: C/C++ Code #include&amp;lt;iostream&amp;gt; using namespace std; void test(float s,float t) { cout &amp;lt;&amp;lt; &amp;quot;Function with float called &amp;quot;; } void test(int s, int t) { cout
2 min read
How to Use Default Arguments in Function Overloading in C++?
In C++, we can provide the default values for the input arguments into the functions and it is also supported in function overloading. In this article, we will learn how to use default arguments in function overloading in C++. Default Arguments in Function Overloading in C++We can define the default value in function overloading as we do it in the
2 min read
Function Overloading and Return Type in C++
Function overloading is possible in C++ and Java but only if the functions must differ from each other by the types and the number of arguments in the argument list. However, functions can not be overloaded if they differ only in the return type. Why is Function overloading not possible with different return types and identical parameters? Function
2 min read
three90RightbarBannerImg