Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Points: 50 Description: Define a class called Rational. A rational number is a n

ID: 3723140 • Letter: P

Question

Points: 50 Description: Define a class called Rational. A rational number is a number that can be represented as the quotient of two integers (i.e., fractions). 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.) Your class should represent rational numbers as two values of type int, one for the numerator and one for the denominator The class should include the following three constructors: a default constructor that initializes an object to 0 (that is, to 0/1); a constructor that has only a single parameter of type int; call this single parameter wholeNumber and the object will be initialized to the rational number wholeNumber/1; a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. e e Overload all the following operators so that they correctly apply to the type Rational: ==, x, +,-(with two Overload the input and output operators» and

Explanation / Answer

Hi, you can try pulling off the code given below, I feel that t will work for most test cases as I checked.
Have fun with the same.
I also implemented a simplify function as I was unsure for the question if -21/-9 can come while multiplication, addition or subtraction.
You can uncomment the same to make it useful.

#include <iostream>

using namespace std;

class Rational

{

private:

void check()

{ if ( b == 0 ) throw std::runtime_error("Denominator of rational number cannot be zero"); }

void simplify()

{

int m = 1;

if ((a > 0 && b < 0) || (a < 0 && b > 0)) m = -1;

a = m * abs(a / __gcd(a, b));

b = abs(b / __gcd(a, b));

}

protected:

int a;

int b;

public:

Rational () : a(0), b(1) // first constructor without any argument

{ } // no need for check(), in this case

Rational (int num) : a(num), b(1) // second constructor with one argument

{ } // no need for check(), in this case

Rational (int first, int second) : a(first), b(second)

{ check(); }

friend ostream &operator << (ostream &os, Rational &p)

{

os << p.a << "/" << p.b; //prints 3 and 9 that i giving in main

return os;

}

Rational operator + (Rational &s)

{

a = a * s.b + s.a * b;

b = b * s.b; // here i will sum a + b

// simplify();

return *this;

}

Rational operator - (Rational &s)

{

a = a * s.b - s.a * b;

b = b * s.b; // here i will sum a - b

// simplify();

return *this;

}

Rational operator *(Rational &s)

{

a = a * s.a; //here i want multiply a * b

b = b * s.b;

// simplify();

return *this;

}

Rational operator /(Rational &s)

{

a = a * s.b; //here i want multiply a / b

b = b * s.a;

// simplify();

return *this;

}

bool operator==(const Rational &r) const

{

int c1 = a * r.b;

int c2 = b * r.a;

if (c1 == c2) // here i want add requred methods for comparison of two rational numbers.

return true;

else

return false;

}

bool operator<(const Rational &r) const {

int lside = a * r.b;

int rside = b * r.a;

return (lside < rside);

}

bool operator>(const Rational &r) {

int lside = a * r.b;

int rside = b * r.a;

return (lside > rside);

}

bool operator<=(const Rational &r) {

return ( (a * r.b < b * r.a) || (a * r.b == b * r.a) );

}

bool operator>=(const Rational& r) {

return ( (a * r.b > b * r.a) || (a * r.b == b * r.a) );

}

};

int main()

{

Rational r1 (3);

cout << r1 << endl;

Rational r2(6, 2);

cout << r2 << endl;

if (r1 == r2) {

cout << "Both are equal" << endl;

} else {

cout << "Both are different" << endl;

}

Rational r3 = r1 - r2;

cout << r3 << endl;

cout << endl;

return 0;

}