Function Overloading In C++ Programming


A Function is a collection of statements that are grouped together to perform an operation. A function in C++ is a block of code that, When the function is invoked from any part of the program, it all executes the codes defined in the body of the function. Function overloading is the ability to create multiple function with the same name, granted that they differ in either number or type of arguments. Compiler checks function signature for function overloading.

Function signature consists of three things :

  • Function name
  • Number of parameters
  • Types of parameters

The program demonstrates function overloading in C++, where multiple functions have the same name but different parameter lists.

  • The program defines three functions named sum() that calculate the sum of two or three integer or floating-point values:
    • The first sum() function takes two integer parameters a and b and returns their sum.
    • The second sum() function takes three integer parameters a, b, and c and returns their sum.
    • The third sum() function takes two floating-point parameters a and b and returns their sum.
  • In the main() function, each of these sum() functions is called with different parameter lists, and their return values are printed to the console using cout.
  • The first sum() function is called with parameters 10 and 20, so it returns 30 and the string "Total : " is printed to the console before the value 30.
  • The second sum() function is called with parameters 10, 20, and 30, so it returns 60 and the string "Total : " is printed to the console before the value 60.
  • The third sum() function is called with floating-point parameters 10.25f and 25.10f, so it returns 35.35f and the string "Total : " is printed to the console before the value 35.35f.

Source Code

#include<iostream>
using namespace std;
//  Function Overloading
int sum(int a,int b)
{
    return a+b;
}
int sum(int a,int b,int c)
{
    return a+b+c;
}
float sum(float a,float b)
{
    return a+b;
}
int main()
{
    cout<<"Total : "<<sum(10,20)<<endl;
    cout<<"Total : "<<sum(10,20,30)<<endl;
    cout<<"Total : "<<sum(10.25f,25.10f)<<endl;
    return 0;
}
 

Output

Total : 30
Total : 60
Total : 35.35
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples