class/struct/union

May 25, 2011 at 12:48am
Rational Rational::operator+(const Rational &)const
{
int num, int den;
c.num = a.num * b.den + a.den * b.num;
c.den = a.den * b.den;
}
Hi everyone I'm trying to do a program for my c++ class with classes and operator overloading and having trouble as to understanding what class/struct/union. In particular with the num and den here.
May 25, 2011 at 1:02am
Have you read the tutorial in this website? Other websites? Books? How far are you in C++? Because I see an operator overload there, but I see no name for the parameter, I see variables a, b and c in use but not declared in the operator's scope, and I see two int's declared but not in use.

So I see a bunch of noob mistakes. Maybe you are not ready to overload operators just yet. I think you need to reinforce your knowledge in more basic areas, like what is a class and around it, and also revisit functions and parameters (maybe also passing by reference too?). Try the tutorial here: http://www.cplusplus.com/doc/tutorial/.

And practice, practice, practice.
May 25, 2011 at 1:04am
well as a matter of fact i was looking at that same tutorial right now and looking it over but thank you.
May 25, 2011 at 4:50am
closed account (D80DSL3A)
This sites tutorial section seems to be missing the subject of operator overloading.
Perhaps an example would help.
The 2 Rational objects you are trying to add (a and b) are the operands in a+b. The right operand (b) is the one supplied as a parameter to the function.
The left operand (a) is the one calling the function. Do you know about the this pointer?
The function should return a Rational object, so you need to construct one in the function.
What constructors are available in the Rational class?

If you only have a default constructor (taking no arguments) then you can make it work like so:
1
2
3
4
5
6
7
Rational Rational::operator+(const Rational & b)const
{
    Rational retVal;// must assign the members here in the function
    retVal.num = this->num * b.den + this->den * b.num;// use of "this" is optional
    retVal.den = this->den * b.den;
    return retVal;
}

If there is a constructor which takes 2 integers and assigns them to num and den, then the function can be written very simply as:
1
2
3
4
5
Rational Rational::operator+(const Rational & b)const
{
    // constructing the returned object in the return statement.
    return Rational( num * b.den + den * b.num, den * b.den );// omitting use of "this" (it is implied)
}

Hopefully this example will help with other operators you need to write.
May 25, 2011 at 2:11pm
thank you very much for your help :) i have read about the this pointer before but i assumed that it was used just to return a value at the end of the function, but i guess i was wrong. This has been very helpful to me and with my other operators thank you :)
May 25, 2011 at 2:29pm
when overloading other operators such as the << and such, i have a reduce function but how would i go about returning those values in the two parameters it asks for? im going to look at that tutorial again. i appreciate all ur help! :)
May 25, 2011 at 3:50pm
never mind i got it : ) thank you very much for your help!
Topic archived. No new replies allowed.