I am trying to create a rational numbers program from scratch (I'm a brand new student basically), and I have all my inputs working right now. My problem lies within my showFraction() function, but I can't figure out how to fix it. It should output the inputs from the user and appear as a fraction, but what's coming out is a large negative number (basically junk).

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

class Rational
{
  public:
	  void setNumerator1(int);
	  void setDenominator1(int);
	  void setNumerator2(int);
	  void setDenominator2(int);
	  void showFraction1(int&,int&);

private:
    int Numerator1;
    int Denominator1;
};

void main()
{
	Rational MyNumerator1, MyDenominator1, MyNumerator2, MyDenominator2;

	int Numerator1, Denominator1, Numerator2, Denominator2;

	Numerator1 = 0;
	Numerator2 = 0;
	Denominator1 = 0;
	Denominator2 = 0;

	MyNumerator1.setNumerator1(Numerator1);
	MyDenominator1.setDenominator1(Denominator1);
	MyNumerator2.setNumerator2(Numerator2);
	MyDenominator2.setDenominator2(Denominator2);

	MyNumerator1.showFraction1(Numerator1,Denominator1);
}

void Rational::setNumerator1(int Numerator1)
{
	cout << "Please Enter First Numerator: ";
    cin >> Numerator1;
    cout << endl;
}

void Rational::setDenominator1(int Denominator1)
{
	cout << "Please Enter First Denominator: ";
	cin >> Denominator1;
	cout << endl;
}


void Rational::setNumerator2(int Numerator2)
{
	cout << "Please Enter First Denominator: ";
	cin >> Numerator2;
	cout << endl;
}

void Rational::setDenominator2(int Denominator2)
{
	cout << "Please Enter First Denominator: ";
	cin >> Denominator2;
	cout << endl;
}

void Rational::showFraction1(int&,int&)
{
	cout << "First Fraction is: " << Numerator1 << "/" << Denominator1 << endl;
}

Here is the output that I'm getting:

Please Enter First Numerator: 1

Please Enter First Denominator: 4

Please Enter First Denominator: 5

Please Enter First Denominator: 2

First Fraction is: -858993460/-858993460
Press any key to continue . . .

Edited 6 Years Ago by PDB1982: n/a

Hint : check your set functions, specifically, check the name of the parameter.

He needs more than that, first. Have a closer look at the structure of the class and his main().

@OP:
Your output is "garbled" because you clearly don't understand how classes and objects work.
Read this function tutorial.
Then read this classes tutorial.
Hopefully those will help set you on the right path.

Read the linked pages, make some changes and re-post.

Edited 6 Years Ago by Fbody: n/a

Well, I figured out that I had spelled something incorrectly, so I corrected that. Here is my new code:

The outcomes are 0 since they are established as that in the beginning. However, if I take those out then it won't compile....not sure where to go from here...

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

class Rational
{
  public:
	  void setNumerator1(int);
	  void setDenominator1(int);
	  void setNumerator2(int);
	  void setDenominator2(int);
	  int showFraction1(int a,int b);

private:
    int Numerator1;
    int Denominator1;
};

void main()
{
	Rational MyNumerator1, MyDenominator1, MyNumerator2, MyDenominator2;

	int Numerator1, Numerator2, Denominator1, Denominator2;

	Numerator1 = 0;
	Numerator2 = 0;
	Denominator1 = 0;
	Denominator2 = 0;

	MyNumerator1.setNumerator1(Numerator1);
	MyDenominator1.setDenominator1(Denominator1);
	MyNumerator2.setNumerator2(Numerator2);
	MyDenominator2.setDenominator2(Denominator2);

	MyNumerator1.showFraction1(Numerator1,Numerator2);
}

void Rational::setNumerator1(int Numerator1)
{
	cout << "Please Enter First Numerator: ";
    cin >> Numerator1;
    cout << endl;
}

void Rational::setDenominator1(int Denominator1)
{
	cout << "Please Enter First Denominator: ";
	cin >> Denominator1;
	cout << endl;
}


void Rational::setNumerator2(int Numerator2)
{
	cout << "Please Enter First Denominator: ";
	cin >> Numerator2;
	cout << endl;
}

void Rational::setDenominator2(int Denominator2)
{
	cout << "Please Enter First Denominator: ";
	cin >> Denominator2;
	cout << endl;
}

int Rational::showFraction1(int Numerator1 ,int Denominator1)
{
	int a,b;

	a = Numerator1;
	b = Denominator1;

	cout << "a = " << a << " " << "B = " << b << endl;
	return a,b;
}

I believe the objective of the assignment is to have one instance of the rational class which contains both the numerator and denominator for the first number and another instance that contains both numerator and denominator for the second number.

As other posters have pointed out, your setter functions are potentially incorrect also. It is customary to have:

void Rational::setNumerator(int numerator)
{ //set the private variable by the value being passed in
     Numerator1 = numerator; //following the name of the private 
                                         //variable from your class
}

Edited 6 Years Ago by jonsca: n/a

I believe the objective of the assignment is to have one instance of the rational class which contains both the numerator and denominator for the first number and another instance that contains both numerator and denominator for the second number.

As other posters have pointed out, your setter functions are potentially incorrect also. It is customary to have:

void Rational::setNumerator(int numerator)
{ //set the private variable by the value being passed in
     Numerator1 = numerator; //following the name of the private 
                                         //variable from your class
}

What if I put it in like this?

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

class Rational
{
  public:
	  void setNumerator1();
	  void setDenominator1();
	  void setNumerator2();
	  void setDenominator2();
	  void showFraction1(int&,int&);

private:
    int Numerator1;
    int Denominator1;
	int Numerator2;
	int Denominator2;
};

void main()
{
	Rational MyNumerator1, MyDenominator1, MyNumerator2, MyDenominator2, Print;

	MyNumerator1.setNumerator1();
	MyDenominator1.setDenominator1();
	MyNumerator2.setNumerator2();
	MyDenominator2.setDenominator2();

	Print.showFraction1();

}

void Rational::setNumerator1()
{
	cout << "Please Enter First Numerator: ";
    cin >> Numerator1;
    cout << endl;


}

void Rational::setDenominator1()
{
	cout << "Please Enter First Denominator: ";
	cin >> Denominator1;
	cout << endl;
}


void Rational::setNumerator2()
{
	cout << "Please Enter First Denominator: ";
	cin >> Numerator2;
	cout << endl;
}

void Rational::setDenominator2()
{
	cout << "Please Enter First Denominator: ";
	cin >> Denominator2;
	cout << endl;
}

void Rational::showFraction1(int &Numerator1, int &Denominator1)
{
	int a,b;

	a = Numerator1;
	b = Denominator1;

	cout << "a = " << a << " " << "B = " << b << endl;
}

You do not need to make different functions for each object. Also your print function is wrong
Read up the concepts of objects one more time and you will see that you just need one getter / setter methods per class

PS: Change your void main to int main

Edited 6 Years Ago by abhimanipal: n/a

You do not need to make different functions for each object. Also your print function is wrong
Read up the concepts of objects one more time and you will see that you just need one getter / setter methods per class

PS: Change your void main to int main

Unfortunately, our teacher has "suggested" that we use the void main() for this assignment, so I can't stray from that.

How do I correct the output errors though?

You're still missing out on how the objects should be used. Your object contains a numerator and denominator and a way to print itself. Therefore you should have

Rational myrationalnumber;
myrationalnumber.setNumerator(2);
myrationalnumber.setDenominator(5);
myrationalnumber.showfraction();

which would print out 2/5
Then you can make another object for the second rational number.

Your class defines what the object has for members and how it behaves. Each object carries around its own numerator and its denominator. It knows how to print itself, therefore you don't need to pass it anything to print as it has access to its own member variables.

You're still missing out on how the objects should be used. Your object contains a numerator and denominator and a way to print itself. Therefore you should have

Rational myrationalnumber;
myrationalnumber.setNumerator(2);
myrationalnumber.setDenominator(5);
myrationalnumber.showfraction();

which would print out 2/5
Then you can make another object for the second rational number.

Your class defines what the object has for members and how it behaves. Each object carries around its own numerator and its denominator. It knows how to print itself, therefore you don't need to pass it anything to print as it has access to its own member variables.

I understand what you are saying, but your example has programmer defined numbers. I was hoping to get all user input for the numerators and denominators. I'm relatively knew to this, but isn't there a way to have user define a numerator, say "Numerator1" from the previous example(s), and then carry that number into all other functions. Would that be a form of overloading?

Couldn't we say:

class Rational
{
public:
     Void NumberNumerator1 (int&);

private:
   int Numerator1;
}
void main()
{
    Rational First;

    First.NumberNumerator1(Number);
}

void Rational::NumberNumerator1 (int &Numerator1)
{
     cout << "Please Enter First Numerator: " << endl;
     cin >> Numerator1;
     cout << endl;
}
}

That way we establish Numerator1 through user input and then keep it so that we can transfer it to another class function like:

void FractionOutput(int &Numerator1)
{
     cout << "Numerator1 = " << Numerator1 << endl;
}

You're not listening. If you want help you need to pay attention to what people are telling you. The answer is staring you in the face and you are completely ignoring it.

void Rational::NumberNumerator1 (int &Numerator1)
{
     cout << "Please Enter First Numerator: " << endl;
     cin >> Numerator1;
     cout << endl;
}

Is completely incorrect. Not only is your parameter variable hiding your member variable so you can't use it (because it has an identical name), you're effectively sending the input value back to main() instead of storing it in the class' member variable.
The proper way to do it is:

void Rational::setNumerator(int numerator)
{ //set the private variable by the value being passed in
     Numerator1 = numerator; //following the name of the private 
                                         //variable from your class
}

just like jonsca said in post #5. You would then do your user input in main() and call setNumerator() according to jonsca's example in post #9.

A corrected version of your code follows:

class Rational
{
public:
     void setNumerator (int);
 
private:
   int myNumerator;
}
void main()
{
    Rational First;
    int newNumerator;

    cout << "Please Enter First Numerator: " << endl;
    cin >> newNumerator;
    cout << endl;

    First.setNumerator(newNumerator);
}
 
void Rational::setNumerator (int newNumerator)
{
    myNumerator = newNumerator;
}

Take that example and apply it to your code.

Also:

Unfortunately, our teacher has "suggested" that we use the void main() for this assignment, so I can't stray from that.
...

If that's true, your instructor shouldn't be teaching. Your main() must ALWAYS be defined as an int per the C++ Standards.

Edited 6 Years Ago by Fbody: n/a

Comments
yes

You can do something of this sort

class test
{
      int x;
public:
  
     void getNum();
      void printNum();
};

void test::getNum()
{
   // You do not have to pass any parameters to this function. This is a member function of the class hence it can access all the members of the class  
    cout<<"Enter x\n";
    cin>>x;
}

void test::printNum()
{
 // You do not have to pass any parameters to this function. This is a member function of the class hence it can access all the members of the class  
     cout<<x;
}

For each object the member function can access all the data members of the class

Conceptually, the structure is okay, but from a usability/clarity standpoint, that naming structure is not really advisable. A "get" function is usually used to retrieve the value of a member variable for processing purposes, not "set" it.

Edited 6 Years Ago by Fbody: n/a

I probably should not jump into a thread where so many people have ready jumped in but here goes...

You have a lot of posts but clearly there are somethings that have never quite grasped.

First what you have to understand is what a rational number is:
an integer is any whole number ie , 45, 678687, -23
and a rational number is one integer on top of another integer

ie 2/9 rather than the decimal 0.2222222

but if you set a float as 2/9 you would get the output to show 0.222

The idea is that you write a class to store the number

This is saying like, int, double, float you want to have something to store your number

in this instance your number say 3/7 needs to be stored in 2 parts
a numerator and a denominator
where here:
numerator would = 3
and
denominator = 7

so first as you cannot say
Rational r = 3/7;
as the compiler sees 3/7 differently from you
you have to tell the class how to load in the numbers

so you would have

r.numerator = 3;
r.denominator = 7;

and to display the number:

std::cout << r.numerator << "/" << r.denominator << std::endl;

but this would only work with numerator and denominator being public:

what is happening is each time you delcare a rational number a
new numerator and denominator are made for that number

Rational r1, r2;
r1.numerator = 1;
r1.denominator = 2;
r2.numerator = 3;
r2.denominator = 4;

here you now have 4 ints 2 for r1 and 2 for r2.

To access their unique variable you place a fullstop after their name
the computer keeps track of their variables via the usinque name r1.numerator normally you will see that numerator is private: this means that you cannot get the data directly this is useful later on
so you need a method set and get so

Rational r1;
r1.set_numerator(3);
//same as
// r1.numerator = 3;

r1.set_denominator(7);
//same as
// r1.denominator = 3;

int n =  r1.get_numerator();
//same as
//int n - r1.numerator;

finally when you want to get the user to input the data you should have another function

void ask_user_for_numerator();

now you can set the r1.numerator in the function as it is a member of r1

so

void Rational::ask_user_for_numerator()
{
cout << "please enter numerator:" ;
cin >> numerator; //numerator is the member of r1
cout << endl;
}

note numerator is only defined in the class
this is the same as

{
Rational r1;

//method 1 direct input
r1.set_numerator(3);
r1.set_denominator(7);
r1.display();

//method 2
int local_numerator(0);
cout << "enter a numerator:";
cin>> local_numerator;
cout << endl;
r1.set_numerator(numerator)
int local_denominator(0);
cout << "enter a denominator:";
cin>> local_denominator;
cout << endl;
r1.set_denominator(local_denominator);
r1.display();

//method 3
r1.ask_numerator();
r1.ask_denominator();
r1.display();

}

all three approaches leave the values of r1.numerator and denominator set
the display method I haven't shown

but the member variables are not known outside of the class

1 - understand why you need a class
2 - what member variables do you want for each item in the class
(i.e. a person has their own name and age that is unique to them this is the same for rational numbers having unique numerators and denominators)
3 - what functions/methods does your clsss need to do
(ie you might want to add a year to a persons age)
4 - test the class by instantiating it
(this is like int x = 7;, person man;)

etc.
this should give you enough to read more about classes but teh first thing with any code is know what you want the computer to do and why.

I understand what you are saying, but your example has programmer defined numbers. I was hoping to get all user input for the numerators and denominators. I'm relatively knew to this, but isn't there a way to have user define a numerator, say "Numerator1" from the previous example(s), and then carry that number into all other functions. Would that be a form of overloading?

Couldn't we say:

class Rational
{
public:
     Void NumberNumerator1 (int&);

private:
   int Numerator1;
}
void main()
{
    Rational First;

    First.NumberNumerator1(Number);
}

void Rational::NumberNumerator1 (int &Numerator1)
{
     cout << "Please Enter First Numerator: " << endl;
     cin >> Numerator1;
     cout << endl;
}
}

That way we establish Numerator1 through user input and then keep it so that we can transfer it to another class function like:

void FractionOutput(int &Numerator1)
{
     cout << "Numerator1 = " << Numerator1 << endl;
}

Edited 6 Years Ago by tetron: missed [/code] closing tag

Comments
Thanks Very Much! You actually took the time to explain the topic to me, which makes this the greatest help I've ever received on this site. Thanks again!
well done

That was THE BEST help I think I've ever received on this site! Thank You very much! You took the time to explain it, and I appreciate that more than you know. Thanks Again!

So, I finally got the main portion of this code working (thanks to everyone's help)....now (not surprisingly, I know...) I'm stuck again, trying to overload operators within the same program. I want to see if numerator1 is > than numerator2, but I don't know how to overload the operators properly to do so....

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

class Rational
{
  public:
	  void setNumerator1();
	  void setNumerator2();
	  void setDenominator1();
	  void setDenominator2();
	  void output1();
	  void output2();
	  int numerator1;
	  int denominator1;
	  int numerator2;
	  int denominator2;

	  bool operator >(Rational);
};

void main()
{
	Rational Fraction1, Fraction2, NumeratorTest;

	Fraction1.setNumerator1();
	Fraction1.setDenominator1();

	Fraction2.setNumerator2();
	Fraction2.setDenominator2();

	cout << endl;

	Fraction1.output1();
	Fraction2.output2();

}


void Rational::setNumerator1()
{
	cout << "Enter First Numerator: ";
	cin >> numerator1;
	cout << endl;
}

void Rational::setDenominator1()
{
	cout << "Enter First Denominator: ";
	cin >> denominator1;
	cout << endl;
}

void Rational::setNumerator2()
{
	cout << "Enter Second Numerator: ";
	cin >> numerator2;
	cout << endl;
}

void Rational::setDenominator2()
{
	cout << "Enter Second Denominator: ";
	cin >> denominator2;
	cout << endl;
}

void Rational::output1()
{
	cout << "First Fraction is: " << numerator1 << "/" << denominator1 << endl;
	cout << endl;
}

void Rational::output2()
{
	cout <<"Second Fraction is: " << numerator2 << "/" << denominator2 << endl;
}

bool operator >(Rational&, Rational&)
{
	return (numerator1 > numerataor2);
}

See, I think you are running into problems because you're supposed to have one rational number per object. Then if you are comparing two rational numbers it will actually mean something.

You have sort of the right idea but you want to compare the numerator and denominator of the object in question to another one (e.g., 1/2 or 1/5 which is bigger, by your definition neither is). Otherwise you can make a two parameter version be a friend function to your class but I'm not sure if that's in line with your objectives.

See, I think you are running into problems because you're supposed to have one rational number per object. Then if you are comparing two rational numbers it will actually mean something.

You have sort of the right idea but you want to compare the numerator and denominator of the object in question to another one (e.g., 1/2 or 1/5 which is bigger, by your definition neither is). Otherwise you can make a two parameter version be a friend function to your class but I'm not sure if that's in line with your objectives.

I was actually trying this out, and it seems to compile ok....I think I went the way of the friend functions, but it makes it kind of lengthy...

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

class Rational
{
  public:
	  void setNumerator1();
	  void setNumerator2();
	  void setDenominator1();
	  void setDenominator2();
	  void output1();
	  void output2();
	  void tests(Rational &Fraction1);
	  int numerator1;
	  int denominator1;
	  int numerator2;
	  int denominator2;

	  friend bool operator> (Rational &Fraction1, Rational &Fraction2);
	  friend bool operator< (Rational &Fraction1, Rational &Fraction2);
	  friend bool operator>= (Rational &Fraction1, Rational &Fraction2);
	  friend bool operator<= (Rational &Fraction1, Rational &Fraction2);

	  friend int operator* (Rational &Fraction1,Rational &Fraction2);
};


void main()
{
	Rational Fraction1, Fraction2, Test;

	Fraction1.setNumerator1();
	Fraction1.setDenominator1();

	Fraction2.setNumerator2();
	Fraction2.setDenominator2();

	cout << endl;

	Fraction1.output1();
	Fraction2.output2();

	cout << endl;

	Test.tests(Fraction1);
}


void Rational::setNumerator1()
{
	cout << "Enter First Numerator: ";
	cin >> numerator1;
	cout << endl;
}

void Rational::setDenominator1()
{
	cout << "Enter First Denominator: ";
	cin >> denominator1;
	cout << endl;
}

void Rational::setNumerator2()
{
	cout << "Enter Second Numerator: ";
	cin >> numerator2;
	cout << endl;
}

void Rational::setDenominator2()
{
	cout << "Enter Second Denominator: ";
	cin >> denominator2;
	cout << endl;
}

void Rational::output1()
{
	cout << "First Fraction is: " << numerator1 << "/" << denominator1 << endl;
	cout << endl;
}

void Rational::output2()
{
	cout <<"Second Fraction is: " << numerator2 << "/" << denominator2 << endl;
}

bool operator> (Rational &Fraction1, Rational &Fraction2)
{
	return  (Fraction1.numerator1 > Fraction2.numerator2);
}

bool operator< (Rational &Fraction1, Rational &Fraction2)
{
	return (Fraction1.numerator1 < Fraction2.numerator2);
}

bool operator>= (Rational &Fraction1, Rational &Fraction2)
{
    return (Fraction1.numerator1 >= Fraction2.numerator2);
}

bool operator<= (Rational &Fraction1, Rational &Fraction2)
{
    return (Fraction1.numerator1 <= Fraction2.numerator2);
}

int operator* (Rational &Fraction1, Rational &Fraction2)
{
	return (Fraction1.numerator1 * Fraction2.denominator2);
	
}

void Rational::tests(Rational &Fraction1)
{
	cout << Fraction1.denominator1 << "  " << Fraction1.numerator1;
}

It is nice to have friends but as a programmer you should not see friend s very often. It doesn't want to be here.

It is allowed to have a class that has two rational numbers in it but you should probably give it another name:

if we go back to what a single rational number is

numerator/denominator

say

2/9

then if we had another rational number

3/11

it makes sense to
say

if(2.0/9 > 3.0/11)
{
std::cout << "first is bigger" << std::endl;
}

I have had to add the .0 to stop the int making both numbers 0

now the computer knows how to compare float and double and you know how to convert a single rational number to a double

class Rational
{
public:
Rational(int num = 0, int denom = 1);
double convert_to_double();
bool operator>(const Rational &r) const;
private:
int numerator. denominator;
]

this is a simplified class
now here note that Rational only can be set in the constructor

double Rational:: convert()
{
 double ret(numerator); //change numerator to 
 if(denominator > 0)
 {
    ret /= denominator;
 } 
 else
 {
    std::cout << "error denominator should not be 0!" << std::endl;
    ret = 0.0;
  }
return ret;
}

in main we want

// r1 = 2/9
Rational r1(2, 9);
//r2 = 3/11
Rational r2(3, 11);
if(r1 > r2)
{
  std::cout << "r1 is bigger than r2" << std::endl;
}

double d1 = r1.convert_to_double();
double d2 = r2.convert_to_double();

if(d2 < d1)
{
std::cout << "d2 is less than d1" << std::endl;
}

now you need to fill the following

bool Rational::operator > (const Rational & r) const)
{
 bool ret(false);
//write me
return ret;
}

now if you want to store two rationals in the same class which is not brilliant idea dont overload > because it should only have one parameter write a function called

bool is_1_greater_than2();

This is going to be a stupid question, but If I do use the

friend bool operator< (Rational &Fraction1, Rational &Fraction2);

What else do I do with it? It doesn't produce an output...so, can I use it within other functions. Say, If I wanted to use the following functions, would I use that overloaded operator to replace the red portions?

void Rational::tests(Rational &Fraction1, Rational &Fraction2)
{
	if ((Fraction1.numerator1 * Fraction2.denominator2) < (Fraction1.denominator2 * Fraction2.numerator2))
		cout << "Numbers are Rational!" << endl;
	else
		cout << "Numers are NOT Rational!" << endl;
}

The whole idea of the overloaded operator is you can now say something like if(Rational1 < Rational2) and it makes sense to the compiler because it has an overloaded operator to tell it how to perform the operation.

However, you have to implement it in your source file like you did on lines 90 to 98 above. Tetron's method of having one parameter member functions would work too but you seem to be more comfortable this way. You can then borrow the formula that you have posted on your latest post in red and so in that case

bool operator <(Rational &Fraction1, Rational &Fraction2)
{
	return ((Fraction1.numerator1 * Fraction2.denominator2) < (Fraction1.denominator2 * Fraction2.numerator2));
}

since we want a true or false value and the ordinary use of the < gives us true if the numerator-denominator products have this relationship. Therefore, reducing the problem to comparing integers (which is something the compiler knows how to do) helps us to compare the objects. Also, hint, the > operator can make use of this method.

On a peripheral note, your tests function above is meaningless as you have set it up. Mathematically a rational number is any that can be expressed as the ratio of 2 integers, with the denominator not equal to 0.

Edited 6 Years Ago by jonsca: n/a

This is going to be a stupid question, but If I do use the

//tet: changed the function name and removed friend
bool greater_than (Rational &Fraction1, Rational &Fraction2);

Operators are really just functions that the compiler can read in a user friendly manner

so to use this function

if(true == greater_than(r1, r2))
{
std::cout << "rational number r1 is bigger than r2" << std::end;
}

you should convert back to a single rational number I think if you work out in the short Rational example I gave whatshould go into the operator function it would help you

an operator> returns a bool this can be used with if etc

but it has a special layout

bool b = (r1 > r2);
if(b)
{
}
Comments
Tetron, use periods and commas. I'll start infratcin if you keep making unreadable comments.
bool b = (r1 > r2);
if(b)
{
}

Unless you need to store the return for some reason, this is redundant. The whole point of an overload is to allow you to use custom data types just as you would built-in ones.

With a properly overloaded operator, this is more appropriate:

if(r1 > r2)
{
}

When you overload an operator within a class, you write it as if it were a member function of the left-hand object with the right-hand object becoming the argument/parameter for the function. The overload would be similar to this:

bool Rational::operator > (const Rational &rhs) const {
  return ((double)this->numerator/(double)this->denominator) > ((double) rhs.numerator/(double)rhs.denominator);
}

This is only conceptual, it is not tailored specifically to your class. You'll have to modify it to fit.

Edited 6 Years Ago by Fbody: n/a

if(r1 > r2)
{
}

I had already given an example like this (see page 2) the purpose of the bool b was to try and demonstrate how the compiler interprets r1 > r2 and if(condition) but I might have missed the mark

This question has already been answered. Start a new discussion instead.