Overload a function add(result, p1, p2) that can add two integers (1 + 2 -> 3),
ID: 3688546 • Letter: O
Question
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 = overloadingfunction > Please input one argument (string) to be added with default parameters: > modified > modified + 11 + 22 = modified1122
Explanation / Answer
// call by reference example
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.