subject

C++ Template PracticeNo documentation (commenting) is required on this assignment. Convert the OrderedPair 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, create a programmer-defined exception class named "DuplicateMemberError" and add an if statement to each of the two mutators to throw this exception 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. Put your class in a namespace named "cs_pairs"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() % 26)); myList2[i].setSecond(empty + char('A' + rand() % 26));Here is the header file, orderedpair. 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: // Use the first of the following two lines to make the non-templated version work. // Use the second one when you begin converting to a templated version. static const int DEFAULT_VALUE = int(); // static const int DEFAULT_VALUE; OrderedPair(int newFirst = DEFAULT_VALUE, int newSecond = DEFAULT_VALUE); void setFirst(int newFirst); void setSecond(int newSecond); int getFirst() const; int getSecond() const; OrderedPair operator+(const OrderedPair& right) const; bool operator<(const OrderedPair& right) const; void print() const; private: int first; int second; }; // Leave the following const declaration commented out when you are testing the non-templated version. // Uncomment it when you begin converting to a templated version. // To convert to a templated version you will need a template prefix here above this declaration // const int OrderedPair::DEFAULT_VALUE = int();}Here is the implementation file, orderedpair. cpp#include "orderedpair. h"#include 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() const { return first; } int OrderedPair::getSecond() const { return second; } OrderedPair OrderedPair::operator+(const OrderedPair& right) const { return OrderedPair(first + right. first, second + right. second); } bool OrderedPair::operator<(const OrderedPair& right) const { return first + second < right. first + right. second; } void OrderedPair::print() const { cout << "(" << first << ", " << second << ")"; }}Here is the client file#include #include #include #include "orderedpair. h"using namespace std;using namespace cs_pairs;int main() { int num1, num2; OrderedPair myList[10]; srand(static_cast(time(0))); 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; /* use this before you've implemented the exception handling in the class: */ x. setFirst(num1); x. setSecond(num2); /* use this after you've implemented the exception handling in the class: 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;}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 22:00
What is a distinguishing feature of today’s graphic application software?) graphic applications are used today on a variety of devices, including touch-screen kiosks and mobile phones.
Answers: 3
question
Computers and Technology, 23.06.2019 18:30
Write a program that prints the day number of the year, given the date in the form month-day-year. for example, if the input is 1-1-2006, the day number is 1; if the input is 12-25-2006, the day number is 359. the program should check for a leap year. a year is a leap year if it is divisible by 4, but not divisible by 100. for example, 1992 and 2008 are divisible by 4, but not by 100. a year that is divisible by 100 is a leap year if it is also divisible by 400. for example, 1600 and 2000 are divisible by 400. however, 1800 is not a leap year because 1800 is not divisible by 400.
Answers: 3
question
Computers and Technology, 24.06.2019 02:10
Consider the usual algorithm to convert an infix expression to a postfix expression. suppose that you have read 10 input characters during a conversion and that the stack now contains these symbols: (5 points) | | | + | | ( | bottom |_*_| now, suppose that you read and process the 11th symbol of the input. draw the stack for the case where the 11th symbol is
Answers: 2
question
Computers and Technology, 24.06.2019 17:40
Which of the following processes applications across multiple computing devices? a. functional application b. distributed system c. workgroup information silo d. information silo
Answers: 3
You know the right answer?
C++ Template PracticeNo documentation (commenting) is required on this assignment. Convert the Order...
Questions
question
Social Studies, 21.01.2021 17:20
question
Social Studies, 21.01.2021 17:20
question
History, 21.01.2021 17:20
question
Mathematics, 21.01.2021 17:20
Questions on the website: 13722363