Convert the OrderedPairs class which is provided below into
Convert the OrderedPairs class, which is provided below, into a templated class. Note that it will only work with types that have the operators + and < and << overloaded. But you should be able to try your templated class out with types string, myString, double, feetInches, and fraction.
Also add an if statement to each of the two mutators to throw an exception named \"DuplicateMemberError\" if the precondition has not been met. The precondition is given as a comment in the header file. Notice that you can test your exception handling by entering the same number twice when prompted for two numbers.
Finally, to show that your class will work with different types, and also to show that you know how to use a templated class as a client, modify the given client file so that it uses your class using int as the type parameter, and then, in the same main(), repeat the code again with a few changes necessary to make it use ordered pairs of strings instead of ordered pairs of ints. One of the things you\'ll have to change is the generation of the random values for the ordered pairs. Here\'s what I used:
------------------------------------------------------------------------------------------------------------------------------------------------------
Here is the header file, orderedpairs.h. The syntax for declaring a constant in a class may look mysterious. To use constants in a class, we have to declare it inside the class, then assign it a value outside the class, as you\'ll see below. (That\'s actually not true for int constants -- they can be assigned inside the class -- but we want our code to be flexible enough to handle different types.)
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Here is the implementation file, orderedpairs.cpp
----------------------------------------------------------------------------------------------------------------------------
Here is the client file.
Solution
SOURCE CODE:
#include<iostream>
#include <cstdlib>
#include <string>
#include <stdexcept>
using namespace std;
template <class T>
class orderedPair {
public:
orderedPair()
{
setFirst(0);
setSecond(0);
}
orderedPair(T newFirst, T newSecond)
{
setFirst(newFirst);
setSecond(newSecond);
}
void setFirst(T newFirst)
{
first = newFirst;
}
void setSecond(T newSecond)
{
second = newSecond;
}
T getFirst()
{
return first;
}
T getSecond()
{
return second;
}
orderedPair operator+(const orderedPair& right)
{
return orderedPair(first + right.first, second + right.second);
}
bool operator<(const orderedPair& right)
{
return (first + second) < (right.first + right.second);
}
void print()
{
cout << \"(\" << first << \", \" << second << \")\";
}
private:
T first;
T second;
};
int main()
{
orderedPair<int> num1 ;
num1.setFirst(6);
num1.setSecond(7);
orderedPair<double> num2 ;
num2.setFirst(8.7);
num2.setSecond(9.4);
cout<<\"Num1 :\";
num1.print();
cout<<\"\ \";
cout<<\"Num2 :\";
num2.print();
cout<<\"\ \";
orderedPair<int> num3;
orderedPair<double> num4;
num3.setFirst(4);
num3.setSecond(9);
num4.setFirst(4.8);
num4.setSecond(7.4);
cout<<\"Num3 :\";
num3.print();
cout<<\"\ \";
cout<<\"Num4 :\";
num4.print();
cout<<\"\ \";
orderedPair<int> res1;
orderedPair<double> res2;
cout<<\"Result1 :\";
res1=num1+num3;
res1.print();
cout<<\"\ \";
cout<<\"Result2 :\";
res2=num2+num4;
res2.print();
cout<<\"\ \";
cout<<\"Condition 1: \";
num1.print();
cout<<\" < \";
num3.print();
cout<<\" : \";
cout<<(num1<num3);
cout<<\"\ \";
cout<<\"Condition 2: \";
num2.print();
cout<<\" < \";
num4.print();
cout<<\" : \";
cout<<(num2<num4);
cout<<\"\ \";
}
OUTPUT:
Num1 :(6, 7)
Num2 :(8.7, 9.4)
Num3 :(4, 9)
Num4 :(4.8, 7.4)
Result1 :(10, 16)
Result2 :(13.5, 16.8)
Condition 1: (6, 7) < (4, 9) : 0
Condition 2: (8.7, 9.4) < (4.8, 7.4) : 0


