Object Oriented Programming
Operator Overloading
Basic Concept
Operator
++ -- -
Basic Concept
Basic Concept
Can not work with t1 & t2 two
because their data type is user
define test. We need to extend
++t1 the functionality of operator to
t2++; work with object then it will work
with user define data types.
This practice is known as
operator overloading
Not overload these operators
Operator Overloading
Overload Operator Syntax
Overload Unary Operator (Prefix ++t)
Unary operator overload because it
only work with variable not with
object.
So now it will also work with object
as we did in chap-3
++t will increase one value as it
increase with variable in Chap-3
It will return 1
Overload Unary Operator (Prefix examp)
Overload Unary Operator (Postfix t++)
When we want to use postfix (t++)
then we use int within parenthesis
(int) and parenthesis remain empty
in prefix ( )
Unary operator overload because it
only work with variable not with
object.
So now it will also work with object
as we did in chap-3
Note: it will work for postfix
t++ will increase one value as it
increases with variable in Chap-3
It will return 1 value
Overload Unary Operator (Postfix Exam)
Overload Unary Operator (Pre+Post)
Overload Unary Oper (Pre+Post) Exam
Return value (Postfix t++)
Here we use operator overload with
int return type so it will return value
of type int.
t++ will increase one value and here
value will come because we have
used return keyword in function
definition.
Return Type object (using Postfix t++)
Here we overload unary operator
(post fix) with return type oper.
So we use return word in function
body as we learn in chap-9.
It will increase one value of class
object as we see in previous
example
[Link]() will return 0 valie
We perform two time t++ operator.
So it will incrase vale of a two times
[Link]() will return 2
Overload Binary Operator (+,-, *,/…)
Constructor which will give value
to m and n automatically when
object of class will be created
Member function that will ask
value of m and n at run time
Binary operator overload function (+).
It will perform addition between (m&n)
r.n = n + t . n ; when we call it.
r.m = m + t . m ;
It will call overload function and
perform addition between the
objects of class (U and W)
It will display value of addition as
we store in object s
Working of Binary Operator C=[Link] (B)
Class Responsibility
class will do practice
of other operators
by yourself