Overloading Functions ( 20 points) Overload a function add(result, p1, p2) that
ID: 3690938 • Letter: O
Question
Overloading Functions ( 20 points) Overload a function add(result, p1, p2) that can add two integers (1 + 2 -> 3), chars (‘a’ + ‘b’ -> “ab”), and add(result, p0, p1, p2) to add three strings (“hello” + “c++” + “world” -> “helloc++world”). The first argument result is a call-by-reference argument and should contain the result of the summation. The type of its value should be int, string and string for the three functions. Arguments p1 and p2 should have default parameters. P0 should not have default parameters. When only one argument is provided (for the first two functions) or two arguments are provided (for the third function), the add function should use 1 (‘1’, “11”) as p1 and 2 (‘2’, “22”) as p2, as default parameters, and should display the sum. Constraints: All functions must have return type void. Sum of two integers is an int. Sum of two characters is a string. Sum of two strings is a string. You can assume that there is no space in string argument. Provide prototypes of all three functions in the beginning of your file. Main function should call each function twice: once with user input arguments, a second time with least necessary arguments (such that default arguments will be used). Example output (shaded texts are input by user): > Please input two arguments (int, int) to add: > 123 987 > 123 + 987 = 1110 > Adding default arguments (int, int): > 1 + 2 = 3 > Please input two arguments (char, char) to add: > a b > a + b = ab > Adding default arguments (char, char): > 1 + 2 = 12 > Please input three arguments (string, string, string) to add: > over loading function > over + loading + function = overloading function > Please input one argument (string) to be added with default parameters: > modified > modified + 11 + 22 = modified1122
Explanation / Answer
#include<iostream>
{
void add(int & result, int p1,int p2)
{
int temp;
temp = result;
temp = p1+p2;
cout<<”Addition of numbers is”<<temp;
}
void add(String & result, char p1, char p2)
{
String temp;
temp= result;
temp = strcat(p1,p2);
cout<<”Addition of two chars is “<<temp;
}
void add(String &result, String p0, String p1, String p2)
{
String temp;
temp= result;
temp= p0+p1+p2;
cout<<”Sum of Strings is “<< temp;
}
void main()
{
int i; # To take input from user which type to addition is required
void add(int &result, int p1, int p2); # Declaring addition method for integers
void add(String &result, char p1,char p2); # Declaring addition method for characters
void add(String &result, String p0, String p1, String p2); # Declaring addition method for Strings
cout<<”Enter 1 to add integers, 2 to add chars and 3 to enter Strings”;
cin>>i;
switch(i)
{
Case 1: int x,y, result;
Cout<<”Please input two integers to be added”
Cin>>x>>y;
add(result,x,y);
cout<<endl;
cout<<”Passing default values”
add(result,1,2);
break;
case 2: char x, y, result;
cout<<”Please input two characters to be added”
cin>>x>>y;
add(result,x,y);
cout<<endl;
cout<<”Passing default values”
add(result,1,2);
break;
Case 3: String x, y, result;
Cout<<”Please input two Strings to be added”
Cin>>x>>y;
add(result,x,y);
cout<<endl;
cout<<”Passing default values”
add(result,modified,1,2);
break;
default :
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.