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

Operator overloading

  • 1.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 1 Department of Information Technology Operator Overloading CE 142: Object Oriented Programming with C++ Kamlesh Makvana
  • 2.
    Classified e-Material ©CopyrightsCharotar 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.
  • 3.
    Classified e-Material ©CopyrightsCharotar 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);
  • 4.
    Classified e-Material ©CopyrightsCharotar 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.
  • 5.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 5 Department of Information Technology Operator functions could be a member functions
  • 6.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 6 Department of Information Technology Operator functions could be a member functions
  • 7.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 7 Department of Information Technology Operator functions could be a normal UDF.
  • 8.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 8 Department of Information TechnologyOperator functions could be a normal UDF.
  • 9.
    Classified e-Material ©CopyrightsCharotar 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 =, (), [], ->.
  • 10.
    Classified e-Material ©CopyrightsCharotar 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.
  • 11.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 11 Department of Information Technology Overloading Binary Addition operator