Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

c++ operator overloading problem (10 points)?

Having some logic errors here and there.

some of the logical operators are not working properly and the constructor is suppose to let me enter 2/4 and I have to enter 2 enter 4, i guess i just dont undestand how to make a constructor accept 2/4 like that. Thanks!!

#ifndef RATIONAL_H

#define RATIONAL_H

#include <iostream>

using namespace std;

class Rational

{

public:

Rational(int numerator = 0,int denominator = 1);

Rational operator+(const Rational&);

Rational operator-(const Rational&);

Rational operator*(const Rational&);

Rational operator/(const Rational&);

void reduction();

Rational operator=(Rational object);

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

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

int operator==(Rational object);

int operator!=(Rational object);

int operator>(Rational object);

int operator<(Rational object);

int operator<=(Rational object);

int operator>=(Rational object);

private:

int numerator;

int denominator;

};

#endif

#include "Rational.h"

#include <iostream>

using namespace std;

int numerator;

int denominator;

Rational::Rational(int n,int d)

{

numerator = d < 0 ? -n : n;

denominator = d < 0 ? -d : d;

reduction();

}

Rational Rational::operator+(const Rational& a)

{

Rational t;

t.numerator = a.numerator * denominator + a.denominator * numerator;

t.denominator = a.denominator * denominator;

t.reduction();

return t;

}

Rational Rational::operator-(const Rational& s)

{

Rational t;

t.numerator = s.denominator * numerator - s.numerator * denominator ;

t.denominator = s.denominator * denominator;

t.reduction();

return t;

}

Rational Rational::operator*(const Rational& m)

{

Rational t;

t.numerator = m.numerator * numerator;

t.denominator = m.denominator * denominator;

t.reduction();

return t;

}

Rational Rational::operator/(const Rational& v)

{

Rational t;

t.numerator = v.denominator * numerator;

t.denominator = denominator * v.numerator;

t.reduction();

return t;

}

void Rational::reduction()

{

int largest;

largest = numerator > denominator ? numerator : denominator;

int gcd = 0; // greatest common divisor

for ( int loop = 2; loop <= largest; loop++ )

if ( numerator % loop == 0 && denominator % loop == 0 )

gcd = loop;

if (gcd != 0)

{

numerator /= gcd;

denominator /= gcd;

}

}

Rational Rational::operator=(Rational object)

{

numerator=object.numerator;

denominator=object.denominator;

return(*this);

}

ostream &operator<<(ostream &output,Rational &object)

{

output<<object.numerator<<"/"<<object.denominator<<endl;

return output;

}

istream &operator>>(istream &input,Rational &object)

{

input>>object.numerator;

input>>object.denominator;

return input;

}

int Rational::operator==(Rational object)

{

if(numerator==object.numerator && denominator==object.denominator)

return(1);

else

return(0);

}

int Rational::operator!=(Rational object)

{

if(numerator!=object.numerator || denominator!=object.denominator)

return(1);

else

return(0);

}

int Rational::operator>(Rational object)

{

return((numerator/denominator)>(object.numerator/object.denominator) ? 1:0);

}

int Rational::operator<(Rational object)

{

return((numerator/denominator)<(object.numerator/object.denominator) ? 1:0);

}

int Rational::operator>=(Rational object)

{

return (numerator * object.denominator) >= (object.numerator * denominator);

}

int Rational::operator<=(Rational object)

{

return (numerator * object.denominator) <= (object.denominator * numerator);

}

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    Some notes:

    - It is most helpful if you specifically describe where the problems are. For example, if you put in some input and got back unexpected output, you would tell us what that input/output was. So in your case you say "some of the logical operators are not working properly" -- well, *which* logical operators? That's like taking your car to a mechanic and saying "something's wrong" rather than describing "the engine makes a funny sound".

    - Yahoo truncates strings with too many non-breaking characters in a row, so for example we see a line "return((numerator/denominator)<(objec… ? 1:0);" near the end, where the ... indicates something Yahoo chopped off. I recommend using pastebin.com, that way we can see indentation as well.

    - A typical Rational class implementation would allow you to define a rational like this: "Rational my_fraction(2,4);".

    - Here is a clean way to calculate gcd:

    int gcd(int a, int b) {

        return (b == 0) ? a : gcd(b, a % b);

    }

Still have questions? Get your answers by asking now.