Operator Overloading and
Type Conversion
Chapter – 10
10.1 Introduction
 Operator Overloading is similar to that of Function Overloading.
 It is a type of Polymorphism
 Polymorphism allows to permit multiple definitions for functions and
operators
 Operator Overloading helps the programmer to use the operators with
objects of classes.
 Eg. Book
10.2 The Keyword Operator
10.3 Overloading Unary Operators
 Overloading of explicit argument to an operator function is
called unary operator overloading.
 Operators ++ , -- and – are unary operator.
 Unary operator ++ and – can be used as prefix or suffix with
functions
 Eg. Book
10.7 Overloading with friend Function
 friend functions are more useful in operator overloading.
 Difference b/n member function and friend function is the member function takes
argument explicitly.
 friend function needs parameters to be explicitly passed
 Syntax:
10.9 Type Conversion
Three possibilities of Data Conversions are
Conversion from Basic Data type to User-defined data type(class
type)
Conversion from class type to basic data type
Conversion from one class type to another class type
10.9.1Conversion from Basic to class type
It is automatically done by the compiler with the help of
built-in routines or by applying type casting.
Here , left-hand operand of = sign is always class type and
right-hand sign operand is always basic type
Eg. Book
10.9.2 Conversion from Class type to Basic Data Type
 Compiler does not know about user defined data type built using
classes.
 Programmer needs explicitly tell the compiler about the conversion
from class to basic type
 Such type of conversion is known as Overloading of type cast
operators
 Here left-hand operand is always basic data type
 Right-hand operand is always of class type
10.9.2 Conversion from Class type to Basic Data Type
 During conversion , statement should satisfy following conditions
:
 The conversion function should not have any argument
 Do not mention return type
 It should be a class member function
 Eg. Book
10.9.3 Conversion from One Class type to Another
Class Type
When an object of one class is assigned to object
of another class, important to give clear
instructions to compiler about how to make
conversion b/n 2 user-defined data type.
10.10 Rules for Overloading Operators
When an operator is overloaded ,its properties
like syntax , precedence and associatively
remain constant

Operator Overloading

  • 1.
    Operator Overloading and TypeConversion Chapter – 10
  • 2.
    10.1 Introduction  OperatorOverloading is similar to that of Function Overloading.  It is a type of Polymorphism  Polymorphism allows to permit multiple definitions for functions and operators  Operator Overloading helps the programmer to use the operators with objects of classes.  Eg. Book
  • 3.
  • 4.
    10.3 Overloading UnaryOperators  Overloading of explicit argument to an operator function is called unary operator overloading.  Operators ++ , -- and – are unary operator.  Unary operator ++ and – can be used as prefix or suffix with functions  Eg. Book
  • 5.
    10.7 Overloading withfriend Function  friend functions are more useful in operator overloading.  Difference b/n member function and friend function is the member function takes argument explicitly.  friend function needs parameters to be explicitly passed  Syntax:
  • 6.
    10.9 Type Conversion Threepossibilities of Data Conversions are Conversion from Basic Data type to User-defined data type(class type) Conversion from class type to basic data type Conversion from one class type to another class type
  • 7.
    10.9.1Conversion from Basicto class type It is automatically done by the compiler with the help of built-in routines or by applying type casting. Here , left-hand operand of = sign is always class type and right-hand sign operand is always basic type Eg. Book
  • 8.
    10.9.2 Conversion fromClass type to Basic Data Type  Compiler does not know about user defined data type built using classes.  Programmer needs explicitly tell the compiler about the conversion from class to basic type  Such type of conversion is known as Overloading of type cast operators  Here left-hand operand is always basic data type  Right-hand operand is always of class type
  • 9.
    10.9.2 Conversion fromClass type to Basic Data Type  During conversion , statement should satisfy following conditions :  The conversion function should not have any argument  Do not mention return type  It should be a class member function  Eg. Book
  • 10.
    10.9.3 Conversion fromOne Class type to Another Class Type When an object of one class is assigned to object of another class, important to give clear instructions to compiler about how to make conversion b/n 2 user-defined data type.
  • 11.
    10.10 Rules forOverloading Operators When an operator is overloaded ,its properties like syntax , precedence and associatively remain constant