As part of an assignment I am creating a class to handle rationals with numerators and denominators, overloading the operators for cin and cout, as well as +-*/. So these are the functions for those operators, now where exactly should I include these in my class?

``````Rational addition(Rational a, Rational b){
Rational result = Rational((a.numerator()*b.denominator() + b.numerator()*a.denominator()), a.denominator()*b.denominator());
return result;
}

Rational subtraction(Rational a, Rational b){
Rational result = Rational((a.numerator()*b.denominator() - b.numerator()*a.denominator()), a.denominator()*b.denominator());
return result;
}

Rational multiplication((Rational a, Rational b){
Rational result = Rational(a.numerator()*b.numerator(), a.denominator()*b.denominator());
return result;
}

Rational division((Rational a, Rational b){
Rational result = Rational(a.numerator()*b.denominator(), a.denominator()*b.numerator());
return result;
}``````

As part of an assignment I am creating a class to handle rationals with numerators and denominators, overloading the operators for cin and cout, as well as +-*/. So these are the functions for those operators, now where exactly should I include these in my class?

``Rational addition(Rational …``

Look at the following example:

``````Rational operator+(const Rational& left, const Rational& right)
{
Rational result;
return result;
}``````

## All 6 Replies

As part of an assignment I am creating a class to handle rationals with numerators and denominators, overloading the operators for cin and cout, as well as +-*/. So these are the functions for those operators, now where exactly should I include these in my class?

``````Rational addition(Rational a, Rational b){
Rational result = Rational((a.numerator()*b.denominator() + b.numerator()*a.denominator()), a.denominator()*b.denominator());
return result;
}

Rational subtraction(Rational a, Rational b){
Rational result = Rational((a.numerator()*b.denominator() - b.numerator()*a.denominator()), a.denominator()*b.denominator());
return result;
}

Rational multiplication((Rational a, Rational b){
Rational result = Rational(a.numerator()*b.numerator(), a.denominator()*b.denominator());
return result;
}

Rational division((Rational a, Rational b){
Rational result = Rational(a.numerator()*b.denominator(), a.denominator()*b.numerator());
return result;
}``````

Look at the following example:

``````Rational operator+(const Rational& left, const Rational& right)
{
Rational result;
return result;
}``````

>>now where exactly should I include these in my class?
As these are the definitions of your class, it can either go right after where you defined the class:

``````class Rational
{
private:
// private members declaration
public:
//public members declaration
};

Rational result = Rational((a.numerator()*b.denominator() + b.numerator()*a.denominator()), a.denominator()*b.denominator());
return result;
}
Rational Rational::subtraction(Rational a, Rational b){
//definition
}
//so on``````

You're right.

But now I'm having trouble with the >> overloading... i can use

``````Rational r;
cin >> r;
cout << r;``````

thats working fine, but when i try to input 2 or more rationals at the same time it doesnt work.... for starters the next line wont execute until u press enter twice, and when it does it didnt do the addition right - which works when it uses 2 cin lines

WORKS

``````Rational r1;
Rational r2;
cin >> r1;
cin >> r2;
cout << r1+r2;``````

that outputs the right value (i tested it with 1/2 and 1/4 and it printed 3/4)

DOESNT WORK

``````Rational r1;
Rational r2;
cin >> r1 >> r2;
cout << r1+r2;``````

outputs 1/2 (or the first value entered)

``````istream& operator>> (istream& in,  Rational& r)
{
if (index < 0)
{
try
{
r.SetDenominator(1);
return in;
}
catch(char * str)
{
throw str;
}
return in;
}
int tempNum,tempDen;
sr >> tempNum >> tempDen;
r.SetNumerator(tempNum);
r.SetDenominator(tempDen);
return in;
}``````

thanks guys, any help is rly appreciated :D

``````int main() {
int nom, denom;
string buf;

getline(cin, buf);
stringstream ss(buf);

ss >> nom;
//discard all the remaining chars, stop when / is found
ss.ignore(ss.str().length(), '/');
ss >> denom;

cout << "Read: " << nom << "/" << denom << endl;

return 0;
}``````

I know this doesn't overload anything, but the code is there. Understand what it does and build it in. It doesn't do any error checking as you can see, so you might want to add that yourself. ;) (Or, you know, screw the user. He should double check his entries. Saves a lot of code :D)

Well look at your code. You are unnecessarily using getline() when you should have been using cin.
Tell me if your user will enter values which will have spaces like "2 / 7" ? If no, then why are you using getline() ?
The harm in using getline is worth considering.
Suppose you issue a statement:

``````cin >> r1 >> r2;
``````

This will cause the execution of the code you wrote. The compiler will issue the call for the function operator>>(cin,r1) which you overloaded. The compiler will issue another call the the same function with the first parameter as the return value of the first function call and the second parameter as r2.
Let us check the first function call. The compiler sees a getline. So it accepts a line from user. Now suppose the user entered: `2/1 3/4`. Hence readLn will have value "2/1 3/4" as a string.
Now you have replaced the first / with a space. So your readLn looks like "2 1 3/4"
And now, using string streams you will set the numerator as 2 and denominator as 1. Good. But the 3/4 is simply LOST.
So this actually will not do what you wanted.
If instead you have been using cin(here in) in the 3rd line of your function like :

``````    string readLn;