Operator Overloading
Data abstraction
 Data abstraction refers to, providing only essential
information to the outside world and hiding their
background details.
 Data abstraction is a programming (and design)
technique that relies on the separation of interface
and implementation.
 C++ classes provides great level of data
abstraction.
Operator Overloading
 C++ allows you to specify more than one definition
for an operator in the same scope, which is called
operator overloading .
 Operators may not look like functions but can hide
function invocations.
 You cannot overload the meaning of operators if all
arguments are primitive data types, nor can you change
the precedence or associativity of operators.
Over loadable Operators in C++
+ - * / %
^ & | ~ ! &&
|| ++ -- << >> ,
< <= == != > >=
= += -= *= /= %=
&= |= ^= <<= >>=
[] () -> new delete
Operator Overloading
 It simplifies the program
 d3.addobject(d1,d2)
 Or the similar but equally obscure
 d3=d1.addobject(d2)
 Can be changed into much readable form
 d3=d1+d2; (legal when d1, d2, d3 are of basic
types)
Simple Example
class complex {
double real, imag;
public:
complex(double r, double i) :
real(r), imag(i) {}
}
 Would like to be able to write:–
complex a = complex(1, 3.0);
complex b = complex(1.2, 2);
complex c = b;
a = b + c;
b = b+c*a;
c = a*b + complex(1,2);
I.e., would like to write
ordinary arithmetic
expressions
on this user-defined class.
With Operator Overloading, We Can
class complex {
double real, imag;
public:
complex(double r, double i) :
real(r), imag(i) {}
complex operator+(complex a, complex b);
complex operator*(complex a, complex b);
complex& operator=(complex a, complex b);
...
}
General Format
returnType operator*(parameters);
  
any type keyword operator symbol
 Return type may be whatever the operator
returns
 Including a reference to the object of the operand
 Operator symbol may be any overloadable
operator from the list.
C++ Philosophy
 All operators have context
 Even the simple “built-in” operators of basic types
 E.g., '+', '-', '*', '/' for numerical types
 Compiler generators different code depending upon type of
operands
 Operator overloading is a generalization of this
feature to non-built-in types
 E.g., '<<', '>>' for bit-shift operations and also for
stream operations
C++ Philosophy (continued)
 Operators retain their precedence and
associativity, even when overloaded
 Operators retain their number of operands
 Cannot redefine operators on built-in types
 Not possible to define new operators
 Only (a subset of) the built-in C++ operators can be
overloaded
Operators that Can and Cannot be
Overloaded
Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
new[] delete[]
Operators that cannot be overloaded
. .* :: ?:
Operator Overload Function
 Either
 a non-static member function definition
or
 a global function definition
 Usually a friend of the class
 Function “name” is keyword operator followed by
the symbol for the operation being overloaded
 E.g. operator+, operator=
Operator Overload Function (continued)
 Operator overload function is a function just like
any other
 Can be called like any other – e.g.,
a.operator+(b)
 C++ provides the following short-hand
a+b
Operator Overload Function (continued)
 If operator overload function is declared global
then
operator+(a, b)
 also reduces to the following short-hand
a+b
Operator Functions as Class Members
 Leftmost operand must be of same class as
operator function.
 Use this keyword to implicitly get left operand
argument.
 Operators (), [], -> or any assignment
operator must be overloaded as a class
member function.
 Called when
 Left operand of binary operator is of this class.
 Single operand of unary operator is of this class.
Operator Functions as Global Members
 Need parameters for both operands.
 Can have object of different class than operator.
 Can be made a friend to access private or
protected data.
Stream Insertion and Extraction Operators
as Global Functions
 Overload << operator used where
 Left operand of type ostream &
 Such as cout object in cout << classObject
 Overload >> has left operand of istream &
 Left operand of type istream &
 Such as cin object in cout >> classObject
 Reason:–
 These operators are associated with class of right
operand
Commutative operators
 May need + to be commutative
 So both “a + b” and “b + a” work as expected.
 Suppose we have two different classes
 Overloaded operator can only be member function
when its class is on left.
 HugeIntClass + long int
 Can be member function
 For the other way, you need a global overloaded
function.
 long int + HugeIntClass
19
Unary Operators
 Operator attached to single operand.
 e.g –a, +a, --a, ++a, a--, a++,
 The unary operators operate on the object for which
they were called and normally, this operator appears
on the left side of the object.
Example
Output
21
Overloading Binary Operators
 The binary operators take two arguments and
following are the examples of Binary operators.
 You use binary operators very frequently like addition
(+) operator, subtraction (-) operator and division (/)
operator.
Example
Example
Example
 Output
Thank You

Operator overloading

  • 1.
  • 2.
    Data abstraction  Dataabstraction refers to, providing only essential information to the outside world and hiding their background details.  Data abstraction is a programming (and design) technique that relies on the separation of interface and implementation.  C++ classes provides great level of data abstraction.
  • 3.
    Operator Overloading  C++allows you to specify more than one definition for an operator in the same scope, which is called operator overloading .  Operators may not look like functions but can hide function invocations.  You cannot overload the meaning of operators if all arguments are primitive data types, nor can you change the precedence or associativity of operators.
  • 4.
    Over loadable Operatorsin C++ + - * / % ^ & | ~ ! && || ++ -- << >> , < <= == != > >= = += -= *= /= %= &= |= ^= <<= >>= [] () -> new delete
  • 5.
    Operator Overloading  Itsimplifies the program  d3.addobject(d1,d2)  Or the similar but equally obscure  d3=d1.addobject(d2)  Can be changed into much readable form  d3=d1+d2; (legal when d1, d2, d3 are of basic types)
  • 6.
    Simple Example class complex{ double real, imag; public: complex(double r, double i) : real(r), imag(i) {} }  Would like to be able to write:– complex a = complex(1, 3.0); complex b = complex(1.2, 2); complex c = b; a = b + c; b = b+c*a; c = a*b + complex(1,2); I.e., would like to write ordinary arithmetic expressions on this user-defined class.
  • 7.
    With Operator Overloading,We Can class complex { double real, imag; public: complex(double r, double i) : real(r), imag(i) {} complex operator+(complex a, complex b); complex operator*(complex a, complex b); complex& operator=(complex a, complex b); ... }
  • 8.
    General Format returnType operator*(parameters);   any type keyword operator symbol  Return type may be whatever the operator returns  Including a reference to the object of the operand  Operator symbol may be any overloadable operator from the list.
  • 9.
    C++ Philosophy  Alloperators have context  Even the simple “built-in” operators of basic types  E.g., '+', '-', '*', '/' for numerical types  Compiler generators different code depending upon type of operands  Operator overloading is a generalization of this feature to non-built-in types  E.g., '<<', '>>' for bit-shift operations and also for stream operations
  • 10.
    C++ Philosophy (continued) Operators retain their precedence and associativity, even when overloaded  Operators retain their number of operands  Cannot redefine operators on built-in types  Not possible to define new operators  Only (a subset of) the built-in C++ operators can be overloaded
  • 11.
    Operators that Canand Cannot be Overloaded Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[] Operators that cannot be overloaded . .* :: ?:
  • 12.
    Operator Overload Function Either  a non-static member function definition or  a global function definition  Usually a friend of the class  Function “name” is keyword operator followed by the symbol for the operation being overloaded  E.g. operator+, operator=
  • 13.
    Operator Overload Function(continued)  Operator overload function is a function just like any other  Can be called like any other – e.g., a.operator+(b)  C++ provides the following short-hand a+b
  • 14.
    Operator Overload Function(continued)  If operator overload function is declared global then operator+(a, b)  also reduces to the following short-hand a+b
  • 15.
    Operator Functions asClass Members  Leftmost operand must be of same class as operator function.  Use this keyword to implicitly get left operand argument.  Operators (), [], -> or any assignment operator must be overloaded as a class member function.  Called when  Left operand of binary operator is of this class.  Single operand of unary operator is of this class.
  • 16.
    Operator Functions asGlobal Members  Need parameters for both operands.  Can have object of different class than operator.  Can be made a friend to access private or protected data.
  • 17.
    Stream Insertion andExtraction Operators as Global Functions  Overload << operator used where  Left operand of type ostream &  Such as cout object in cout << classObject  Overload >> has left operand of istream &  Left operand of type istream &  Such as cin object in cout >> classObject  Reason:–  These operators are associated with class of right operand
  • 18.
    Commutative operators  Mayneed + to be commutative  So both “a + b” and “b + a” work as expected.  Suppose we have two different classes  Overloaded operator can only be member function when its class is on left.  HugeIntClass + long int  Can be member function  For the other way, you need a global overloaded function.  long int + HugeIntClass
  • 19.
    19 Unary Operators  Operatorattached to single operand.  e.g –a, +a, --a, ++a, a--, a++,  The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object.
  • 20.
  • 21.
    21 Overloading Binary Operators The binary operators take two arguments and following are the examples of Binary operators.  You use binary operators very frequently like addition (+) operator, subtraction (-) operator and division (/) operator.
  • 22.
  • 23.
  • 24.
  • 25.