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

Define a class of rational numbers. A rational number is a number that can be re

ID: 3790930 • Letter: D

Question

Define a class of 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 and so on we mean the every day fraction, not the integer division this expression would produce in C++ programs.)

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. Include a one argument constructor that has a single parameter of type int for the numerator. The denominator is set to 1. Also include a default constructor that will initialize the numerator to 0 and denominator to 1. Overload the input ( >> )and output operators ( <<). The user should be able to input numbers of the form 1/2, 15/32 and so on. The output should be printed in the same format. Note that negative numbers such as -1/2 are also possible. Overload all the following operators, ==, <, <=, >, >- +, -, *, / so that they correctly apply to the type Rational.

Below is the testing program -

int main()
{
Rational n1(1,2), n2, n3;
cout << n1 << endl;
cin >> n2; // Input 2/3
cout << n2 << endl;
cout << n1 + n2 << endl;
cout << n1 - n2 << endl;
cout << n1 * n2 << endl;
cout << n1 / n2 << endl;
if (n1 == n2)
cout << "n1 equals n2" << endl;
else if (n1 > n2)
cout << n1 << " greater than " << n2 << endl;
else
cout << n1 << " less than " << n2 << endl;
cin >> n3; // Input -1/3
cout << n3 << endl;
if (n3 >= 0)
cout << n3 << " is non-negative" << endl;
else if (n3 <= 0)
cout << n3 << " is non-positive" << endl;

return 0;

Explanation / Answer

#include <iostream>
using namespace std;

class Rational
{
private:
int numerator;   //class variables
int denominator;
public:
Rational()   //default constructor
{}
Rational(int num)
{
numerator = num;
denominator = 1;
}
Rational(int num,int den)
{
numerator=num;
if(den != 0)
denominator=den;
else
cout<<" Invaid denominator : should not be 0";
}

Rational operator+(Rational r)
{
Rational temp;
temp.numerator=numerator*r.denominator+r.numerator*denominator;
temp.denominator=denominator*r.denominator;
return temp;
}
     Rational operator-(Rational r)
     {
     Rational temp;
temp.numerator=numerator*r.denominator-r.numerator*denominator;
temp.denominator=denominator*r.denominator;
return temp;
     }
     Rational operator*(Rational r)
     {
     Rational temp;
     temp.numerator=numerator*r.numerator;
     temp.denominator=denominator*r.denominator;
     return temp;
  
     }
     Rational operator/(Rational r)
     {
     Rational temp;
     temp.numerator=numerator*r.denominator;
     temp.denominator=denominator*r.numerator;
     return temp;
     }

void display()
{
cout<<numerator<<"/"<<denominator;
}
bool operator>=(Rational &R2)          //operator function
{
return ( (this->numerator >= R2.numerator && this->denominator ==
R2.denominator) || (this->denominator < R2.denominator &&
this->numerator == R2.denominator));
}
bool operator>=(int num)          //operator function
{
if((this->numerator /this->denominator) >= 0)
   return false;
   else
   return true;
}

bool operator>(Rational &R2)          //operator function
{
return ( (this->numerator > R2.numerator && this->denominator ==
R2.denominator) || (this->denominator < R2.denominator &&
this->numerator == R2.denominator));
}
friend ostream &operator<<( ostream &,const Rational & );
friend istream &operator>>( istream &, Rational &) ;
bool operator== (Rational &R2)          //operator function   
{
return (this->numerator == R2.numerator && this->denominator == R2.denominator);
}
};
ostream &operator<<( ostream &output,const Rational &R ) {
         output <<   R.numerator << "/"<<R.denominator ;
         return output;         
      }

       istream &operator>>( istream &input, Rational &R ) {
         input >> R.numerator >> R.denominator;
         return input;         
      }


int main()
{
Rational n1(1,2), n2, n3;
cout << n1 << endl;
cin >> n2; // Input 2/3
cout << n2 << endl;
cout << n1 + n2 << endl;
cout << n1 - n2 << endl;
cout << n1 * n2 << endl;
cout << n1 / n2 << endl;
if (n1 == n2)
cout << "n1 equals n2" << endl;
else if (n1 > n2)
cout << n1 << " greater than " << n2 << endl;
else
cout << n1 << " less than " << n2 << endl;
cin >> n3; // Input -1/3
cout << n3 << endl;
if(n3 >= 0)
{
cout << n3 << " is non-negative" << endl;
}
else
{
cout << n3 << " is non-positive" << endl;
}

return 0;
}

output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote