Hi this is my first time at this site and i am trying to create a program that uses operator overloading on rational numbers entered by the user.
here is my code.
the compiler keeps giving me errors with numerator, and denominator
mainly because the numerator and denominator are pointers in the header
and i have to have them that way.
Rational::Rational( int numerator , int denominator )
{
*numerator = n ; // sets numerator
if(d == 0) d = 1; // If denominator is zero then set to one.
*denominator = d; // sets denominator
reduction(); // store the fraction in reduced form
} // end Rational constructor
also i have trouble with the operators being setup right
Rational Rational::operator/( const Rational &v )
{
Rational t; // creates Rational object
t.numerator = v.denominator * numerator;
t.denominator = denominator * v.numerator;
t.reduction(); // store the fraction in reduced form
return t;
} // end function division
Rational::operatordouble()
{
returnstatic_cast< double >( numerator ) / denominator;
} // end function printRationalAsDouble
bool Rational::operator == (const Rational &rhs) // is equal?
{
double d_lhs = numerator / static_cast<double>(denominator);
double d_rhs = rhs.numerator / static_cast<double>(denominator);
return (d_lhs == d_rhs);
There're many wrong spots I saw only scan for secs.
Just like, i.e.
1 2 3 4 5 6 7 8
int *numerator;//Holds the numerator of the rational number//
int *denominator;//Holds the denominator of the rational number //
Rational::Rational( int numerator , int denominator ){
*numerator = n ; // sets numerator
if(d == 0) d = 1; // If denominator is zero then set to one.
*denominator = d; // sets denominator
reduction(); // store the fraction in reduced form
}
Where do n and d come from?
And you get param as int numerator and you have int *numerator; defined and then use *numerator = n ; ?
Non-sense at all.
1 2 3 4 5 6 7 8
int numerator;//Holds the numerator of the rational number//
int denominator;//Holds the denominator of the rational number //
Rational::Rational( int n, int d){
numerator = n ; // sets numerator
if(d == 0) d = 1; // If denominator is zero then set to one.
denominator = d; // sets denominator
reduction(); // store the fraction in reduced form
}
makes more sense?
I think you have to fix all of these non-sense syntax first and come ask again.