Question: Write a class Rational to represent rational numbers. (Language: C++) (a) Your class should have private member variables for the numerator and denominator. (b) Your

Write a class Rational to represent rational numbers. (Language: C++)

(a) Your class should have private member variables for the numerator and denominator.

(b) Your class should have two constructors; one that takes no arguments, and one that takes two arguments.

(c) Write a Getter (Accessor) and Setter (Mutator) for each of the member variables. Perform error checking for denominator Setter.

(d) Add a public member function to your class:

void Print();

If numerator is 5 and denominator is 8, it should print 5/8".

(e) Add a public member function to your class:

void Reduce();

A member function that puts the rational into its most reduced form. For instance, it would change 24/36 to 2/3.

(f) Implement += as a member function (you are NOT required to reduce your fraction).

Rational& operator +=(const Rational& rhs);

Modifies the Rational it is called on to add rhs.

(g) Implement * operator as a non-member function. You are NOT required to reduce your fraction.

Rational operator *(const Rational& lhs, const Rational& rhs);

Returns a new Rational that is the product of the two Rationals passed in.

(h) Implement + operator as a non-member function. You are NOT required to reduce your fraction.

Rational operator +(const Rational& lhs, const Rational& rhs);

Returns a new Rational that is the sum of the two Rationals passed in.

(i) Implement == operator as a non-member function.

bool operator ==(const Rational& lhs, const Rational& rhs);

Returns true if a==b and false if not. Note; if a is 6/8 and b is 3/4, it should return true.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!