Link to home
Start Free TrialLog in
Avatar of loving_star
loving_star

asked on

Operator Overloading in C++

Its very urgent to me. Can u please write for me a small easy program how to overload '+' and '++' operator in C++? I will be really grateful if u can do it soon..Thanx
regards - LovingStar
Avatar of fxnut
fxnut

It depends on exactly what you want to do with it, i.e. what you want the operators to operate on. Here's an example for a 2D vector class...


class Vector2D
{


};
ASKER CERTIFIED SOLUTION
Avatar of fxnut
fxnut

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep, thanks! I was typing that in (instead of copying and pasting) and added the return by reference in by mistake. And you also noticed that I forgot to add in the public definition - something I keep on forgetting to do in my own code! :-)

So the start should look like this...

class Vector2
{
public:
   float x,y;

etc...
SOLUTION
Avatar of Harisha M G
Harisha M G
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I could be wrong on this mgh (feel free to correct me!), but I don't think your ++ operator is correct.

For your complex class, the ++ operator overload definitions should be..

   Complex& operator++();       // Prefix increment operator.
   Complex operator++(int);     // Postfix increment operator

Or alternatively, you could define them at file scope (i.e. not in the class)

   friend Complex& operator++( Complex& )      // Prefix increment
   friend Complex& operator++( Complex&, int ) // Postfix increment

The other problem is that your ++ operator passes in a Complex type by value, and it doesn't modify "this"s values at all, so in actual fact your ++ operator does nothing to the instance that is operated on, all it does is return a new incremented copy of the value passed in. In my mind, it's a bad idea to define a ++ operator that has non-standard behaviour. (And that's assuming that your compiler will let you define such an overload which some might not)


In mgh_mgharish's absence I'll comment.

The code is very C-esque - absence of encapsulation - and as such is fully functional. Again, the lack of reference parameters is again traditional C.

I agree with fxnut on c++; This is definitely postfix and the extra [unused] int parameter would be required. Perhaps the compiler has optomised this (given that the return value is not required) and transformed it into ++c;. Unusual for user defined types.

Final note - I wouldn't re-invent the wheel for complex classes; STL already has one.
Are you gonna close this question?
Since mgh_mgharish's answer was prettyflawed, and _ys_ was only providing feedback on my answer, I would have thought accepting my answer with an assist from _ys_ would seem the fairest. But I'm happy to settle for a split between myself and _ys_.