Function Overloading in C++



Function overloading in C++ allows you to define multiple functions with the same name but different parameters. Function overloading is used to achieve polymorphism which is an important concept of object-oriented programming systems.

Syntax for Overloaded Functions

Consider the following two function declarations having the same name but different parameters −

 return_type function_name(parameter1); return_type function_name(parameter2); 

Example of Function Overloading

In the following example, we are defining three different functions with the same name but different parameters. This example demonstrates the implementation of function overloading −

 #include<iostream> using namespace std; // Adding two integers (Function definition 1) int addition(int a, int b) { return a + b; } // Adding three integers (Function definition 2) int addition(int a, int b, int c) { return a + b + c; } // Adding two floating-point numbers (Function definition 3) float addition(float a, float b) { return a + b; } int main() { cout<<addition(10.5f, 20.3f)<<endl; cout<<addition(10, 20, 30)<<endl; cout<<addition(10, 20)<<endl; return 0; } 

Output

 30.8 60 30 

How Function Overloading Works?

In the case of different functions with the same name (function overloading), when the compiler reaches a specific function call, it checks with the different function definition based on the parameters type, order, or number of arguments, and executes the matched function definition.

Example

Let suppose, there are 3 different function definitions to add numbers with different parameters −

 // Adding two integers (Function definition 1) int addition(int a, int b) { return a + b; } // Adding three integers (Function definition 2) int addition(int a, int b, int c) { return a + b + c; } // Adding two floating-point numbers (Function definition 3) float addition(float a, float b) { return a + b; } 

And, you call the function in the following order −

 addition(10.5f, 20.3f); // Function call 1 addition(10, 20, 30); // Function call 2 addition(10, 20); // Function call 3 

In the above function calls, function definition will be called in the following order −

  • Function call 1 will execute Function definition 3, because here we are passing two float values.
  • Function call 2 will execute Function definition 2, because here we are passing three integer values.
  • Function call 3 will execute Function definition 1, because here we are passing two integer values.

Function Overloading Based on Number of Parameters

This method involves defining multiple functions with the same name but a different number of parameters.

Syntax

 void display(int a); // takes one parameter void display(int a, double b); // takes two parameters 

Example

The following example demonstrates the function overloading based on the number of parameters −

 #include <iostream> using namespace std; // Function overloads based on the number of parameters void display(int a) { cout << "Display with one integer: " << a << endl; } void display(int a, double b) { cout << "Display with an integer and a double: " << a << " and " << b << endl; } int main() { // Calls the first overload display(10); // Calls the second overload display(10, 3.14); double: 10 and 3.14 return 0; } 

Output

 Display with one integer: 10 Display with an integer and a double: 10 and 3.14 

Function Overloading Based on Different Parameter Types

This method involves defining multiple functions with the same name but different types of parameters.

Syntax

 void show(int a); // parameter with int type void show(double a); // parameter with double type 

Example

The following example demonstrates the function overloading based on the different parameter types −

 #include <iostream> using namespace std; // Function for integer input void show(int a) { cout << "Integer value: " << a << std::endl; } // Function for double input void show(double a) { cout << "Double value: " << a << std::endl; } int main() { show(10); show(3.14); return 0; } 

Output

 Integer value: 10 Double value: 3.14 

Function Overloading Based on Different Parameter Order

This method involves defining multiple functions with the same name but different sequences of parameters.

Syntax

 // integer followed by a double, // can be any datatype(bool, char etc) void display(int a, double b) { cout << "Integer and Double: " << a << ", " << b << endl; } // double followed by an integer void display(double a, int b) { cout << "Double and Integer: " << a << ", " << b << endl; } 

Example

The following example demonstrates the function overloading based on the different parameter order −

 #include <iostream> using namespace std; void display(int a, double b) { cout << "Integer and Double: " << a << ", " << b << endl; } void display(double a, int b) { cout << "Double and Integer: " << a << ", " << b <<endl; } int main() { display(10, 3.14); display(3.14, 10); return 0; } 

Output

 Integer and Double: 10, 3.14 Double and Integer: 3.14, 10 

Use Cases for Function Overloading

Function overloading in C++ is a powerful feature that allows you to use the same function name to perform different tasks based on different parameter lists. This can lead to more readable and maintainable code. Here are some common scenarios and examples where function overloading is useful −

  • Different Data Types − Function overloading is useful for handling various data types with a common function name.
  • Different Number of Parameters − Function overloading provides flexibility with functions which have varying numbers of parameters.
  • Parameter Type and Order − Function overloading handles different parameter types or their order.
  • Different Operations − Function overloading supports similar operations for different data types or contexts.
  • Variant Contexts − Function overloading provides variations of a function to suit different requirements or levels of detail.
Advertisements
close