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
loving_starAsked:
Who is Participating?
 
fxnutCommented:
Oops, pressed submit by accident..


class Vector2
{
   float x,y;

   Vector2& operator +=(const Vector2& rhs)
   {
      x+=rhs.x;
      y+=rhs.y;
      return *this;
   }

   const Vector2& operator + (const Vector2& rhs) const
   {
      return Vector2(x+rhs.x,y+rhs.y);
   }

   //Not much use in a vector class, but you could do this...
   
   //This is the prefix operator
   Vector2& operator ++()
   {
      x++;
      y++;
      return *this;
   }

   //And this is the postfix operator
   Vector2 operator ++(int)
   {
      Vector2 temp = *this;
      x++;
      y++;
      return temp;
   }

};
0
 
fxnutCommented:
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
{


};
0
 
_ys_Commented:
Quick correction on operator+
Should not return a reference

const Vector2 operator + (const Vector2& rhs) const;

Also operators are typically non-privately scoped.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
fxnutCommented:
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...
0
 
Harisha M GCommented:
1. Consider a program for complex numbers

struct complex
{
  int re,im;
};

complex operator + (complex c1, complex c2)
{
  complex temp;
  temp.re = c1.re + c2.re;
  temp.im = c1.im + c2.im;
  return temp;
}

complex operator ++(complex c1)
{
  complex temp;
  temp.re = c1.re + 1;
  temp.im = c1.im;
  return temp;
}


void main()
{
  complex a, b, c;
  a.re = 1;
  a.im = 4;
  b.re = 3;
  b.im = 2;
 
  c = a + b;
  if(c.im>0)
    cout<<c.re<<" + i"<<c.im;
  if(c.im<0)
    cout<<c.re<<" - i"<<c.im;
  if(c.im==0)
    cout<<c.re;
  c++;
  if(c.im>0)
    cout<<c.re<<" + i"<<c.im;
  if(c.im<0)
    cout<<c.re<<" - i"<<c.im;
  if(c.im==0)
    cout<<c.re;

}

0
 
fxnutCommented:
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)


0
 
_ys_Commented:
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.
0
 
fxnutCommented:
Are you gonna close this question?
0
 
fxnutCommented:
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_.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.