Operator Overloading Flashcards

1
Q

Operator Overloading:

A

C++ tries to make the user-defined data types behave in much the same way as the built-in types.
For instance, C++ permits us to add two variables of user-defined types with the same syntax that is
applied to the basic types. This means that C++ has the ability to provide the operators with a special
meaning for a data type. The mechanism of giving such special meanings to an operator is known as
operator overloading.

Overload(Give additional meaning to).

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

Which operators in C++ cant be overloaded:

A
  • Class member access operators (., .*).
  • Scope resolution operator (::).
  • Size operator (sizeof).
  • Conditional operator (?:).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why are there exceptions to operator overloading in C++?

A

The reason why we cannot overload these operators may be attributed to the fact that these
operators take names (example class name) as their operand instead of values, as is the case with
other normal operators.

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

How operator overloading affects the operator?

A

Although the semantics of an operator can be extended, we cannot change its syntax, the
grammatical rules that govern its use such as the number of operands, precedence and associativity.

Remember, when an operator is overloaded, its original meaning is not lost. For
instance, the operator +, which has been overloaded to add two vectors, can still be used to add
two integers.

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

DEFINING OPERATOR OVERLOADING
and
The process of overloading involves the following steps:

A
To define an additional task to an operator, we must specify what it means in relation to the class to
which the operator is applied. This is done with the help of a special function, called operator function,
which describes the task.
  1. Create a class that defines the data type that is to be used in the overloading operation.
  2. Declare the operator function operator op() in the public part of the class. It may be either a
    member function(nonstatic) or a friend function.
  3. Define the operator function to implement the required operations.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The general form of an operator function is:

A

return type classname :: operator op(arglist)
{
Function body // task defined
}

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

Operator functions must be either member functions (nonstatic) or friend functions. A basic difference between them is?

A
that a friend function will have only one argument for unary operators and two
for binary operators, while a member function has no arguments for unary operators and only one for
binary operators. This is because the object used to invoke the member function is passed implicitly
and therefore is available for the member function. This is not the case with friend functions.
Arguments may be passed either by value or by reference. Operator functions are declared in the
class using prototypes as follows:

vector operator+(vector); // vector addition
vector operator–(); // unary minus
friend vector operator+(vector,vector); // vector addition
friend vector operator–(vector); // unary minus
vector operator–(vector &a); // subtraction
int operator==(vector); // comparison
friend int operator==(vector,vector) // comparison

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

Overloaded operator functions can be invoked by expressions such as:

A
op x or x op
for unary operators and
x op y
for binary operators. op x (or x op) would be interpreted as
operator op (x)
for friend functions. Similarly, the expression x op y would be interpreted as either
x.operator op (y)
in case of member functions, or
operator op (x,y)
in case of friend functions. 

When both the forms are declared, standard argument matching is
applied to resolve any ambiguity.

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