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

Templared code highlighted.... implemented in header files and template implementation only in header files. In .cpp files it is not necessry.

#include <iostream>

/* precondition for setFirst and setSecond: the values of first and second cannot be equal,
except when they are both equal to DEFAULT_VALUE.
*/


namespace cs_pairs {
   template <typename T>
class orderedPair {
public:
static T const& DEFAULT_VALUE;
orderedPair(T const& newFirst = DEFAULT_VALUE, T const& newSecond = DEFAULT_VALUE);
void setFirst(T const& newFirst);
void setSecond(T const& newSecond);
T const& getFirst();
T const& getSecond();
orderedPair operator+(const orderedPair& right);
bool operator<(const orderedPair& right);
void print();
private:
T const& first;
T const& second;
};


// You will need a template prefix here above this declaration
const T const& orderedPair::DEFAULT_VALUE = int();

}

//Here is the implementation file, orderedpairs.cpp
#include \"orderedpair.h\"
#include <iostream>
using namespace std;

namespace cs_pairs {
orderedPair::orderedPair(int newFirst, int newSecond) {
setFirst(newFirst);
setSecond(newSecond);
}

void orderedPair::setFirst(int newFirst) {
// if statement to throw an exception if precondition not met goes here.
first = newFirst;
}

void orderedPair::setSecond(int newSecond) {
// if statement to throw an exception if precondition not met goes here.
second = newSecond;
}

int orderedPair::getFirst() {
return first;
}

int orderedPair::getSecond() {
return second;
}

orderedPair orderedPair::operator+(const orderedPair& right) {
return orderedPair(first + right.first, second + right.second);
}


bool orderedPair::operator<(const orderedPair& right) {
return first + second < right.first + right.second;
}


void orderedPair::print() {
cout << \"(\" << first << \", \" << second << \")\";
}
}
//Here is the client file.
#include <iostream>
#include \"orderedpair.h\"
using namespace std;
using namespace cs_pairs;

int main() {
int num1, num2;
orderedPair myList[10];
  
cout << \"default value: \";
myList[0].print();
cout << endl;
  
for (int i = 0; i < 10; i++) {
myList[i].setFirst(rand() % 50);
myList[i].setSecond(rand() % 50 + 50);
}
  
myList[2] = myList[0] + myList[1];
  
if (myList[0] < myList[1]) {
myList[0].print();
cout << \" is less than \";
myList[1].print();
cout << endl;
}
  
for (int i = 0; i < 10; i++) {
myList[i].print();
cout << endl;
}
  
cout << \"Enter two numbers to use in an orderedPair. Make sure they are different numbers: \";
cin >> num1 >> num2;
orderedPair x;
try {
x.setFirst(num1);
x.setSecond(num2);
} catch (orderedPair::DuplicateMemberError e) {
x.setFirst(0);
x.setSecond(0);
}

cout << \"The resulting orderedPair: \";
x.print();
cout << endl;
}

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 &
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 &
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 &

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site