Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Function Overloading

In this tutorial, you will learn about Function Overloading, Types of function overloading with the help of examples.

Function Overloading

  • Overloading means the use of same thing for different purposes.
  • Function overloading is a most important feature in C++ where two or more functions can have the same name but different parameters or arguments for the different tasks.
  • Function overloading can be considered as an example of polymorphism feature in C++.
  • The function will perform different operations but on the basis of the argument list in the function call.
  • Function Overloading is usually used to enhance the readability of the program.

Syntax

These functions having the same name but different arguments are known as overloaded functions. For example:

// same name different arguments
int test() { }
int test(int a) { }
int test(int a, double b) { }
float test(double a) { }
double test(double a) { }
int test(int a, double b) { }

Here, all 6 functions are overloaded functions.

Notice that the return types of all these 6 functions are not the same.

Overloaded functions may or may not have different return types but they must have different arguments. For example,

// Error code
int test(int a) { }
double test(int b){ }

Here, both functions have the same name, the same type, and the same number of arguments. Hence, the compiler will throw an error.


Different ways to Overloading a Function

  1. By having different types of Arguments/Parameters.
  2. By changing number of Arguments/Parameters.

Function Overloading using Different Types of Arguments/Parameters

  • In this method, we define two or more functions with same name and same number of parameters but the data type of parameter is different.
  • For example, we have two add() function, first one gets two integer arguments and second one gets two double arguments.

Example 1: C++ Function Overloading using Different Types of Arguments/Parameters

// Program to compute sum of value
// Works for both int and double
                                      
#include <iostream>
using namespace std;
                                      
// first definition                               
int add(int a, int b) 
{
    cout  << "Sum of a + b (integer value) = " <<  a + b << endl;
}
                                      
// Second overloaded definition                               
double add(double a, double b) 
{
    cout  << "Sum of a + b (floating point value) = "  <<  a + b << endl;
}
                                      
int main() {
                                          
    add (1, 2);
    add (1.5, 2.5);
                                      
    return 0;
}

Output

Sum of a + b (integer value) = 3
Sum of a + b (floating point value) = 4

In this program, we overload the add() function. Based on the type of parameter passed during the function call, the corresponding function is called.


Function Overloading using Different Number of Arguments/Parameters

  • In this type of function overloading we define two functions with same names but different number of parameters of the same type.
  • For example, in the below program we have made two add() functions to return sum of two and three integers.

Example 2: C++ Function Overloading using Different Numbers of Arguments/Parameters

// Program to compute sum of value                                      
#include <iostream>
using namespace std;
                                      
// first definition                               
int add(int a, int b) 
{
    cout << "Sum of a + b (integer value) = " <<  a + b << endl;
}
                                      
// Second overloaded definition                               
int add(int a, int b, int c) 
{
    cout  << "Sum of a + b + c (floating point value) = "  << a + b + c << endl;
}
                                      
int main() {
                                          
    add (1, 2);
    add (1.5, 2.5, 3.5);
                                      
    return 0;
}

Output

Sum of a + b (integer value) = 3
Sum of a + b + c (floating point value) = 6

Here, the add() function is called two times with different arguments. Depending on the number and type of arguments passed, the corresponding add() function is called.

The return type of all these functions is the same but that need not be the case for function overloading.


Note: In C++, many standard library functions are overloaded. For example, the sqrt() function can take doublefloatint, etc. as parameters. This is possible because the sqrt() function is overloaded in C++.


Advantages of Function Overloading

  • Function overloading save the memory space, consistency and readability of our program.
  • We can develop more then one function with the same name.
  • Function overloading shows the behaviour of polymorphism that allows us to get different behaviour, although there will be some link using th esame name of the function.
  • Function overloading speeds up the execution of the program.
  • It is used for code reusability and also to save memory.
  • It helps application to load the class method based on the type of parameter.
  • Code maintenance is easy

Disadvantages of Function Overloading

  • Function declarations that differ only by its return type cannot be overloaded with function overloading process.
  • Member function declarations with the same parameters or the same name types cannot be overloaded if any one of them is declared as a static member function.
Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!