Question: Use C++ Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For
Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2 and so on we mean the everyday 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. Include a default constructor that initializes an object to 0 (that is, to 0/1). You should include a function to normalize ghe values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/-8 would be represented the same as -1/2. To help with the normalize function, it may be helpful to also implement a greatest common divisor function. Overload the input and output operators >> and <. numbers are to be input and output in the form so forth. note that numerator denominator or both may contain a minus sign also possible inputs. overload all following operators they correctly apply type rational :="<." .> , > , + ,-, * , and / . Write a test program to test your class. Hints: Two rational numbers a/b and c/d are equal if a'd equals c*b. If b and d are positive rational numbers, a/b is less than c/d provided a d is less than c"b. You should include a function to normalize the values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after 4/-8 would be represented the same as-1/2 normalization On the next page is what your program should look like. It should ask for 3 fractions to be inputted, then test each of the operators on the functions. First with inputs 1 and 2, then with inputs 2 and 3. Note that the results displayed are the normalized results
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
