Computers and Technology

No 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 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(; 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; }

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 19:50, rosyposy43
Write a car class having two private member variables called tank and speed. write public methods called pumpgas and gofast. the method pumpgas gets an integer for gas that must be pumped. that value needs to be added to tank (no more than 20 gallons). it must return the amount of gas that is purchased ($4 per gallon). the method gofast should increase the speed by 5 each time it is called. write a constructor for the above class that initialized both variables to zero. write a tostring to display both the tank and speed when the car is printed. modify the car class to implement the interface comparable and an interface called carinter having the public methods in carinter. write the main program to create an array of size 5 of type car. create 5 car objects having each location of the array to refer to one of the cars. test the pumpgas, gofast, equals method on the array items. write an enhanced loop to print all the car values (using a tostring written last time).write a generic method to find the minimum of four items. pass int, double, char, string and car objects to test this method.
Answers: 1
image
Computers and Technology, 23.06.2019 09:30, cdavis379
:you areto design the controller for alight that functions both as an ordinary light and also as a motion activated light and alarm. a. if the manual switch s is on, then the light l is on. b. besides the manual switch, there is a motion detector, m1, which activatesthis light. c.if motion is detected but the light is on anyway because s is on, only then a secondoutput a, an alarm, is turned on. d. the disable switch, d, disables the motion activated light and alarmbut leaves manual control operation of the light using switch s.(i)read the problem statement and clearly identify the inputs and outputs for the circuit you are designing. (ii)create the truth table for this system; include the light, alarm, switch, disable, and the motion sensor.(iii)draw a schematic of this system.
Answers: 1
image
Computers and Technology, 23.06.2019 19:50, Aprillove7939
Which feature is selected to practice and save the timing of a presentation
Answers: 1
image
Computers and Technology, 24.06.2019 15:30, lakenyagillard79
Python. primary u. s. interstate highways are numbered 1-99. odd numbers (like the 5 or 95) go north/south, and evens (like the 10 or 90) go east/west. auxiliary highways are numbered 100-999, and service the primary highway indicated by the rightmost two digits. thus, the 405 services the 5, and the 290 services the 90. given a highway number, indicate whether it is a primary or auxiliary highway. if auxiliary, indicate what primary highway it serves. also indicate if the (primary) highway runs north/south or east/west.
Answers: 1
Do you know the correct answer?
No documentation (commenting) is required on this assignment. convert the orderedpair class, which i...

Questions in other subjects:

Konu
Geography, 23.05.2020 00:04