Operator Overloading in C++
Operator Overloading
What is an operator?
Operators are symbolic representation of mathematical or
logical operations that can be performed on one or more operands. In C++, there
are following operators present:
Operator
|
Type
|
Number of operands
|
++,--
|
Unary Operator
|
1
|
+,-,*,/,%
|
Arithmetic operator
|
2
|
<,<=,>,>=,==,!=
|
Relational operator
|
2
|
&&,||,!
|
Logical operator
|
2
|
&,|,<<,>>,~,^
|
Bitwise operator
|
2
|
=,+=,-=,*=,/=,%=
|
Assignment operator
|
2
|
?:
|
Ternary operator
|
3
|
There are some special operators such as . (dot
operator), :: (Scope resolution) and sizeof operator. Each of them has a
special purpose such as the dot operator is used for Member resolution, :: is
used to change the resolution of a assignment or Member and sizeof is a C++
compiler unary operator which is used to determine the memory size of its operand.
Operator precedence table is as follows-
OPERATOR
|
DESCRIPTION
|
ASSOCIATIVITY
|
()
|
Parentheses (function call)
|
left-to-right
|
[]
|
Brackets (array subscript)
|
|
.
|
Member selection via object
name
|
|
->
|
Member selection via pointer
|
|
++/–
|
Postfix increment/decrement
|
|
++/–
|
Prefix increment/decrement
|
right-to-left
|
+/-
|
Unary plus/minus
|
|
!~
|
Logical negation/bitwise
complement
|
|
(type)
|
Cast (convert value to
temporary value of type)
|
|
*
|
Dereference
|
|
&
|
Address (of operand)
|
|
sizeof
|
Determine size in bytes on this
implementation
|
|
*,/,%
|
Multiplication/division/modulus
|
left-to-right
|
+/-
|
Addition/subtraction
|
left-to-right
|
<< , >>
|
Bitwise shift left, Bitwise
shift right
|
left-to-right
|
< , <=
|
Relational less than/less than
or equal to
|
left-to-right
|
> , >=
|
Relational greater than/greater
than or equal to
|
left-to-right
|
== , !=
|
Relational is equal to/is not equal
to
|
left-to-right
|
&
|
Bitwise AND
|
left-to-right
|
^
|
Bitwise exclusive OR
|
left-to-right
|
|
|
Bitwise inclusive OR
|
left-to-right
|
&&
|
Logical AND
|
left-to-right
|
||
|
Logical OR
|
left-to-right
|
?:
|
Ternary conditional
|
right-to-left
|
=
|
Assignment
|
right-to-left
|
+= , -=
|
Addition/subtraction assignment
|
|
*= , /=
|
Multiplication/division
assignment
|
|
%= , &=
|
Modulus/bitwise AND assignment
|
|
^= , |=
|
Bitwise exclusive/inclusive OR
assignment
|
|
<>=
|
Bitwise shift left/right
assignment
|
|
,
|
expression separator
|
left-to-right
|
Operator Overloading
In C++, the operators work on in-built data types
without any issues but if we want to use operators for user-defined classes then
we must Operator Overloading.
In overloading, a declaration which is similar (in name/operator)
is declared in the same scope, and the compiler decided which definition is
appropriate depending on the argument types passed with the call of the
declaration. This process is known as overload resolution. Overloading can be
done for functions as well as operators. For operator overloading the keyword operator
and operator symbol are required as the function name. User cannot
define their own operators.
In operator overloading, all above mentioned operators
can be overloaded with the exceptions-
1) . (dot operator)
2) ::
(Scope resolution operator)
3) ?: (Ternary operator)
4) sizeof
operator.
In operator overloading, At least one operand must be a
user defined class object. Also, Assignment operator creates a default operator
with every class, such that it can assign right side to left side. Conversion
operators can be used to convert data from one type to another.
The operator declaration is called in base class of
the first operand. The second operator can be passed by reference or by value during
the operator call.
Example:
Limitations on Operator overloading-
1) The
precedence and associativity of the operators cannot be changed.
2) Number
of operands for an operator cannot be changed.
3) One
operand must be of used defined class.
4) User
cannot define new operators.
Aadhiraj More
Brilliant! Looking forward for next blog.
ReplyDeleteThank You
DeleteVery informative! Good work
ReplyDelete