Question
C++ No documentation (i.e., commenting) is required on this assignment. Convert the OrderedPairs class, which is provided below, into a templated class. Note that it
C++
No documentation (i.e., commenting) is required on this assignment.
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:
string empty = ""; myList2[i].setFirst(empty + char('a' + rand() % 13)); myList2[i].setSecond(empty + char('n' + rand() % 13));
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.)
#include/* 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 { class orderedPair { public: static const int DEFAULT_VALUE; orderedPair(int newFirst = DEFAULT_VALUE, int newSecond = DEFAULT_VALUE); void setFirst(int newFirst); void setSecond(int newSecond); int getFirst(); int getSecond(); orderedPair operator+(const orderedPair& right); bool operator<(const orderedPair& right); void print(); private: int first; int second; }; // You will need a template prefix here above this declaration const int orderedPair::DEFAULT_VALUE = int(); }
Here is the implementation file, orderedpairs.cpp
#include "orderedpair.h" #includeusing 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#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; }
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