Question: main.cpp: Define a class for rational numbers. You should follow the principles for Abstract Data Types (ADTs) in writing the class. A rational number is

 main.cpp: Define a class for rational numbers. You should follow the

principles for Abstract Data Types (ADTs) in writing the class. A rational

main.cpp:

number is expressed as a ratio of two integers. The division is

Define a class for rational numbers. You should follow the principles for Abstract Data Types (ADTs) in writing the class. A rational number is expressed as a ratio of two integers. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32, 65/4, 16/5. You should represent rational numbers by two int values, numerator and denominator. We want to store rational numbers in canonical form, meaning the rational number 9/12 would be stored as 3/4 and 25/5 would be stored as 5/1. This is discussed more below A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values (i.e. a constructor with two int parameters). Since every int is also a rational number, for example, 2/1 or 17/1, you should provide a constructor with a single int parameter, which just sets the numerator to the value of the parameter and the denominator to 1. The default constructor must set the numerator to 0 and denominator to 1 Use operator overloading to implement addition (+), subtraction (-), multiplication (*), division (/), negation (-) less than and equal to ), as well as insertion (>). Insertion operator must simply print the rational number in a format like -9/13, no new line or any other text. And extraction operator must read a rational number with a format like: 1/2, -2/3 and store it in canonical form. This class does not have accessor (e.g getNumerator) or mutator (e.g. setDenominator) functions The arithmetic operator functions must return another object of type Rational. For example, here is what the operator+ function could look like: Rational operator+(const Rational &rl, const Rational &r2) int resultNum, resultDenom; resultNum .. .; resultDe nom . . . . Rational res (resultNum, resultDenom) return res; Always store the canonical form of the rational number; that is, always divide out the greatest common divisor (GCD) of the numerator and denominator. You can find the code for GCD calculation here: http://www.aivosto.com/visustin/sample/gcd-c.htm You can make this a private helper function or a non-member function Hint: Use the GCD function in the constructor that has two inputs (numerator and denominator); only use the GCD function here. Then, like the code example above, when you use the constructor to create the result object it would be in a canonical form

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!