Function Overloading in C++

Function Overloading in C++

5 mins read2.5K Views Comment
Updated on Aug 27, 2024 17:23 IST

Do you want to know about function overloading? This article will clear the concepts of function overloading with programming examples.

2022_05_FI23.jpg

C++ allows you to specify the same function in the same program but with different numbers and types of arguments. But will it be right to use the same function name in the program? The answer is YES because if the function name is the same, the number/ type of arguments and implementations will be different for all functions. In case we have two or more functions with the same functions name, the compiler decides which definition of that function to use. In this article, we are going to explore function overloading in C++ with example.

How does the compiler differentiate between these functions?

The compiler decides this by checking the number and type of arguments you have used while calling the function. So while writing the functions, we can vary the number of arguments or change the data type of arguments. This is how a compiler can differentiate between two or more overloaded functions. It is done at compile-time; hence, it is known as compile-time polymorphism. This process of selecting the most appropriate function is called overload resolution.

Table of contents

Recommended online courses

Best-suited C++ courses for you

Learn C++ with these high-rated online courses

– / –
4 months
4.24 K
6 weeks
– / –
15 days
– / –
– / –
7.14 K
5 weeks
name
SoloLearnCertificateStar Icon4.5
Free
– / –
– / –
4 months
– / –
3 months

What is function overloading in C++?

Same function name with different types and number of parameters.

Function Overloading is the process in which there are two or more member functions with the same name in a program but with different numbers and types of parameters. The function can have other implementations also.

An overloaded function must have:

  1. Different types of parameter
  2. Different number of parameter

Example:

void myfun();

void myfun(int a);

void myfun(float a);

void myfun(int a, int b);

void myfun(int a,double b);

Working with Strings in C++
Classes and Objects in C++

Rules of function overloading in C++

  1. Functions having different types of parameter

add(int a,int b);

add(double,double b);

2. Functions having a different number of parameter

add(int a,int b);

add(inta ,int b ,int c);

3. Functions having different sequences of parameter

add(int a, double b);

add(double a, int b)’

Advantages/disadvantages of function Overloading in C++

Advantages

  • Memory space is saved by using function overloading.
  • The readability and consistency of the program become good.
  • Function overloading allows us to get different behavior with the same function name.
  • Execution of the program becomes fast.
  • Function overloading is used for code reusability.
  • Maintaining and debugging code becomes easy.
  • It helps the application load the class method based on the parameter type.

 Disadvantages

  • If the function declaration of two functions differs by their return type, they cannot be overloaded.    
2022_05_image-55.jpg
  • If two Member function declarations are the same with the same name types or parameters and one of them is declared static, they cannot be overloaded. Else it will throw an error. Eg.
2022_05_image-59.jpg

Also Explore: OOPs concepts in C++

Function overloading types 

1. Compile-time overloading

When the compiler binds the function definition with a function call at the time program is compiled is called compile-time overloading. During this time, functions are overloaded by using different signatures. Signatures here mean the number of parameters, types of parameters, and return type. Example-function overloading.

2. Runtime overloading

When the compiler binds the function definition with the function call at the time of execution of a program is called runtime overloading.Example-function overriding.

Programming examples

Write a program to add different values.

 Function overloading: When the function name is same but number of arguments is different

#include <iostream>
 
using namespace std;
 
int add(int a, int b) //passing 2 integer parameters
{
    cout << a+b <<endl;
    return 0;
}
 
int add(int a, int b, int c) //passing 3 integer parameters
{
    cout << a+b+c <<endl;
    return 0;
}
 
int add(int a,int b,int c,int d) //passing 4 integer parameters
 
{
    cout << a+b+c+d <<endl;
    return 0;
}
 
int main()
{
 
    add(20, 40);  
 
    add(40, 20, 30);  
 
    add(50, 30, 10, 70);
}
 
Output:
60
90
160
 

In this, we have add() three times in the program. First, add function have two integer parameter. The second add function has three integer parameters. The third add function has four integer parameters. So same function with different number parameters are there. And the parameters are of integer type. You can see that in every function, the number of parameters varies.

 Function overloading: When the function name is same but type of arguments are different

#include <iostream>
 
using namespace std;
 
int add(double a, double b, double c) // double parameters
{
    cout << a+b+c <<endl;
    return 0;
}
 
int add(int a, int b, int c) // integer parameters
{
    cout << a+b+c <<endl;
    return 0;
}
int add(float a, float b, float c) // float parameters
{
    cout << a+b+c <<endl;
    return 0;
}
 
int main()
{
 
    add(20.45, 40.65, 34.67); //passing values to double parameters
 
    add(40, 20, 30);  //passing values to integer parameters
 
    add(20.8f, 30.0f, 70.5f); //passing values to float parameters
}
Output:
95.77
9
121.3
 
 

In the above example, we define an add() function three times. First, using double datatype for all parameters, secondly, using an integer datatype for all parameters, and third, using float datatype for all parameters.

Difference between Function Overloading and Function Overriding

Function Overloading Function Overriding
In function overloading, two or more functions have different arguments but the same method name. In function overriding, two functions have the function name and same parameters, but one function is in the base class and the other is in the derived class.
Function Signature is different. A signature means a type of parameters or the number of parameters  The function signature should remain the same.
For function overloading, there is a need for one class.  For function overriding, there is a need for a minimum of two classes.
Resolved at compile Time. Resolved at run Time.

Endnotes

Function overloading in C++ can be used to enhance code readability. To save compile time and memory, we can use this function overloading concept. I hope you understand the idea. If you liked this article, then please share it with your friends.

Top Trending Articles:
Data Analyst Interview Questions | Data Science Interview Questions | Machine Learning Applications | Big Data vs Machine Learning | Data Scientist vs Data Analyst | How to Become a Data Analyst | Data Science vs. Big Data vs. Data Analytics | What is Data Science | What is a Data Scientist | What is Data Analyst

 

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio