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 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;
}

You aren't overloading any operators in this way ...

Look at the following example:

Rational operator+(const Rational& left, const Rational& right)
{
    Rational result;
    /* Your code to add two Rationals here */
    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 Rational::addition(Rational a, Rational b){
	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 aren't overloading any operators in this way ..
You're right.

Ok thanks for that, and I know I wasn't overloading in those functions, I did the overloading already.

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)


any ideas?? here is the overloading code

istream& operator>> (istream& in,  Rational& r)
{
		string readLn;
		getline(in,readLn);	
		int index = readLn.find("/");
		if (index < 0)
		{
			try
			{
			r.SetNumerator(atoi(readLn.c_str())); 
			r.SetDenominator(1);
			return in;
			}
			catch(char * str)
			{
				throw str;
			}
			return in;
		}
		readLn.replace(index,1," ");
		stringstream sr = stringstream(readLn);
		int tempNum,tempDen;
		sr >> tempNum >> tempDen;
		r.SetNumerator(tempNum);
		r.SetDenominator(tempDen);
		return in;
}

thanks guys, any help is rly appreciated :D

Your overloaded >> is a bit complicated, here's a simpler version:

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)

any ideas?? here is the overloading code

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;
    in>>readLn;

Things would have work more smoothly.

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.