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 −

Open Compiler

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

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)intaddition(int a,int b){return a + b;}// Adding three integers (Function definition 2)intaddition(int a,int b,int c){return a + b + c;}// Adding two floating-point numbers (Function definition 3)floataddition(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

voiddisplay(int a);// takes one parametervoiddisplay(int a,double b);// takes two parameters

Example

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

#include <iostream>usingnamespace std;// Function overloads based on the number of parametersvoiddisplay(int a){
   cout <<"Display with one integer: "<< a << endl;}voiddisplay(int a,double b){
   cout <<"Display with an integer and a double: "<< a <<" and "<< b << endl;}intmain(){// Calls the first overloaddisplay(10);// Calls the second overloaddisplay(10,3.14);double:10and3.14return0;}

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

voidshow(int a);// parameter with int type voidshow(double a);// parameter with double type

Example

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

Open Compiler

#include <iostream>usingnamespace std;// Function for integer inputvoidshow(int a){
   cout <<"Integer value: "<< a << std::endl;}// Function for double inputvoidshow(double a){
   cout <<"Double value: "<< a << std::endl;}intmain(){show(10);show(3.14);return0;}

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)voiddisplay(int a,double b){ 
   cout <<"Integer and Double: "<< a <<", "<< b << endl;}// double followed by an integervoiddisplay(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 −

Open Compiler

#include <iostream>usingnamespace std;voiddisplay(int a,double b){
   cout <<"Integer and Double: "<< a <<", "<< b << endl;}voiddisplay(double a,int b){
   cout <<"Double and Integer: "<< a <<", "<< b <<endl;}intmain(){display(10,3.14);display(3.14,10);return0;}

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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *