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”);
#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;
}
Comments
Leave a comment