Define a class for rational numbers.

Hypothesize a scenario in which one could intentionally misstate liabilities for his or her personal financial gain.
August 5, 2017
Compare Different Productivity Software in the Cloud.
August 5, 2017
Show all

Define a class for rational numbers.

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 ½, etc we mean the 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 rationalNum

Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate value. Also include a constructor that has only a single parameter of type int; call this single parameter whole_number and define the constructor so that the object will be initialized to the rational number whole_number/1. Also include a default constructor that initializes an object to 0    (that is, to 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, -300/-400 are all possible input. The input operator, >>, reads the string 15/32 as 

Overload all of the following operators so that they correctly apply to the type rationalNum: ==, <, >, +, -, *, 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 numbers, a/b is less than c/d provided a*d is less than c*b. 

  1. (a/b + c/d) is given by:

Numerator =a*d + c*b

Denominator = b*d

  1. (a/b – c/d) is given by 

Numerator = a*d – c*b

Denominator = b*d

Leave a Reply

Your email address will not be published. Required fields are marked *