Solved

Operator Overloading in C++

Posted on 2004-03-22
11
478 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:loving_star
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
11 Comments
 
LVL 1

Expert Comment

by:fxnut
ID: 10656255
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
 
LVL 1

Accepted Solution

by:
fxnut earned 43 total points
ID: 10656299
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
 
LVL 9

Assisted Solution

by:_ys_
_ys_ earned 41 total points
ID: 10657690
Quick correction on operator+
Should not return a reference

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

Also operators are typically non-privately scoped.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:fxnut
ID: 10658176
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
 
LVL 37

Assisted Solution

by:Harisha M G
Harisha M G earned 41 total points
ID: 10665209
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
 
LVL 1

Expert Comment

by:fxnut
ID: 10665453
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
 
LVL 9

Expert Comment

by:_ys_
ID: 10665829
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
 
LVL 1

Expert Comment

by:fxnut
ID: 10821214
Are you gonna close this question?
0
 
LVL 1

Expert Comment

by:fxnut
ID: 10984210
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question