Question
In C++ Write a class for rational numbers. Each object in the class should have two integer values that define the rational number: the numerator
In C++ Write a class for rational numbers. Each object in the class should have two integer values that define the rational number: the numerator and the denominator. For example, the fraction 5/6 would have a denominator of 5 and a numerator of 6. Include a constructor with two arguments that can be used to set the numerator and denominator (forbidding zero in the denominator). Provide default values of zero for the numerator and one for the denominator. Overload the input and output operators. Numbers are to be read and written in the form 1/2, 32/15, 300/401, and so forth. Note that the numerator, the denominator, or both may contain a minus sign, so -1/2, 32/-15, and -300/-401 are possible. Include a function to normalize the values stored so that, after normalization, the denominator is positive and as small as possible. For example, after normalization, 4/-8 would be represented the same as -1/2. Overload the usual arithmetic operators to provide addition, subtraction, multiplication, and division of two rational numbers. Overload the usual comparison operations to allow comparison of two rational numbers.
Hints: Two rational numbers a/b and c/d are equal if a*d equals c*b. For positive rational numbers, a/b is less than c/d, provided a*d is less than c*b.
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