Answer the following statements true (T) or false (F)

1. You can change the behavior of + for the int type using operator overloading.
2. Overloading a binary operator as a member requires two arguments.
3. Overloading a binary operator as a stand-alone function requires two arguments.
4. When defining a member function of a class, say class A, you can access only private members of the calling object, not those of parameter objects, although they might be of the same class type.
5. One can guarantee left to right evaluation of the arguments to a comma expression overloading.

1. False.
Overloading any operator requires that one or more of the operands be a
class object. This prevents changing the behavior of operators on primitive types.
2. False
One argument is required. Consider the class A with an overloading of
operator +.
```
class A
{
public:
// constructors
A operator+( const A& x);
};
```
If we suppose that leftObject and rightObject are objects of class A. This use of the overloaded operator +,
leftObject + rightObject;
translates to a call to the operator function
leftObject.operator+(rightObject);
That is, the left-hand argument is the calling object for the operator function and the right hand argument is the argument. The result is a single argument for the operator function.
3. True
Consider a use of the operator _:
leftAObject + rightAObject;
This translates to
operator+(rightAObject, leftAObject);
requiring two arguments.
4. False
The text says it clearly: “A class has access to all its objects.” When defining a member function or member operator function of a class, you are allowed to access any member, public or private of any object of the class whose member is being defined.
5. False
Invocations of overloaded operators are function calls. All the arguments of function calls are evaluated before the function is called, but there is no guarantee that any two compilers will generate code with left to right evaluation. It is better not to overload the comma operator.

Computer Science & Information Technology

You might also like to view...

Which key combination is used to open a new tab in Google Chrome?

a. Ctrl + T b. Ctrl + N c. Shift + N

Computer Science & Information Technology

The ____ option plays an animation continuously.

A. Play B. Play All C. Loop D. Single Frame

Computer Science & Information Technology