Question
15.7 CIST2362 Lab: Pairs (class templates) Complete template class Pair by defining the following methods: void Input() Read two values from input and initialize the
15.7 CIST2362 Lab: Pairs (class templates)
Complete template
void Input()
Read two values from input and initialize the data members with the values in the order in which they appear
void Output()
Output the Pair in the format "[firstVal, secondVal]"
char CompareWith(Pair* otherPair)
Return the character '<', '=', or '>' according to whether the Pair is less than, equal to, or greater than otherPair
Precedence of comparisons is firstVal then secondVal
char ShowComparison(Pair* otherPair)
Compare with otherPair by calling CompareWith()
Output the two Pairs separated by the character returned by CompareWith(). Hint: Output each Pair using Output()
Note: For each type main() calls Input() twice to create two Pairs of that type.
Ex: If the input for two Integer Pairs is:
4 6 3 5
the first Pair is [4, 6], and the second Pair is [3, 5].
Ex: If the input of the program is:
4 6 3 5 4.3 2.1 4.3 2.1 one two three four
the output is:
[4, 6] > [3, 5] [4.3, 2.1] = [4.3, 2.1] [one, two] < [three, four]
#include#include using namespace std; /*** Template class Pair ***/ template class Pair { public: void Input(); void Output(); char CompareWith(Pair * otherPair) ; void ShowComparison(Pair * otherPair); private: TheType firstVal; TheType secondVal; }; // Return '<', '=', or '>' according to whether the Pair is less than, // equal to, or greater than the argument Pair template char Pair ::CompareWith(Pair * otherPair) { /* Type your code here. */ if (firstVal < otherPair->firstVal){ return '<'; } } // Input values into a pair template void Pair ::Input() { /* Type your code here. */ cin >> firstVal,secondVal; } // Output a Pair template void Pair ::Output() { /* Type your code here. */ cout << "[" << firstVal << ", " << secondVal<<"]"; } // Output both pairs with a comparison symbol in between template void Pair ::ShowComparison(Pair * otherPair) { /* Type your code here. */ cout << Output() << char() << otherPair() ; } /*** End template class Pair ***/ int main() { Pair intPair; Pair intOtherPair; intPair.Input(); intOtherPair.Input(); intPair.ShowComparison(&intOtherPair); Pair doublePair; Pair doubleOtherPair; doublePair.Input(); doubleOtherPair.Input(); doublePair.ShowComparison(&doubleOtherPair); Pair wordPair; Pair wordOtherPair; wordPair.Input(); wordOtherPair.Input(); wordPair.ShowComparison(&wordOtherPair); return 0; }
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