I am writing a rational number class and need to know if this is the correct way to do it. Can someone please let me know if I'm on track or not? Thanks.

class RationalNumber
{
public:
    RationalNumber( int = 0, int = 1); // constructor
    RationalNumber operator+( const RationalNumber& ) const; // addition
    RationalNumber operator-( const RationalNumber& ) const; // subtraction
    RationalNumber operator*( const RationalNumber& ) const; // multiplication
    RationalNumber operator/( const RationalNumber& ) const; // division

    // relational operators
    bool operator>( const RationalNumber& ) const; // greater than
    bool operator<( const RationalNumber& ) const; // less than
    bool operator>=( const RationalNumber&, ) const; // greater than or equal to
    bool operator<=( const RationalNumber&, ) const; // less than or equal to

    // equality operators
    bool operator==( const RationalNumber & ) const; // equality operator
    // inequality operator    
    bool operator!=( const RationalNumber &right ) const
    {
    return ! ( *this == right );
    } // end function != 

    void printRational() const; // display rational number
    
	private:
    int numerator; // private variable numerator
    int denominator; // private variable denominator
    void reduction(); // function for fraction reduction
    }; // end class RationalNumber

    RationalNumber::RationalNumber( int n, int d )
    {
        denominator = ( d > 0 ? d : 1 ); validate that denominator to insure it is positive 
	    numerator = n;
	reduction();
	}
	RationalNumber RationalNumber::operator+( const RationalNumber &x ) const
	{
	    return RationalNumber( numerator * x.denominator + denominator * x.denominator, 
		denominator * x.denominator );
	}

Recommended Answers

All 13 Replies

First thing that you need to do is write a driver program that has a main function in order to test it. Test the constructors and printRational. If you haven't written printRational, write it because you'll need it a lot when testing.

// driver program 
// includes

int main ()
{
    RationalNumber a();
    RationalNumber b (3, 4);
    a.printRational ();
    b.printRational ();
    return 0;
}

See if it compiles/runs/gives good results. If not, debug. If yes, you're on your way and you can write some of the operators.

First thing that you need to do is write a driver program that has a main function in order to test it. Test the constructors and printRational. If you haven't written printRational, write it because you'll need it a lot when testing.

// driver program 
// includes

int main ()
{
    RationalNumber a();
    RationalNumber b (3, 4);
    a.printRational ();
    b.printRational ();
    return 0;
}

See if it compiles/runs/gives good results. If not, debug. If yes, you're on your way and you can write some of the operators.

I tested and compiled but I'm getting the wrong results. I only get the operators and not the fractions. I think that my functions are incorrect.

int main()
{
    RationalNumber c(7, 3 ), d( 1, 9 ), x;

    c.printRational();
    cout << " + ";
    d.printRational();
    cout << " = ";
    x = c + d;
    x.printRational(); // test overload operators + and =
    system("pause");
} // end main

First thing that you need to do is write a driver program that has a main function in order to test it. Test the constructors and printRational. If you haven't written printRational, write it because you'll need it a lot when testing.

// driver program 
// includes

int main ()
{
    RationalNumber a();
    RationalNumber b (3, 4);
    a.printRational ();
    b.printRational ();
    return 0;
}

See if it compiles/runs/gives good results. If not, debug. If yes, you're on your way and you can write some of the operators.

Minor error on the red highlighted part. The compiler will see that
as a function. So to fix that we remove the parenthesis. Making it look like this :

RationalNumber a;
commented: Good catch. +11

The code does comple but with no fractions. I need to redo the overload function. Any suggestions?

Minor error on the red highlighted part. The compiler will see that
as a function. So to fix that we remove the parenthesis. Making it look like this :

RationalNumber a;

firstPerson, you are correct. Good catch. +rep

The code does comple but with no fractions. I need to redo the overload function. Any suggestions?

You need to do first things first, I think, and that means don't work on the operators at all until the simple driver program I posted works flawlessly. Do they? If not, fix that part before working on the + and = operators.

If my simple program works, but it breaks when you add your more complicated part (+ and = operators), then that zeroes in on the problem. Get the = operator right first, then go for the + operator.

It's unclear to me where exactly it breaks down and what the results are. Do the c and d objects display correctly? If so, break this line into more than one test:

x = c + d;
x = c;
x.printRational ();
c.printRational ();
x = d;
x.printRational ();
d.printRational ();
x = c + d;
x.printRational ();
c.printRational ();
d.printRational ();

It's fairly tedious, but it will help you get a feel for where exactly things go wrong (with the = operator or with the + operator).

Make sure = works first, then tackle +. Also consider the possibility that these all work, but printRational has a problem.

Post the updated code and some output too please.

That advise really helped; I narrowed the problem down to the operator function. It's not taking the numerator and denominator; only takes one integer, not two. I'm not sure how to write the overloaded operator +

RationalNumber RationalNumber::operator+( const RationalNumber &x ) const

That advise really helped; I narrowed the problem down to the operator function. It's not taking the numerator and denominator; only takes one integer, not two. I'm not sure how to write the overloaded operator +

RationalNumber RationalNumber::operator+( const RationalNumber &x ) const

It seems to be an incorrect operator + function; not sure how to write it.

output is: 7/1 + 8/1 = 8/1

#include <cstdlib>
#include <iostream>
using namespace std;

class RationalNumber
{
    public:
    RationalNumber( int = 0, int = 1); // constructor
    RationalNumber operator+( const RationalNumber& ) const; // addition
    RationalNumber operator-( const RationalNumber& ) const; // subtraction
    RationalNumber operator*( const RationalNumber& ) const; // multiplication
    RationalNumber operator/( const RationalNumber& ) const; // division

    // relational operators
    bool operator>( const RationalNumber& ) const; // greater than
    bool operator<( const RationalNumber& ) const; // less than
    bool operator>=( const RationalNumber& ) const; // greater than or equal to
    bool operator<=( const RationalNumber& ) const; // less than or equal to

    // equality operators
    bool operator==( const RationalNumber & ) const; // equality operator
    // inequality operator    
    bool operator!=( const RationalNumber &right ) const
    {
    return ! ( *this == right );
    } // end function != 

    void printRational() const; // display rational number

    private:
    int numerator; // private variable numerator
    int denominator; // private variable denominator
    void reduction(); // function for fraction reduction
    }; // end class RationalNumber

    RationalNumber::RationalNumber( int n, int d )
    {
        denominator = ( d > 0 ? d:1 );  
        numerator = n;

    }
    RationalNumber RationalNumber::operator+( const RationalNumber &x ) const
    {
        return x;
    }
    void RationalNumber::printRational() const
    {
        if ( numerator == 0 ) // print fraction as zero
            cout << numerator;
        else if ( denominator == 1 ) // print fraction as integer
            cout << numerator << '/' << denominator;
    } // end function printRational 
void RationalNumber::reduction()
{
    int largest, gcd = 1; // greatest common divisor;

    largest = ( numerator > denominator ) ? numerator: denominator;

    for ( int loop = 2; loop <= largest; loop++ )
        if ( numerator % loop == 0 && denominator % loop == 0 )
           gcd = loop;
    numerator /= gcd;
    denominator /= gcd;
} // end function reduction

int main()
{
    RationalNumber c( 7 ), d( 8 ), x;


    c.printRational();
    cout << " + ";
    d.printRational();
    cout << " = ";
    x = c + d;
    x.printRational(); // test overload operators + and =
    cout<<endl;
    system("pause");
} // end main

A little off on the code tags. It is this:

[code]

// code here

[/code]

not

[/code] // code here

[/code]

Hit the edit button and remove the slash (/) from the starting code tag, then hit preview. You should see keyword-highlighted code like mine. When it looks correct, submit the edited post. You have 30 minutes to edit your post before it is locked.

Think about how you add rational numbers in real life. Get the math down, then turn the math into C++.

a / b + c / d =  e / f

You are given a, b, c, d. You need to find e and f. How do you do it? You need to find the lcm of b and d and covert your fractions to have that denominator. Make f that lcm. Now add the new numerators and you get e. Finally, if e / f is reducible, reduce it. You already have a function to do that, I think.

So write an lcm helper function and use it in your + and - operators. turn the above process into C++ code and you have your + operator. A slight change turns it into a - operator.

It seems to be an incorrect operator + function; not sure how to write it.

output is: 7/1 + 8/1 = 8/1

#include <cstdlib>
#include <iostream>
using namespace std;

class RationalNumber
{
    public:
    RationalNumber( int = 0, int = 1); // constructor
    RationalNumber operator+( const RationalNumber& ) const; // addition
    RationalNumber operator-( const RationalNumber& ) const; // subtraction
    RationalNumber operator*( const RationalNumber& ) const; // multiplication
    RationalNumber operator/( const RationalNumber& ) const; // division

    // relational operators
    bool operator>( const RationalNumber& ) const; // greater than
    bool operator<( const RationalNumber& ) const; // less than
    bool operator>=( const RationalNumber& ) const; // greater than or equal to
    bool operator<=( const RationalNumber& ) const; // less than or equal to

    // equality operators
    bool operator==( const RationalNumber & ) const; // equality operator
    // inequality operator    
    bool operator!=( const RationalNumber &right ) const
    {
    return ! ( *this == right );
    } // end function != 

    void printRational() const; // display rational number

    private:
    int numerator; // private variable numerator
    int denominator; // private variable denominator
    void reduction(); // function for fraction reduction
    }; // end class RationalNumber

    RationalNumber::RationalNumber( int n, int d )
    {
        denominator = ( d > 0 ? d:1 );  
        numerator = n;

    }
    RationalNumber RationalNumber::operator+( const RationalNumber &x ) const
    {
        return x;
    }
    void RationalNumber::printRational() const
    {
        if ( numerator == 0 ) // print fraction as zero
            cout << numerator;
        else if ( denominator == 1 ) // print fraction as integer
            cout << numerator << '/' << denominator;
    } // end function printRational 
void RationalNumber::reduction()
{
    int largest, gcd = 1; // greatest common divisor;

    largest = ( numerator > denominator ) ? numerator: denominator;

    for ( int loop = 2; loop <= largest; loop++ )
        if ( numerator % loop == 0 && denominator % loop == 0 )
           gcd = loop;
    numerator /= gcd;
    denominator /= gcd;
} // end function reduction

int main()
{
    RationalNumber c( 7 ), d( 8 ), x;


    c.printRational();
    cout << " + ";
    d.printRational();
    cout << " = ";
    x = c + d;
    x.printRational(); // test overload operators + and =
    cout<<endl;
    system("pause");
} // end main

end quote.

I think that I figured it out, for the most part. Now I have to add the relational and equality operators; that will be another challenge. Thanks a lot for making me think through this.

class RationalNumber
{
    public:
    RationalNumber( int = 0, int = 1); // constructor
    RationalNumber operator+( const RationalNumber& ) const; // addition
    RationalNumber operator-( const RationalNumber& ) const; // subtraction
    RationalNumber operator*( const RationalNumber& ) const; // multiplication
    RationalNumber operator/( const RationalNumber& ) const; // division

    // relational operators
    bool operator>( const RationalNumber& ) const; // greater than
    bool operator<( const RationalNumber& ) const; // less than
    bool operator>=( const RationalNumber& ) const; // greater than or equal to
    bool operator<=( const RationalNumber& ) const; // less than or equal to

    // equality operators
    bool operator==( const RationalNumber & ) const; // equality operator
    // inequality operator    
    bool operator!=( const RationalNumber &right ) const
    {
    return ! ( *this == right );
    } // end function != 

    void printRational() const; // display rational number

    private:
    int numerator; // private variable numerator
    int denominator; // private variable denominator
    void reduction(); // function for fraction reduction
    }; // end class RationalNumber

    RationalNumber::RationalNumber( int n, int d )
    {
        denominator = ( d > 0 ? d : 1 );
        numerator = n;
        reduction();
    }
    RationalNumber RationalNumber::operator+( const RationalNumber &x ) const
    {
        return RationalNumber(numerator * x.denominator + denominator * x.numerator,
            denominator*x.denominator);
    }
    RationalNumber RationalNumber::operator-( const RationalNumber &x ) const
    {
        return RationalNumber(numerator * x.denominator - denominator * x.numerator,
            denominator*x.denominator);
    }
    RationalNumber RationalNumber::operator*( const RationalNumber &x ) const
    {
        return RationalNumber(numerator * x.denominator * denominator * x.numerator,
            denominator*x.denominator);
    }
    RationalNumber RationalNumber::operator/( const RationalNumber &x ) const
    {
        return RationalNumber(numerator * x.denominator / denominator * x.numerator,
            denominator*x.denominator);
    }
    void RationalNumber::printRational() const
    {
        if ( numerator == 0 ) // print fraction as zero
            cout << numerator;
        else if ( denominator == 1 ) // print fraction as integer
            cout << numerator;
        else
        cout << numerator << '/' << denominator;
    } // end function printRational 
void RationalNumber::reduction()
{
    int largest, gcd = 1; // greatest common divisor;

    largest = ( numerator > denominator ) ? numerator: denominator;

    for ( int loop = 2; loop <= largest; loop++ )
        if ( numerator % loop == 0 && denominator % loop == 0 )
           gcd = loop;
    numerator /= gcd;
    denominator /= gcd;
} // end function reduction

int main()
{
    RationalNumber c( 7, 3 ), d( 3, 9 ), x;


    c.printRational();
    cout << " + ";
    d.printRational();
    cout << " = ";
    x = c + d;
    x.printRational(); // test overload operators + and =
    cout<<endl;

    cout << '\n';
    c.printRational();
    cout << " - ";
    d.printRational();
    cout << " = ";
    x = c - d;
    x.printRational(); // test overload operators + and =
    cout<<endl;

    cout << '\n';
    c.printRational();
    cout << " * ";
    d.printRational();
    cout << " = ";
    x = c * d;
    x.printRational(); // test overload operators + and =
    cout<<endl;

    cout << '\n';
    c.printRational();
    cout << " / ";
    d.printRational();
    cout << " = ";
    x = c / d;
    x.printRational(); // test overload operators + and =
    cout<<endl;
    system("pause");
} // end main

I think that I figured it out, for the most part. Now I have to add the relational and equality operators; that will be another challenge. Thanks a lot for making me think through this.

class RationalNumber
{
    public:
    RationalNumber( int = 0, int = 1); // constructor
    RationalNumber operator+( const RationalNumber& ) const; // addition
    RationalNumber operator-( const RationalNumber& ) const; // subtraction
    RationalNumber operator*( const RationalNumber& ) const; // multiplication
    RationalNumber operator/( const RationalNumber& ) const; // division

    // relational operators
    bool operator>( const RationalNumber& ) const; // greater than
    bool operator<( const RationalNumber& ) const; // less than
    bool operator>=( const RationalNumber& ) const; // greater than or equal to
    bool operator<=( const RationalNumber& ) const; // less than or equal to

    // equality operators
    bool operator==( const RationalNumber & ) const; // equality operator
    // inequality operator    
    bool operator!=( const RationalNumber &right ) const
    {
    return ! ( *this == right );
    } // end function != 

    void printRational() const; // display rational number
    
	private:
    int numerator; // private variable numerator
    int denominator; // private variable denominator
    void reduction(); // function for fraction reduction
    }; // end class RationalNumber

    RationalNumber::RationalNumber( int n, int d )
    {
		denominator = ( d > 0 ? d : 1 );
		numerator = n;
		reduction();
	}
	RationalNumber RationalNumber::operator+( const RationalNumber &x ) const
	{
		return RationalNumber(numerator * x.denominator + denominator * x.numerator,
			denominator*x.denominator);
	}
    RationalNumber RationalNumber::operator-( const RationalNumber &x ) const
	{
		return RationalNumber(numerator * x.denominator - denominator * x.numerator,
			denominator*x.denominator);
	}
	RationalNumber RationalNumber::operator*( const RationalNumber &x ) const
	{
		return RationalNumber(numerator * x.denominator * denominator * x.numerator,
			denominator*x.denominator);
	}
	RationalNumber RationalNumber::operator/( const RationalNumber &x ) const
	{
		return RationalNumber(numerator * x.denominator / denominator * x.numerator,
			denominator*x.denominator);
	}
	void RationalNumber::printRational() const
	{
	    if ( numerator == 0 ) // print fraction as zero
		    cout << numerator;
		else if ( denominator == 1 ) // print fraction as integer
            cout << numerator;
		else
		cout << numerator << '/' << denominator;
    } // end function printRational	
void RationalNumber::reduction()
{
    int largest, gcd = 1; // greatest common divisor;

    largest = ( numerator > denominator ) ? numerator: denominator;

    for ( int loop = 2; loop <= largest; loop++ )
        if ( numerator % loop == 0 && denominator % loop == 0 )
           gcd = loop;
    numerator /= gcd;
    denominator /= gcd;
} // end function reduction

int main()
{
    RationalNumber c( 7, 3 ), d( 3, 9 ), x;
	

    c.printRational();
    cout << " + ";
    d.printRational();
    cout << " = ";
    x = c + d;
    x.printRational(); // test overload operators + and =
	cout<<endl;
	
	cout << '\n';
    c.printRational();
    cout << " - ";
    d.printRational();
    cout << " = ";
    x = c - d;
    x.printRational(); // test overload operators + and =
	cout<<endl;

	cout << '\n';
    c.printRational();
    cout << " * ";
    d.printRational();
    cout << " = ";
    x = c * d;
    x.printRational(); // test overload operators + and =
	cout<<endl;

	cout << '\n';
    c.printRational();
    cout << " / ";
    d.printRational();
    cout << " = ";
    x = c / d;
    x.printRational(); // test overload operators + and =
	cout<<endl;
	system("pause");
} // end main

I thought I was done; ran into another glitch with relational operators. syntax error. I could use some help on this function. Thanks.

bool RationalNumber::operator >( const RationalNumber &x ) const
	{
		if RationalNumber(numerator* x.denominator > denominator* x.numerator,
			denominator*x.denominator)
			return true;

			return false;
	}

I thought I was done; ran into another glitch with relational operators. syntax error. I could use some help on this function. Thanks.

bool RationalNumber::operator >( const RationalNumber &x ) const
	{
		if RationalNumber(numerator* x.denominator > denominator* x.numerator,
			denominator*x.denominator)
			return true;

			return false;
	}

I don't see a need to call a constructor in this function. Just compare the two rational numbers.

double val1 = ((double) numerator) / ((double) denominator);
double val2 = ((double) x.numerator) / ((double) x.denominator);
return (val1 > val2);

I have everything except for the inequility operator !=; I'm getting an error message "const already has a body". This is the last test to run the code, my brain is smoking... I don't think that this function is written correctly.

bool RationalNumber::operator!=( RationalNumber const& rightSide ) const
	{
		return (( numerator*rightSide.denominator) != (denominator*rightSide.numerator));
	}

Finally done. If anyone needs help with this code, let me know, I will be glad to help. Thanks Vernon for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.