Question
Extend rational class [MY PREVIOUS VERSION GIVEN BELOW] (from lab6, lab7) to overload the input operator>> and output operator < seq_length; rational * fractions =
Extend rational class [MY PREVIOUS VERSION GIVEN BELOW] (from lab6, lab7) to overload the input operator>> and output operator<<, and all of the following operators. And then test them in your main function. Take your files from lab7 [ALL GIVEN AND LABELED BELOW] and copy them to a lab8 directory. In the parent directory, where lab7 and lab8 are each subdirectories, issue the following commands: cp lab7/rational7.cpp lab8/rational8.cpp cp lab7/rational7.h lab8/rational8.h cp lab7/lab7.cpp lab8/lab8.cpp None of the lab7 program is needed in lab8. It'll be a brand new main program to test the new rational class. Lab8 requires that you declare and implement all of the following operators: Boolean operators ==, !=, <, <=, >, >= Arithmetic operators +, - (subtraction), * (multiplication), / , - (negation) I/O operators >> (input), << (output) Assume that all of the operators are overloaded friend functions as discussed in the textbook and lectures. Hint: Think about the return type for each category of operator: *** Remember boolean operators return a bool. This means you can implement them simply by returing a boolean expression. *** Remember to compare computed values (as a double) like we did with the isEquals member function in lab6 rather than the individual components (i.e. numerators and denominators). *** Remember that Arithmetic operators compute a value that is the same type as the calling object. *** Remember that I/O operators >> and << can be chained to output multiple values in a simgle line of code. What does this imply about the return types? The main program in lab8.cpp should have a do-while loop that asks the user to enter an operator. The program should expect to read a string for the operator: "+", "-", "/", "*", "-1" (for negation), "==", "<", "<=", ">", ">=", "!=", "<<", ">>" and map it to the overloaded operator in the class. For binary operators, the program should ask the user to enter two (2) rationals. Use the operator<< to read in the rationals and compute the values and print out the results. Allow the user to enter the word "quit" or "exit" to exit the program. General Requirements At this stage of CS2, please pay attention to the following requirements on all code that you write: Separate your program into multiple files (modules): class header file.h class implementation file.cpp and driver lab#.cpp (refer to lab7 exercise). Write comments for each function in the class declaration (.h) file. Remember we are trying to define Abstract Data Types with our classes. An ADT has an public interface with accompanying comments. Write comments for each function in the implementation (.cpp) file, with the following information: One line short description about the function: what the function is supposed to do Describe each parameter Describe the precondition Describe the postcondition Describe the return value Use const modifier on pass-by-reference parameters that the function is not supposed to modify Use const modifier on member functions which don't modify the invoking object In your main function, always display a message about what's being tested followed by the result, for example: Detailed Requirement Your main function should work as follows: Let's start to test operators defined for rational class. Enter the operator (+,-,*,/,==,..., enter -1 for negation):+ Enter the two operands:1/2 -3/4 (1/2)+(-3/4)=-1/4 Continue(y/n): y Enter the operator (+,-,*,/,==,..., enter -1 for negation):-1 Enter the operand:1/2 -(1/2)=-1/2 Continue(y/n): y Enter the operator (+,-,*,/,==,..., enter -1 for negation):- Enter the operand:1/2 1/4 (1/2)-(1/4)=1/4 Continue(y/n): n Bye! Previous work asked for in first paragraph: LAB7.CPP: //main function for lab 7 #include #include using namespace std; int main() { char answer; int seq_length; cout <<"Enter the number of rational numbers you want to add/multiply: "; cin >> seq_length; rational * fractions = new rational[seq_length]; for (int i= 0; i < seq_length; i++) fractions[i].set(1, i+1); rational r_sum, r_product(1); for (int i=0; i > answer; } while (answer=='y' || answer=='Y'); //test getters cout << "C's numerator is: " << c.get_numerator() << endl; cout << "C's denominator is: "<< c.get_denominator() << endl; } RATIONAL7.CPP //class implementation #include "rational7.h" #include using namespace std; //Definitions of all member functions int rational::counter=0; rational::rational (int p, int q) { if (q==0) exit(1); numerator = p; denominator = q; counter++ ; cout <<"So far "<< counter <<" rational objects created "; } /* Set the invoking object's value from user input */ void rational::input() { char c; int top, bottom; bool valid = true; //keep reading until valid input is entered do { cin >> top >> c >> bottom; if ( c!= '/' || bottom == 0) { cout << " Wrong input. Enter rational (p/q): "; valid = false; } else valid = true; } while (!valid); //set the invoking object's numerator, denominator numerator = top; denominator = bottom; simplify(); } /* display invoking objects value in the standard output, in the form of numerator/ denominator */ void rational::output() { cout << numerator << "/" << denominator; } // return the invoking objects numerator int rational::get_numerator() const { return numerator; } int rational::get_denominator() const { return denominator; } void rational::set(int p, int q) { if (q==0) exit(1); numerator = p; denominator = q; simplify(); } void rational::sum(rational op1, rational op2) { denominator = op1.denominator * op2.denominator; numerator = op1.numerator * op2.denominator + op2.numerator * op1.denominator; } void rational::product(const rational & op1, const rational & op2) { denominator = op1.denominator * op2.denominator; numerator = op1.numerator * op2.numerator; } bool rational::isEqual(const rational & op) const { if (double(numerator)/denominator == double(op.numerator)/op.denominator) return true; else return false; } void rational::simplify() { int gcd = 0; for (int i=numerator; i>0; i--) { if (denominator%i == 0 && numerator%i == 0) { gcd = i; break; } } if (gcd =! 0) { numerator = numerator/gcd; denominator = denominator/gcd; } } RATIONAL7.H //class declaration /* This rational class practice basics of defining and using class. */ #include #include using namespace std; class rational { public: /* default constructor set the rational number to 0, (i.e., numerator is 0, denominator is 1) */ rational (int p=0, int q=1); /* Set the invoking object's value from user input */ void input(); /* Display invoking object's value in the standard output, in the form of numerator/denominator */ void output(); //return the invoking object's numerator int get_numerator()const; //return the invoking object's denominator int get_denominator()const; // Set invoking object to be the sum of op1 and op2 void Sum (rational op1, rational op2); //set numerator and denominator to the user's input value void set(int p, int q); //get the product of two rational numbers void product( const rational & op1, const rational & op2); //check equality bool isEqual( const rational & op)const; static int counter; private: int numerator; int denominator; int simplify(); //simplify rational number };
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