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

I am having some issues with figuring out exactly what I am supposed to do with

ID: 3543252 • Letter: I

Question

I am having some issues with figuring out exactly what I am supposed to do with this progam and I'm unsure of what the outcome should be.  As such, I do not have a working program, but only a sort of rough draft. Here's the question:


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. 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 tha can be used to set the ember variables of an object to any legitimate values. Also include a contructor 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_numer/1. Also include a default constructor that initialized 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, etc. Note that the numerator, the denominator, or both may contain a minus sign so -1/2, -15/32, etc are also possible inputs. Overload all of the following operators so that they correctly apply to the type Rational: ==, <, <=, >=, +, -, *, and /.


It's not much, but here's the outline I basically have:

#include <iostream>
using namespace std;

//declaration of the class Rational. Contains public and private
//memeber functions as well as friend functions.
class Rational
{
      public:
             Rational (int numerator, int denominator);
             Rational (int numerator);
             Rational ();
             
             friend Rational operator + (const rational&, const rational&);
             friend Rational operator - (const rational&, const rational&);
             friend Rational operator * (const rational&, const rational&);
             friend Rational operator / (const rational&, const rational&);
             
             friend bool operator < (const rational&, const rational&);
             friend bool operator <=(const rational&, const rational&);
             friend bool operator > (const rational&, const rational&);
             friend bool operator >= (const rational&, const rational&);
             friend bool operator == (const rational&, const rational&);
             
             friend ostream& operator << (ostream&, const rational&);
             //does not have a constant argument because it uses input
             friend istream& operator >> (istream&, rational&);
      private:
              int first, second;
};

  int didget_to_int (char c);
  
int main ();
{
    Rational        ;
    cout <<         ;
    
    
    
system ("pause");
return 0;

}

Explanation / Answer

You are looking for this, I suppose...


/*

* Utkarsh Chanchlani

* Overloading Rational Number operators

*/

#include<iostream>

using namespace std;


int HCF(int, int);


class Rational{

public:

//constructor

Rational(int, int);

Rational(int);

Rational();



//destructor

~Rational();


//binary operators

Rational operator+(const Rational&);

Rational operator-(const Rational&);

Rational operator*(const Rational&);

Rational operator/(const Rational&);


//binary relations

bool operator==(const Rational&);

bool operator<(const Rational&);

bool operator<=(const Rational&);

bool operator>(const Rational&);

bool operator>=(const Rational&);


//basic input/output

friend ostream& operator<<(ostream&, const Rational&);

friend istream& operator>>(istream&, Rational&);


private:

//Data Members

int first, second;

};


Rational::Rational(int numerator, int denominator) {

int h = HCF(numerator, denominator);

this->first = numerator / h;

this->second = denominator / h;

}


Rational::Rational(int numerator) {

this->first = numerator;

this->second = 1;

}

Rational::Rational() {

this->first = 0;

this->second = 1;

}



Rational::~Rational() {}


Rational Rational::operator+(const Rational& A) {

Rational D;

D.first = (A.first)*(this->second) + (A.second)*(this->first);

D.second = (A.second)*(this->second);

int h = HCF(D.first, D.second);

D.first /= h;

D.first /= h;

return D;

}



Rational Rational::operator-(const Rational& A) {

Rational D;

D.first = (A.first)*(this->second) - (A.second)*(this->first);

D.second = (A.second)*(this->second);

int h = HCF(D.first, D.second);

D.first /= h;

D.first /= h;

return D;

}


Rational Rational::operator*(const Rational& A) {

Rational D;

D.first = (A.first)*(this->first);

D.second = (A.second)*(this->second);

int h = HCF(D.first, D.second);

D.first /= h;

D.first /= h;

return D;

}


Rational Rational::operator/(const Rational& A) {

Rational D;

D.first = (A.first)*(this->second);

D.second = (A.second)*(this->first);

int h = HCF(D.first, D.second);

D.first /= h;

D.first /= h;

return D;

}


bool Rational::operator==(const Rational& A) {

return((A.first)*(this->second)==(A.second)*(this->first));

}


bool Rational::operator<(const Rational& A) {

return((A.first)*(this->second)<(A.second)*(this->first));

}


bool Rational::operator<=(const Rational& A) {

return((A.first)*(this->second)<=(A.second)*(this->first));

}


bool Rational::operator>(const Rational& A) {

return((A.first)*(this->second)>(A.second)*(this->first));

}


bool Rational::operator>=(const Rational& A) {

return((A.first)*(this->second)>=(A.second)*(this->first));

}


ostream& operator<<(ostream& out, const Rational& A) {

out << A.first << "/" << A.second;

return out;

}


istream& operator>>(istream& in, Rational& A) {

in >> A.first >> A.second;

return in;

}


int HCF(int x, int y){

int i, m;

if(x>y)

m=y;

else

m=x;


for(i=m;i>=1;i--){

if(x%i==0 && y%i==0)

return i;

}

}


int main() {

Rational a(1,2);

Rational b(3);


cout << "Number were initiallized as: " << a << " and " << b << endl;


cout << "Enter numerator and denominator: ";

cin >> a;


int i = (a==b);


cout << a << " == " << b << " gives: " << i << endl;


cout << a << " + " << b << " = " << a + b << endl;

return 0;

}