Write a rational number class. Recall a rational number is a rational number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide the following a default constructor to initialize the data members: 1. A default constructor to make rational objects without any argument. The constructor sets the rational object's numerator to 0, and denominator to 1. 2. EXTRA CREDIT: implement two other constructors: one that takes just numerator and one that takes both as parameters. REMEMBER that fractions cannot have a denominator of 0. You should also provide the following member functions (no simplifying rationals until part2): 1. an input function that reads from standard input the value for current object. The input should be in the form of 2/3 or 27/51. 2. an output function that displays the current object in the terminal. The output should also be in the form of 2/3, i.e., numerator/denominator. 3. Two accessor (getter) functions that return the numerator and denominator respectively. 4. a Add function that takes two rational objects as parameters. It sets the invoking object to be the sum of the two given rational numbers like op1 + op2. 5. a Subtract function, which will set the invoking object to the difference of op1 - op2 6. a Multiply function, which will set the invoking object to the product of op1 * op2 7. a Divide function, which will set the invoking object to the quotient of op1 / op2.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Code in c++

Requirements
Write a rational number class. Recall a rational number is a rational number, composed of two integers
with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You
should represent rational numbers using two int values, numerator and denominator.
A principle of abstract data type construction is that constructors must be present to create objects with
any legal values. You should provide the following a default constructor to initialize the data members:
1. A default constructor to make rational objects without any argument. The constructor sets the
rational object's numerator to 0, and denominator to 1.
EXTRA CREDIT: implement two other constructors: one that takes just numerator and one that
takes both as parameters. REMEMBER that fractions cannot have a denominator of 0.
2.
You should also provide the following member functions (no simplifying rationals until part2):
1. an input function that reads from standard input the value for current object. The input should be in
the form of 2/3 or 27/51.
2. an output function that displays the current object in the terminal. The output should also be in the
form of 2/3, i.e., numerator/denominator.
Two accessor (getter) functions that return the numerator and denominator respectively.
4. a Add function that takes two rational objects as parameters. It sets the invoking object to be the
sum of the two given rational numbers like op1 + op2.
a Subtract function, which will set the invoking object to the difference of op1 - op2
a Multiply function, which will set the invoking object to the product of op1 * op2
7. a Divide function, which will set the invoking object to the quotient of op1 / op2.
3.
5.
6.
Note the formula for adding two rational numbers (same for subtraction but change + to -):
numerator1/denominator1
+ numerator2/denominator2 =
( n1*d2 + n2*d1)/ (d1* d2)
Set the numerator to the top part ( nl * d2 + n2 * d1) and the denominator to the bottom (d1 * d2) in your
object. Remember that division multiplies the reciprocal of the second operand.
Main Program
The following code segment demonstrate the usage of the above mentioned member functions, and
constructors. Your main function should be some code like this.
int main ()
{
// TODO: declare 2 rational objects (op1, result) using the default
constructor
char oper;
cout << "Enter opl (in the format of p/q) : ";
// TODO: use your input member function to read the first op1
rational
// Main loop to read in rationals and compute the sum
do {
cout << "\nEnter operator [+, -,
1, *, =, c (lear),
a (ccessors), q(uit)]: ";
// TODO: read in a character operator into oper as shown above.
// TODO: Pseudocode below says when to use your input member
function to read the second operand (i.e. rational object)
if (oper in "+-*/")
cout << "\nEnter op2 (in the format of
p/q) :";
// TODO: Implement a switch or multiway if statement with one case
for each option above where
// '+','*','/','-' input a rational operand and calculate
result.oper (result, op1)
// '=' outputs the current result,
// 'c' to clear current result, use input function to read first
operand into result,
// 'a' to test accessors, ' q' to quit.
} while (oper != 'q');
return 0;
}
Transcribed Image Text:Requirements Write a rational number class. Recall a rational number is a rational number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide the following a default constructor to initialize the data members: 1. A default constructor to make rational objects without any argument. The constructor sets the rational object's numerator to 0, and denominator to 1. EXTRA CREDIT: implement two other constructors: one that takes just numerator and one that takes both as parameters. REMEMBER that fractions cannot have a denominator of 0. 2. You should also provide the following member functions (no simplifying rationals until part2): 1. an input function that reads from standard input the value for current object. The input should be in the form of 2/3 or 27/51. 2. an output function that displays the current object in the terminal. The output should also be in the form of 2/3, i.e., numerator/denominator. Two accessor (getter) functions that return the numerator and denominator respectively. 4. a Add function that takes two rational objects as parameters. It sets the invoking object to be the sum of the two given rational numbers like op1 + op2. a Subtract function, which will set the invoking object to the difference of op1 - op2 a Multiply function, which will set the invoking object to the product of op1 * op2 7. a Divide function, which will set the invoking object to the quotient of op1 / op2. 3. 5. 6. Note the formula for adding two rational numbers (same for subtraction but change + to -): numerator1/denominator1 + numerator2/denominator2 = ( n1*d2 + n2*d1)/ (d1* d2) Set the numerator to the top part ( nl * d2 + n2 * d1) and the denominator to the bottom (d1 * d2) in your object. Remember that division multiplies the reciprocal of the second operand. Main Program The following code segment demonstrate the usage of the above mentioned member functions, and constructors. Your main function should be some code like this. int main () { // TODO: declare 2 rational objects (op1, result) using the default constructor char oper; cout << "Enter opl (in the format of p/q) : "; // TODO: use your input member function to read the first op1 rational // Main loop to read in rationals and compute the sum do { cout << "\nEnter operator [+, -, 1, *, =, c (lear), a (ccessors), q(uit)]: "; // TODO: read in a character operator into oper as shown above. // TODO: Pseudocode below says when to use your input member function to read the second operand (i.e. rational object) if (oper in "+-*/") cout << "\nEnter op2 (in the format of p/q) :"; // TODO: Implement a switch or multiway if statement with one case for each option above where // '+','*','/','-' input a rational operand and calculate result.oper (result, op1) // '=' outputs the current result, // 'c' to clear current result, use input function to read first operand into result, // 'a' to test accessors, ' q' to quit. } while (oper != 'q'); return 0; }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 18 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY