RationalNumber class operator overloading

this is what it is asking...


the RationalNumber class should overload the stream insertion (<<) and stream extraction (>>) operators. The stream extraction operator should prevent a 0 denominator in a fraction, reduce fractions that are not in reduced form, and prevent negative denominators. Negative or 0 denominators should be set to 1.

Sample Output 1 (user input is bold):

Enter a Rational Number (n/d): 1/3
Enter a Rational Number (n/d): 2/4
1/3 + 1/2 = 5/6
1/3 - 1/2 = -1/6
1/3 * 1/2 = 1/6
1/3 / 1/2 = 2/3
1/3 <= 1/2 according to the overloaded > operator
1/3 < 1/2 according to the overloaded >= operator
1/3 < 1/2 according to the overloaded < operator
1/3 <= 1/2 according to the overloaded <= operator
1/3 != 1/2 according to the overloaded == operator
1/3 != 1/2 according to the overloaded != operator


Sample Output 2 (user input is bold):

Enter a Rational Number (n/d): 2/2
Enter a Rational Number (n/d): 1/0
Invalid denominator: 0
1/1 + 1/1 = 2/1
1/1 - 1/1 = 0/1
1/1 * 1/1 = 1/1
1/1 / 1/1 = 1/1
1/1 <= 1/1 according to the overloaded > operator
1/1 >= 1/1 according to the overloaded >= operator
1/1 >= 1/1 according to the overloaded < operator
1/1 <= 1/1 according to the overloaded <= operator
1/1 == 1/1 according to the overloaded == operator
1/1 == 1/1 according to the overloaded != operator


Driver Program:

#include "RationalNumber.h"

int main()
{
// RationalNumber c( 1, 3 ), d( 2, 4 ), x;
RationalNumber c, d, x;

// test overloaded stream extraction operator
cout << "Enter a Rational Number (n/d): ";
cin >> c;
cout << "Enter a Rational Number (n/d): ";
cin >> d;

x = c + d; // test overloaded operators + and =
cout << c << " + " << d << " = " << x << endl;

x = c - d; // test overloaded operators - and =
cout << c << " - " << d << " = " << x << endl;

x = c * d; // test overloaded operators * and =
cout << c << " * " << d << " = " << x << endl;

x = c / d; // test overloaded operators / and =
cout << c << " / " << d << " = " << x << endl;

// test overloaded > operator
cout << c << ( ( c > d ) ? " > " : " <= " ) << d
<< " according to the overloaded > operator\n";

// test overloaded >= operator
cout << c << ( ( c >= d ) ? " >= " : " < " ) << d
<< " according to the overloaded >= operator\n";

// test overloaded < operator
cout << c << ( ( c < d ) ? " < " : " >= " ) << d
<< " according to the overloaded < operator\n";

// test overloaded <= operator
cout << c << ( ( c <= d ) ? " <= " : " > " ) << d
<< " according to the overloaded <= operator\n";

// test overloaded == operator
cout << c << ( ( c == d ) ? " == " : " != " ) << d
<< " according to the overloaded == operator\n";

// test overloaded != operator
cout << c << ( ( c != d ) ? " != " : " == " ) << d
<< " according to the overloaded != operator\n";

return 0;
} // end main

this is what i have...


#include <iostream>
using namespace std;
#ifndef Rational
class Rational {
public:
Rational( int = 0, int = 1 ); // default constructor
Rational addition( const Rational & );
Rational subtraction( const Rational & );
Rational multiplication( const Rational & );
Rational division( Rational & );
void printRational( void );
void printRationalAsDouble( void );
private:
int numerator;
int denominator;
void reduction( void ); // utility function
};
#endif

Rational::Rational( int n, int d )
{
numerator = n;
denominator = d;
}
Rational Rational::addition( const Rational &a )
{
Rational t;
t.numerator = a.numerator * denominator;
t.numerator += a.denominator * numerator;
t.denominator = a.denominator * denominator;
t.reduction();
return t;
}
Rational Rational::subtraction( const Rational &s )
{
Rational t;
t.numerator = s.denominator * numerator;
t.numerator -= denominator * s.numerator;
t.denominator = s.denominator * denominator;
t.reduction();
return t;
}
Rational Rational::multiplication( const Rational &m )
{
Rational t;
t.numerator = m.numerator * numerator;
t.denominator = m.denominator * denominator;
t.reduction();
return t;
}
Rational Rational::division( Rational &v )
{
Rational t;
t.numerator = v.denominator * numerator;
t.denominator = denominator * v.numerator;
t.reduction();
return t;
}
void Rational::printRational( void )
{
if ( denominator == 0 )
cout << "\nDIVIDE BY ZERO ERROR!!!" << '\n';
else if ( numerator == 0 )
cout << 0;
else
cout << numerator << '/' << denominator;
}
void Rational::printRationalAsDouble( void )
{ cout << ( double)( numerator ) / denominator; }
void Rational::reduction( void )
{
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;
}
}

plus the driver program. please help.
Last edited on
You can do the following:

1
2
3
4
class MyClass {
    bool operator<(const MyClass &r);
    // etc... this works for every operator on the following link  
}


http://msdn.microsoft.com/en-us/library/5tk49fh2(VS.80).aspx
Topic archived. No new replies allowed.