Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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 formStep 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