Answer to Question #289977 in C++ for ali

Question #289977

Create a function, plus(), which adds two values and returns their sum. Provide overloaded versions to work with int, double and string types, and test that they work with the following function calls: int n = plus(3, 4); double d = plus (3.2, 4.5); string s = plus (“Hi”, “There”);


1
Expert's answer
2022-01-24T08:03:45-0500
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;


int plus(int i, int j) {
    return i+j;
}

double plus(double x, double y) {
    return x + y;
}

string plus(string s1, string s2) {
    return s1 + s2;
}

int main() {
    int n = plus(3, 4);
    cout << "plus(3, 4) = " << n << endl;

    double d = plus(3.2, 4.5);
    cout << "plus(3.2, 4.5) = " << d << endl;

    string s = plus("Hi", "There");
    cout << "plus(\"Hi\", \"There\") = " << s << endl;

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog