8 - Operator overloading, Friends, and References. Flashcards

1
Q

What is the relationship between operators and functions?

A

Operators are nothing but functions that are used with a slightly different syntax. We write x + 7 instead of +(x, 7). But the operator is a function that takes two arguments (often called operands in this case.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

If operators basically are functions, why do they exist at all?

A

Operands are an example of what is often called syntactic sugar, meaning a slightly different syntax that people like.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is syntactic sugar?

A

Syntactic sugar us a syntax that people like. It may differ from the rest of the syntax.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the syntax for the function declaration of an operator declaration?

A

An example is :

const Money operator + (const Money& amount1, const Money& amount 2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

One major restriction on overloading an operator is that …

A

at least one operand must be of a class type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a binary operator?

A

With a binary operator, the arguments are listed before and after the operator; with a function the arguments are listed in parentheses after the function name. Examples of binary operators are: +, - , /, %, and so forth.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is special about the return statement in a definition of an overloaded operator?

A

It may often return an object of a class. This can be done simply using the constructor of the class, and passing it arguments. Such as:

return Money (finalDollars, finalCents);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

return Money (finalDollars, finalCents);

This object is sometimes called …

A

An anonymous object, since it is not named by any variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an anonymous object?

A

An object of a class that is not named by any variable. Can be constructed using the constructor function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

const Money operator + (……..)

This is a new use of the const modifier. What is this use called?

A

This is called “returning a value as const” or “ returning by const value” or “returning by constant value”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why would you ever want to return by const value?

A

It provides a kind of automatic error checking. When you construct (m1 + m2) , you normally do not want to inadvertently change it. In this case changing the object (m1 + m2) probably does not matter, but if the returned object is a reference to an existing object then this can cause problems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

With objects of a class, the default assignment operator does not …

A

With objects of a class, the default assignment operator (=) does not make the two objects the same objects, it only copies values of member variables from one object to another object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Returning by const is difficult to understand. What’s a good rule of thumb for this method?

A

A good rule of thumb is to always return class types by const value unless you have have explicit reason not to do so.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

There are two types of operators. Which?

A
  1. binary and

2 unary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are unary operators?

A

A unary operator is an operator that takes only one operand.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Give an example of a unary operator.

A
  • when used on a number, meaning the negative value of that number. Also the operators ++ and –.
17
Q

What is the difference of overloading binary operators as standalone functions defined outside the class, and overloading them as a member operator?

A

When a binary operator is overloaded as a member operator, there is only one parameter, not two. The calling object serves as the first parameter.

18
Q

What are good arguments for overloading as member functions and not as standalone functions?

A

Many experts advocate always overloading operators as member operators. It is more in the spirit of object-oriented programming and is a bit more efficient, since the definition can directly reference member variables and need to use accessor and mutator functions.

19
Q

What are the disadvantages of overloading as member functions and not as standalone functions?

A

coming soon.

20
Q

Which operators is it generally best not to overload?

A

The && and ||.

21
Q

Why is it generally a bad idea to overload && and ||?

A

When overloaded these operators perform complete evaluation, and not the normal short-circuit evaluation. This is so contrary to what most programmers expect that it inevitably causes problems. It is best to just not overload these two operators.