Operator overloading

KamleshMakvana 381 views 11 slides Apr 03, 2018
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Operator Overloading in C++.


Slide Content

Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 1
Department of Information Technology
Operator Overloading
CE 142: Object Oriented
Programming with C++
Kamlesh Makvana

Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 2
Department of Information Technology
C++ Operator Overloading
One to type of polymorphism
Gives user defined meaning of operators
i.e. perform operations on user defined data
types.
Meaning of operator is already defined
by the compiler for built-in data types.
Redefine same for user defined data
types like objects.

Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 3
Department of Information Technology
Why Operator Overloading
Make program more readable
c= add(c1,c2);
c= c1+c2;
z=add(mul(a,b),sub(x,y));
z=(a*b)+(x-y);

Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 4
Department of Information Technology
How to overload Operator in C++
Create a operator functions
Operator functions could be a member
functions.
Operator functions could be a normal user
defined functions.

Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 5
Department of Information Technology
Operator functions could be a
member functions

Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 6
Department of Information Technology
Operator functions could be a
member functions

Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 7
Department of Information Technology
Operator functions could be a
normal UDF.

Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 8
Department of Information TechnologyOperator functions could be
a normal UDF.

Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 9
Department of Information Technology
Rules for operator overloading
Only existing operators is overloaded. Can
not create new operators.
Overloaded operator must have at least one
operands.
Basic meaning of operator could not be
changed.
Some operators can not be overloaded like
dot(.), ::, ?:, sizeof, etc.
We can not use friend function to overload
some operators like =, (), [], ->.

Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 10
Department of Information Technology
Rules for operator overloading
If operator function is friend function
Unary operator: one arguments.
Binary operator: two arguments.
If operator is non-static member function.
Unary operator: no arguments.
Binary operator: one arguments.
Left-hand operand must be a type of relevant type.
Binary arithmetic should explicitly return a value.

Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 11
Department of Information Technology
Overloading Binary Addition
operator