Operator Overloading Flashcards
Operator Overloading:
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).
Which operators in C++ cant be overloaded:
- Class member access operators (., .*).
- Scope resolution operator (::).
- Size operator (sizeof).
- Conditional operator (?:).
Why are there exceptions to operator overloading in C++?
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 operator overloading affects the operator?
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.
DEFINING OPERATOR OVERLOADING
and
The process of overloading involves the following steps:
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.
- Create a class that defines the data type that is to be used in the overloading operation.
- 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. - Define the operator function to implement the required operations.
The general form of an operator function is:
return type classname :: operator op(arglist)
{
Function body // task defined
}
Operator functions must be either member functions (nonstatic) or friend functions. A basic difference between them is?
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
Overloaded operator functions can be invoked by expressions such as:
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.