Advertisement Area

Program #16: To Demonstrate Function Overloading over Different Number of Arguments

C++ Language

#include<iostream.h>
#include<conio.h>
void add(int,int);
void add(int,int,int);
void add(int,int,int,int);
void main()
{
    int a,b,c,d;
    clrscr();
    cout<<"Enter Four Integer Values - ";
    cin>>a>>b>>c>>d;
    add(a,b);
    add(a,b,c);
    add(a,b,c,d);
    getch();
}
void add(int a,int b)
{
    int sum;
    sum=a+b;
    cout<<"\nThe Addition of first two integer values - "<<sum;
}
void add(int a, int b, int c)
{
    int sum;
    sum=a+b+c;
    cout<<"\nThe addition of first three integer values - "<<sum;
}
void add(int a, int b, int c, int d)
{
    int sum;
    sum=a+b+c+d;
    cout<<"\nThe Addition of all the four integer values - "<<sum;
}

/*OUTPUT*/
Enter Four Integer Values - 
10
20
30
40
The Addition of first two integer values - 30
The addition of first three integer values - 60
The Addition of all the four integer values - 100