Function Overloading in C++


Example Program

Here is the simple program for printing function overloading in C++.

Source Code Example : 1

#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

Source Code Example : 2

#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


Source Code Example : 3

#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

Basic Programs


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


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes