Question
Refer to the following program instructions: (Actual problem is at the bottom in bold) Define a class for rational numbers. A rational number is a
Refer to the following program instructions: (Actual problem is at the bottom in bold)
Define a class for rational numbers. A rational number is a number that can be represented as a quotient of two integers. For example, 1/2, 3/4, 64/2 and so forth are all rational numbers. (By 1/2, etc., we mean tha everyday meaning of the fraction, not the integer division this expression would produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call the class Rational.
Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. Also include a constructor that has only a single parameter of type int; call this single parameter wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber/1. Also include a default constructor that initializes an object to 0 (that is 0/1).
Overload the input and output operators >> and <<. Numbers are to be input and output in the form 1/2, 15/32, 300/401 and so forth. Note that the numerator, the denominator or both may contain a minus sign, so -1/2, 15/32 and -300/401 are also possible inputs. Overload all of the following operators so that they correctly apply to the type Rational: ==, <. <=, >, >=, +, -, * and /. Also write a test program to test your class.
Sample Test Data:-
Testing declarations
Rational x, y(2), z(-5,-6), w(1,-3);
z = 5/6, y = 2/1, z = 5/6, w = -1/3
Testing << overloading:
Enter a fraction in the format integer_numerator/integer_denominator
You entered the equivalent of: 9/7
5/6 - (-1/3) = 7/6
Testing the constructor and normalization routines:
y =Rational(-128, -48) outputs as 8/3
y =Rational(-128, 48)outputs as -8/3
y =Rational(128, -48) outputs as -8/3
Rational a(1,1); a outputs as: 1/1
-8/3 * 1/1 = -8/3
Testing arithmetic and relational operator overloading
25/9 * 3/5 = 5/3
25/9 + 3/5 = 152/45
25/9 - 3/5 = 98/45
25/9 / 3/5 = 125/27
25/9 < 3/5 = 0
25/9 < 25/9 = 0
25/9 <= 3/5 = 0
25/9 <= 25/9 = 1
25/9 > 3/5 = 1
25/9 > 25/9 = 0
25/9 >= 3/5 = 1
25/9 >= 25/9 = 1
-7/3 * 3/5 = -7/5
-7/3 + 3/5 = -26/15
-7/3 - 3/5 = -44/15
-7/3 / 3/5 = -35/9
-7/3 < 3/5 = 1
-7/3 < -7/3 = 0
-7/3 <= 3/5 = 1
-7/3 <= -7/3 = 1
-7/3 > 3/5 = 0
-7/3 > -7/3 = 0
-7/3 >= 3/5 = 0
-7/3 >= -7/3 = 1
PreviousNext
Define your ADT class in separate files so that it can be compiled separately.
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