Overload the sum functions by using a different set of parameters for the four s
ID: 674272 • Letter: O
Question
Overload the sum functions by using a different set of parameters for the four sum functions prototypes below... (notice we do not use reference parameters...only value parameters):
int sum(int, int);
int sum(int,int,int);
int sum(int,int,int,int);
int sum(int,int,int,int,int);
Each of the four sum functions should return the sum of all of it's int parameters.
You can write your code so that each of the last 3 sum functions calls the one listed before it. For example, inside the function with 4 parameters will be the call to the function with 3 parameters and likewise for the remaining two functions.
Explanation / Answer
#include<iostream>
using namespace std;
int sum(int x, int y)
{
int add;
add=x+y;
return add;
}
int sum(int x,int y,int z)
{
int add;
add=x+y+z;
return add;
}
int sum(int x,int y,int z,int a)
{
int add;
add=x+y+z+a;
return add;
}
int sum(int x,int y,int z,int a,int b)
{
int add;
add=x+y+z+a+b;
return add;
}
int main()
{
int add;
add=sum(2,3);
cout<<" output of first function is: "<<add;
add=sum(2,3,4);
cout<<" output of second function is: "<<add;
add=sum(2,3,4,5);
cout<<" output of third function is: "<<add;
add=sum(2,3,4,5,6);
cout<<" output of fourth function is: "<<add;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.