Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To give an existing operator a new meaning, what property must the argument type

ID: 3572169 • Letter: T

Question

To give an existing operator a new meaning, what property must the argument types possess? When would you choose an overloaded operator for a particular operation, and when would you choose a function? Overload the + operator to raise an employee salary. For example, harry + 5 gives Harry a 5 percent raise. Is this a good use for operator overloading? Could you overload the unary negation operator so that -T would decrement the Time value T by 60 seconds? Would this be a good idea or not? Why are there two versions of the ++ and -- operator functions? Are there any other operators with two versions?

Explanation / Answer

1. For any operator to be overloaded, it is always mandatory to have the function arguments as different. the main reason for having such thing is that because the compiler actually determines which function to access on the basis of the Arguments of the function.

2. Overloading is an ad-hoc polymorphism. Overloading should always be used in the case when you want to increase the functionality or add on the functionality of the Operator. For e.g + is the operator which adds two number, but in case if you want the variable of an object to be added, then in that case you can over load the operator with the defination to add the value inside the object. This actually adds on the basic operator. While function should be used where we can satisfy the requirement with the basic functionality of the operator.

3. Yes, here overloading will help. As after overloading we are enhancing the functionality of the operator. Rather than simple addition it actually makes sure that the total salary's 5% has to be added

4. Yes, we can overload. But it would not be suitable, as having an function would also help us in doing the same. We have constant value to be decremented. which can be put in an function directly for the giving the functionality.

5.++ and -- have two version because by the two version we have made the functionaliy of normal addition function enhanced. ++ -> increases the value by 1, whereas += increase the value mentioned on the RHS, such as the +=2 increases by 2.Similary for the -- operator. No there are no other operator with such versions.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote