Sometimes we create complex objects that need special handling when we do things like add them together (or subtract).
http://en.wikipedia.org/wiki/Operator_overloading
Let's say you have a class that has a description and a percentage.
You could add object+object+object (etc.) but you might want to ensure the value never goes above 100%. Overloading the + operator would allow you to ensure that.
Let's also say you want to determine what a comparison of Equals means.
Comparing object==object might not return what you expect until you specifically tell the compiler HOW to compare (or which elements are compared) when asking ==.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
In other words, simplistically, it's a way to define what ==, <, >, etc are meant to be for a class that you define yourself.
Example, you have a class of CAR which contains
Make
Model
Year
Price
You can define operator= to mean:
Same Make
Same Year
Price is within $1500 of each other.
Of course at thines01 said, you can also define how to ADD and SUBtract two cars -- whatever you think that would mean...
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Convenience is one benefit.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
But we can do these without overloading also.
Sure. We can make a function: CAR AddTwoCars(CAR vehicle1, CAR vehicle2, CAR vehicleNew)
which 'adds' #1 & #2 and returns the new one. Or we can overload the + operator and have: vehicleNew = vehicle1 + vehicle2; Java do not support operator overloading so why to do in c++ and c#? I am not getting the actual usage.
So if Java doesn't do something, C++ and C# shouldn't either?
Well, ForTran doesn't have classes and pointers. Why should Java, C, C++, C#, Python, ...?
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
@WaltP:
i am not saying that if java does not support than c# should not. I mean to say is java has eliminated the concept of operator overloading and than too we don't need any operator overloading there.
Nitpick: When did Java eliminate overloading? What version of Java took it out of the definition?
Be careful with your wording...So, why we use it here?
Simplistic answer: Because when C++ was defined overloading was a logical enhancement to theobject concept. Why have objects if we can't define operators to operate on them?
Well, i think operator overloading is required when we want to do some operation by creating more than one class objects so that it becomes easy to pass data to function. An i right?
Not a clue what you mean.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
There is no law saying you MUST use this concept.
You also can write code without loops and without user-defined functions.
Some languages don't have conventions for direct memory writes or pointers or multiple inheritance or macros or header files or deterministic destruction.
Some languages have implicit types (Duck Typing).
Some languages come with interactive consoles.
Some languages don't require you to compile and link.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
If at some point, you're going to perform that operation (combining, subtracting, comparing) ANYWAY, you'll still be writing the same code.
But, if you don't need it, don't use it.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402