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 ==.
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, ...?
Only as much as any other function, not really a problem in general
Means in totality there is no actual use.
I'd disagree with this. You are correct in the sense that, since and operator is basically a function, it would always be possible to do whatever it is that you want to do using a "traditional" functions. However, in lots of cases, an overloaded operator is much more readable than a regular function. And, readability should NEVER be underestimated. The other area where operator overloading is very useful, is if you introduce a new class/concept and you want to communicate that certain behaviours are equivalent to some existing behaviour. The best example I can think of this is STL iterators. Iterators overload many operators to make them behave like pointers in situations where they will basically be used like pointers, for example: for ( it = it_start; it != it_finish; ++it ) or it->MyMemberFunction(); or SomeClass x = *it; All of these could be done using specific iterator member functions (since that's all an operator overload is), but I prefer for ( it = it_start; it != it_finish; ++it ) to for ( it.set_position( it_start ); it.not_equal_to( it_finish ); it.increment() ) .
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. So, why we use it here? 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?
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 the object 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?
public class GrossmontBank
//class variables (global - accessible throughout this class)
//scanner object to be used throughout
private static Scanner input = new Scanner(System.in);