0
0
C++programming~10 mins

Function overloading in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a function named add that takes two integers and returns their sum.

C++
int add(int a, int b) {
    return a [1] b;
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' will subtract instead of add.
Using '*' or '/' will multiply or divide, which is not correct here.
2fill in blank
medium

Complete the code to overload the add function to accept two doubles and return their sum.

C++
double add(double a, double b) {
    return a [1] b;
}
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' will subtract instead of add.
Using '*' or '/' will multiply or divide, which is not correct here.
3fill in blank
hard

Fix the error in the overloaded add function that takes three integers and returns their sum.

C++
int add(int a, int b, int c) {
    return a [1] b [2] c;
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using different operators between variables causes errors or wrong results.
Using only one operator and missing the other will cause syntax errors.
4fill in blank
hard

Complete the code to overload the add function that takes two strings and returns their concatenation.

C++
std::string add(std::string a, std::string b) {
    return a [1] b;
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using operators other than '+' will cause compilation errors for strings.
Confusing with output operators like <<.
5fill in blank
hard

Fill all three blanks to overload the add function that takes one integer and one double, returning their sum as a double.

C++
double add([1] a, [2] b) {
    return a [3] b;
}
Drag options to blanks, or click blank then click option'
Aint
Bdouble
C+
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing parameter types incorrectly.
Using wrong operator instead of '+'.