CSC-210 Lecture 04-Operator Overloading
CSC-210 Lecture 04-Operator Overloading
Operator Overloading
1
CONTENTS
2
ADDING MEANING TO OPERATORS
Complex c3= c1 + c2;
The above statement indicates two numbers c1 and c2.
The actual operation is more involved than a simple arithmetic addition between
two numbers, we would be required to add more meaning to the ‘+’ operator in the
specified context. The C++ feature called operator overloading does this.
Operator overloading allows to override the default meaning of assigned to the
regular operators in a given context.
3
OVERLOADING ARITHMETIC OPERATORS
First we will consider overloading arithmetic operators such as the addition (‘+’)
and the subtraction (‘-’) operators. These are binary operators; it means they
require two operands.
4
OVERLOADING ARITHMETIC OPERATORS (TASK)
Overloading Binary Operators:
Overloading addition operator
5
OVERLOADING ARITHMETIC OPERATORS
Defining operator function outside Class definition:
6
OVERLOADING SHORT HAND OPERATORS (TASK)
will discuss overloading of assignment operators. We will overload += and *=operators.
7
OVERLOADING INCREMENT & DECREMENT OPERATORS IN
POSTFIX FORM (TASK)
8
OVERLOADING SPECIAL OPERATORS
Overloading of some special operators like: array subscripting operator [ ] ,
function calling ( ) and class member axis operator -> brings some useful task to
achieve.
But there are some restrictions while overloading these operators:
They must be non-static member functions.
These functions cannot be friend function to overload.
9
OVERLOADING SPECIAL OPERATORS (TASK)
OVERLOADING FUNCTION CALL OPERATOR ( ):
10