Question
C++ Please implement the class Pair in pair.h, which includes the private member first and second. Implement the sum_diff member function that returns a pair
C++
Please implement the class Pair in pair.h, which includes the private member first and second.
Implement the sum_diff member function that returns a pair consisting of the sum of the first and second element of the pair, and the difference of those elements. For example, if the input is 3 and 4, then the output should be 7 and -1. If the input is 4 and 3, then the output should be 7 and 1
The elements could be int, double and string. (So you need to use template class). If the first and second elements are string variables. Then define the sum of them to be the concatenation of them, and define the difference of them to be an empty string variable. For example, if the input is "ab" and "cd", then the output is "abcd" and "".
hint: You need to handle this special case with the template specialization.
Please don't add the following statement in the header file
using namespace std;
Remark: without this statement, you may need to modify your code. For example, you need to change cout to std::cout, and change string to std::string.
In the cpp file, please include the header file "pair.h" and the main function. The first part in main() function is given as follows. You need to identify the extra member functions in the class Pair and their function signatures based on this snippet.
Pairpair1(3, 4); Pair result1 = pair1.sum_diff(); cout << result1.get_first() << " " << result1.get_second() << endl; cout << "Expected: 7 -1" << endl; Pair pair2(3.5, 4.1); Pair result2 = pair2.sum_diff(); cout << result2.get_first() << " " << result2.get_second() << endl; Pair pair3("x", "y"); Pair result3 = pair3.sum_diff(); cout << result3.get_first() << " " << result3.get_second() << endl;
Run your code and save the screenshots of the console.
Please also upload screenshots of the "pair.h" and cpp file, thank you!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started