A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing


  • 4 years ago

can someone give me example and explain in easy way to understand overloading operators in c++?? I'm already googling that and read about that but my mind can't understand it.

  • This Question is Closed
  1. farmdawgnation
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    So, this article explains, in general, how Operator Overloading works: http://en.wikipedia.org/wiki/Operator_overloading In a nutshell, the practice of Operator Overloading essentially allows you to take basic operators, such as "+", and give them different behaviors based on what the operands are. So, for example, let's say you have to integers. intA and intB. The + operator makes sense for these two operators. It's whatever the sum of the two integers are. But, what if you have two arbitrary classes: classA and classB. What happens when you try to do this: classA + classB It doesn't make sense by default because they are not integers or strings. However, when C++ sees that, it will check for an overloaded version of that operator. If it finds such an overloaded version, it will run that instead of a normal add operation. Anyway, I hope that's a good explanation. I've never had cause to use it extensively. If you have any further questions, just ask. :)

  2. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    An overloaded operator functions in much the same way as an overridden virtual function. They have defined signatures (there are multiple valid versions for each operator) and are called just like a function when the operator is used (as farmdawgnation described). In addition to the normal arithmetic operators (+-*/), you can also overload the assignment operator = (vital for concepts such as smart pointers) and compound assignment operators, modulo (%), pre- and post increment and decrement (i--, --i, i++, ++i), comparison operators, logical operators, dereferencing operators, stream insertion and extraction operators (<<, >>) and pretty much anything else you can think of. new and delete are also operators that can be overloaded, so you can implement custom memory allocation for each class if you like. Operator overloading is extremely powerful and can make your code much easier to understand. Imagine a vector class in a math library - without overloaded operators, you'd have to implement add or subtract as functions, so a simple operation like adding the difference between vectors a and b to vector c and multiplying the result by d, can turn into a pretty ugly line of code: Vector result = d.Multiply(c.Add( a.Subtract(b) )); At first glance, it's difficult to understand what this code does. Now compare to the version with overloaded operators: Vector result = d * ( c + (a-b) ); Much better, isn't it? It's up to you what the operators do, but I highly recommend sticking to common sense and established conventions. Always write your code as if someone else was going to use it - even if nobody else does, chances are you'll end up looking at it again six months after you wrote it and won't remember what it did, so it's in effect as if someone else is trying to use it ;) Some software I've seen does strange things, for example decide to override the ^ operator in a string class to do string concatenation. ^ is the XOR operator, so having it do string concatention makes not a bit of sense and is fairly unintuitive - if 0xFFFF ^ 0x1111 = 0xEEEE, then why is "Hello" ^ "World" = "Hello World"?

  3. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...


  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.