I have an assignment that calls for me to make a class called RationalNumber with the following capabilities:
a)Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators.
b)Overload the addition,subtraction,multiplication and division operators for this class.
c)Overload the relational and equality operators for this class.
Here is my header:
#ifndef RationalNumber_H //
#define RationalNumber_H
#include <iostream>
using namespace std;
class RationalNumber
{
friend ostream &operator<<(ostream &, const RationalNumber &);
friend istream &operator>>(istream &, RationalNumber &);
public:
RationalNumber(int= 0,int =0);
const RationalNumber &operator=(const RationalNumber &);
RationalNumber &operator+(const RationalNumber &);
RationalNumber &operator-(const RationalNumber &);
RationalNumber &operator/(const RationalNumber &);
RationalNumber &operator*(const RationalNumber &);
bool operator>(const RationalNumber &);
bool operator>=(const RationalNumber &);
bool operator<(const RationalNumber &);
bool operator<=(const RationalNumber &);
bool operator==(const RationalNumber &);
bool operator !=(const RationalNumber &right);
private:
int numerator;
int denominator;
int mixed;
float decimalAnswer;
};
#endif
Here is my .cpp file:
#include <iostream>
#include <iomanip>
#include "RationalNumber.h"
using namespace std;
RationalNumber::RationalNumber(int num,int den)
{
numerator = num;
if(den <=0)
{
den =1;
}
else denominator = den;
RationalNumber RationalNumber::operator= (const RationalNumber &right)
{
RationalNumber = right.RationalNumber; return *this;
}
RationalNumber RationalNumber::operator+ (const RationalNumber &right)
{
return Rational(numerator * right.denominator + denominator * right.denominator,
denominator * right.denominator);
}
RationalNumber RationalNumber::operator-(const RationalNumber &right)
{
return Rational(numerator * right.denominator - denominator * right.denominator,
denominator * right.denominator);
}
RationalNumber RationalNumber::operator*(const RationalNumber &right)
{
return Rational(numerator * right.numerator, denominator * right.denominator);
}
RationalNumber RationalNumber::operator/(const RationalNumber &right)
{
return Rational(numerator * x.denominator, denominator * x.numerator);
}
bool RationalNumber::operator>(const RationalNumber &right) const
{
bool r = false;
if(RationalNumber > RationalNumber.right) r=true;
return r;
}
bool RationalNumber::operator>=(const RationalNumber &right) const
{
bool r = false;
if(RationalNumber >= RationalNumber.right) r=true;
return r;
}
bool RationalNumber::operator<(const RationalNumber &right) const
{
bool r = false;
if(RationalNumber < RationalNumber.right) r=true;
return r;
}
bool RationalNumber::operator<=(const RationalNumber &right) const
{
bool r = false;
if(RationalNumber <= RationalNumber.right) r=true;
return r;
}
bool RationalNumber::operator==(const RationalNumber &)
{
bool r = false;
if(RationalNumber == RationalNumber.right) r=true;
return r;
}
bool RationalNumber::operator!= (const RationalNumber &right)
{
return !(*this == right);
}
}
}
This is my tester supplied by the instructor:
#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
I keep getting compile errors saying that my function definitions are not allowed where my braces are located. Any help would be appreciated. I feel like I almost have the problem solved. I just need to clean up a few things.