0% found this document useful (0 votes)
18 views

Write A C++ Program Illustrating Function Overloading: Week-7 Algorithm

The document describes a C++ program that illustrates function overloading. It defines a sum function that can take 2, 3, or 4 integer arguments. The main function calls each version of the sum function, prompting the user to enter the appropriate number of integers and displaying the result. Function overloading allows the same function name to be used for multiple tasks as long as the number or type of arguments is different.

Uploaded by

Lavanya_123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Write A C++ Program Illustrating Function Overloading: Week-7 Algorithm

The document describes a C++ program that illustrates function overloading. It defines a sum function that can take 2, 3, or 4 integer arguments. The main function calls each version of the sum function, prompting the user to enter the appropriate number of integers and displaying the result. Function overloading allows the same function name to be used for multiple tasks as long as the number or type of arguments is different.

Uploaded by

Lavanya_123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like