Sie sind auf Seite 1von 2

Week-7

Write a C++ program illustrating function overloading


Algorithm :
1.start
2.declare a,b,c,d,result variables.
3.read 2 integers and call function sum and display its result
4.read 3 integers and call function sum and display its result.
5.stop
If Same function used for multiple tasks ,then it is called as function overloading
PROGRAM
#include <iostream.h>
#include <conio.h>
void main()
{
int sum(int,int);
//function with 2 arguments
int sum(int,int,int);
//function with 3 arguments
int sum(int,int,int,int);
//function with 4 arguments
int sum(int a,int b,int c,int d);
clrscr();
int a,b,c,d,result;
cout<<"\n\nfor 2 argument\n";
cout<<"Enter 2 Integers\n";
cin>>a>>b;
result=sum(a,b);
cout<<"Addition ="<<result;
cout<<"\n\nfor 3 argument\n";
cout<<"Enter 3 Integers\n";
cin>>a>>b>>c;
result=sum(a,b,c);
cout<<"Addition ="<<result;
cout<<"\n\nfor 4 argument\n";
cout<<"Enter 4 Integers\n";
cin>>a>>b>>c>>d;
result=sum(a,b,c,d);
cout<<"Addition ="<<result;
getch();
}

//function with 2 argument


int sum(int a,int b)
{
return(a+b);
}
//function with 3 argument
int sum(int a,int b,int c)
{
return(a+b+c);
}
//function with 4 argument
int sum(int a,int b,int c,int d)
{
return(a+b+c+d);
}

}
INPUT & OUTPUT

for 2 arguments
Enter 2 Integers
2
3
Addition =5
for 3 arguments
Enter 3 Integers
2
3
4
Addition =9
for 4 arguments
Enter 4 Integers
2
3
4
5
Addition =14

Das könnte Ihnen auch gefallen