Function Overloading in C++


Example Program : 1

This is a C++ program that demonstrates function overloading using a class named data. The data class contains four different versions of a function called add(), each with a different number of parameters. In the add() function with no parameters, two integer variables a and b are initialized to 9 and 12 respectively, and their sum is stored in a variable c. The value of c is then printed to the console using cout.

In the other versions of the add() function, the values of a and b are passed as parameters to the function. The second and third versions of the add() function have two and three parameters respectively, and they perform the same calculation as the first version but with the values of a and b passed as arguments. The add() function with three parameters does not use the b parameter but instead calculates the sum of a and b and stores it in the c parameter.

In the main() function, an object of the data class is created, and each version of the add() function is called with different parameters. The program prints the sum of the parameters passed to each function to the console using cout. Function overloading allows you to use the same function name for multiple functions with different parameter types or numbers. This helps to make your code more flexible and easier to read.

Source Code

#include<iostream>
using namespace std;
class data
{
    public:
        void add()
        {
            int a=9,b=12,c;
            c=a+b;
            cout<<"\nTotal :"<<c;
        }
        void add(int a)
        {
            int b=12,c;
            c=a+b;
            cout<<"\nTotal :"<<c;
        }
        void add(int a,int b)
        {
            int c;
            c=a+b;
            cout<<"\nTotal :"<<c;
        }
        void add(int a,int b,int c)
        {
            c=a+b;
            cout<<"\nTotal :"<<c;
        }
};
int main()
{
    data o;
    o.add();
    o.add(12);
    o.add(3,5);
    o.add(2,5,0);
    return 0;
}
To download raw file Click Here

Output

Total :21
Total :24
Total :8
Total :7

Example Program : 2

This is a C++ program that demonstrates function overloading using two functions named absolute(). The absolute() function takes an argument and returns its absolute value. The first version of the absolute() function is designed to work with floating-point numbers. It takes a float parameter var, checks if var is less than 0.0, and if so, makes it positive by multiplying it by -1.0. It then returns the absolute value of var.

The second version of the absolute() function is designed to work with integers. It takes an integer parameter var, checks if var is less than 0, and if so, makes it positive by multiplying it by -1. It then returns the absolute value of var.

In the main() function, the two versions of the absolute() function are called with different parameter types. The first version of the absolute() function is called with a floating-point value of -3.3f, and the second version of the absolute() function is called with an integer value of -3. The program prints the absolute value of each parameter to the console using cout.

Function overloading allows you to use the same function name for multiple functions with different parameter types or numbers. This helps to make your code more flexible and easier to read.

Source Code

#include <iostream>
using namespace std;
float absolute(float var)
{
    if (var<0.0)
        var = -var;
    return var;
}
int absolute(int var)
{
     if (var<0)
         var = -var;
    return var;
}
int main()
{
    cout<<"Absolute value of -3 = "<<absolute(-3)<<endl;
    cout<<"Absolute value of 3.3 = "<<absolute(3.3f)<<endl;
    return 0;
}
To download raw file Click Here

Output

Absolute value of -3 = 3
Absolute value of 3.3 = 3.3


Example Program : 3

This is a C++ program that demonstrates function overloading using two functions named absolute(). The absolute() function takes an argument and returns its absolute value. The first version of the absolute() function is designed to work with floating-point numbers. It takes a float parameter var, checks if var is less than 0.0, and if so, makes it positive by multiplying it by -1.0. It then returns the absolute value of var.

The second version of the absolute() function is designed to work with integers. It takes an integer parameter var, checks if var is less than 0, and if so, makes it positive by multiplying it by -1. It then returns the absolute value of var.

In the main() function, the two versions of the absolute() function are called with different parameter types. The first version of the absolute() function is called with a floating-point value of -3.3f, and the second version of the absolute() function is called with an integer value of -3. The program prints the absolute value of each parameter to the console using cout.

Function overloading allows you to use the same function name for multiple functions with different parameter types or numbers. This helps to make your code more flexible and easier to read.

Source Code

#include <iostream>
using namespace std;
float absolute(float var)
{
    if (var<0.0)
        var = -var;
    return var;
}
int absolute(int var)
{
     if (var<0)
         var = -var;
    return var;
}
int main()
{
    cout<<"Absolute value of -3 = "<<absolute(-3)<<endl;
    cout<<"Absolute value of 3.3 = "<<absolute(3.3f)<<endl;
    return 0;
}
To download raw file Click Here

Output

Absolute value of -3 = 3
Absolute value of 3.3 = 3.3

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