0

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.

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Schol-R-LEA
0

The specific problem you mention comes from a missing closing brace in the RationalNumber() c'tor.

However, it is not by any means the only problem in the code. For example, the assignment operator should be something like this:

RationalNumber& RationalNumber::operator= (const RationalNumber &right)
{
    numerator = right.numerator;
    denominator = right.denominator;
    return *this;
}

For that matter, there's a logical error in the c'tor itself, which should be more like:

RationalNumber::RationalNumber(int num,int den)
{
    if(den == 0)
    {
        den = 1;
    }
    else if (den < 0)
    {
        denominator = -den;      // use the absolute of the denominator
        numerator = -num;        // and the negative of the numerator
    }
    else
    {
        numerator = num;
        denominator = den;
    }
}

Edited by Schol-R-LEA: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.