SlideShare a Scribd company logo
2
Most read
3
Most read
13
Most read
Programming with Sikander
Programming with Sikander 1
 Most fundamental data types have pre-defined
operations associated with them.
 To make a user-defined data type as natural as a
fundamental data type, the appropriate set of operators
must be associated with it.
 C++ allows to define additional meaning to an existing
operator without changing its basic meaning.
 It is known as operator overloading.
Programming with Sikander 2
 To add two objects of the same class , a member function needs
to be invoked.
- obj3 = obj1.add(obj2)
 User have to remember functions names for all operations
 Understanding of operations will be more easier when we use
operators. Ie
 Obj3 = obj1 + obj2;
 The user can understand the operation more easily as compared to
the function call because it is closer to a real-life implementation.
 Thus by associating a set of meaningful operators, manipulation of
an ADT can be done in a conventional and simple form. Associating
operators with ADTs involves overloading the operators.
Programming with Sikander 3
 Only an expression containing a user defined type can have
overloaded operator.
 Only the predefined set of C++ operators can be overloaded, no
new operator can be introduced.
 By overloading, it is not possible to change the number of
arguments the operator take.
 By overloading, it is not possible to change the precedence of the
operators.
Programming with Sikander 4
 An operator can be overloaded by defining an operator function for it.
 Syntax of operator function
keyword symbol of the operator
 Return_type operator # (function arguments)
{
//definition of the function
}
 In most cases, the operator function can be either a member function
or a friend function
 However in a few cases, the operator function can be only a member
or only a friend as mandated
 In operator overloading, a single interface is again used for multiple
implementation and the binding is taking place during compile time.
 So operator overloading is considered as compile time polymorphism.
Programming with Sikander 5
 When a binary operator is overloaded as a member function, LHS
object acts as the invoking object and RHS object acts as the
argument. ie) the operator member function will take only a single
argument.
 When a binary operator is overloaded as global function, since
global function need not be invoked using any object, both the LHS
and RHS object will acts as arguments to the function. ie) the
operator global function will take two arguments.
Programming with Sikander 6
Operator function Unary operator Binary operator
As member function No Arguments One Argument
As global function One Argument Two Arguments
Programming with Sikander 7
Programming with Sikander 8
Programming with Sikander 9
Programming with Sikander 10
Programming with Sikander 11
 A unary operator is one which takes only a single operand.
 While overloading a unary operator as a member function, the
single operand will take up the responsibility of invoking the
function.
i.e., the operator member function will take no arguments.
 While overloading a unary operator as a global function, since the
function need not be invoked by an object, the single operand will
now act as an argument to this function.
i.e., the operator global function will take one argument.
Programming with Sikander 12
Programming with Sikander 13
Programming with Sikander 14
Programming with Sikander 15
 When you try to write the operator function of prefix and postfix operators
(++ / --), you can see two functions with the same name which is not
differing in terms of arguments. This violates the function overloading
principles.
 In-order to solve this problem, the operator function for postfix operations
should have a integer dummy argument.
 While overloading postfix operator as global function, the dummy
argument should always be the second argument.
 The compiler invokes the operator function without dummy argument for
the prefix application of the operator.
 The compiler invokes the operator function with int argument for the
postfix application of the operator.
Programming with Sikander 16
Programming with Sikander 17
 Operators which can be overloaded only as member functions are
 =
 [ ]
 ( )
 ->
Programming with Sikander 18
 The << (Insertion / put to) operator and >>(extraction / get from)
operator is already overloaded to work with fundamental data
types.
 Put to operator / insertion operator (<< ) always works with “cout”
which is a predefined object of class “ostream” and get from /
extraction operator (>>) always works with the “cin” which is a
predefined object of class “istream”.
 Since cin and cout are objects of istream and ostream respectively,
it cannot be used to invoke member functions of user’s class. i.e.,
overloading of these two operators are not possible with member
function.
 The only other possibility here is writing the operator function as a
global / friend function.
Programming with Sikander 19
 Syntax for overloading << and >>
 The arguments of ostream and istream should be taken by reference and
returned by reference. This is because, the copy constructor of istream
and ostream class is present in the non public section of the class.
Programming with Sikander 20
Programming with Sikander 21
 The following operators cannot be overloaded
 .
 .*
 : :
 sizeof
 ?:
Programming with Sikander 22
Programming with Sikander 23
Ad

Recommended

Need of object oriented programming
Need of object oriented programming
Amar Jukuntla
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
OOP Assignment 03.pdf
OOP Assignment 03.pdf
ARSLANMEHMOOD47
 
srs for railway reservation system
srs for railway reservation system
khushi kalaria
 
Function overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 
Microsoft Office Package: Practical Questions
Microsoft Office Package: Practical Questions
Makaha Rutendo
 
C++ string
C++ string
Dheenadayalan18
 
Implementing String
Implementing String
MohammedSadiq211406
 
Functions in C++
Functions in C++
Mohammed Sikander
 
OOP in C++
OOP in C++
ppd1961
 
Files in c++
Files in c++
Selvin Josy Bai Somu
 
Operator overloading
Operator overloading
Pranali Chaudhari
 
Inline function
Inline function
Tech_MX
 
Type casting in java
Type casting in java
Farooq Baloch
 
Exception handling c++
Exception handling c++
Jayant Dalvi
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1
Prof. Dr. K. Adisesha
 
classes and objects in C++
classes and objects in C++
HalaiHansaika
 
C++ decision making
C++ decision making
Zohaib Ahmed
 
What is Constructors and Destructors in C++ (Explained with Example along wi...
What is Constructors and Destructors in C++ (Explained with Example along wi...
Pallavi Seth
 
friend function(c++)
friend function(c++)
Ritika Sharma
 
Constants, Variables, and Data Types
Constants, Variables, and Data Types
Rokonuzzaman Rony
 
C++ Programming Language
C++ Programming Language
Mohamed Loey
 
File handling in c
File handling in c
aakanksha s
 
User Defined Functions
User Defined Functions
Praveen M Jigajinni
 
Input and output in C++
Input and output in C++
Nilesh Dalvi
 
Functions in c language
Functions in c language
tanmaymodi4
 
Types of Constructor in C++
Types of Constructor in C++
Bhavik Vashi
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
esuEthopi
 
Oops
Oops
ankush_kumar
 

More Related Content

What's hot (20)

Implementing String
Implementing String
MohammedSadiq211406
 
Functions in C++
Functions in C++
Mohammed Sikander
 
OOP in C++
OOP in C++
ppd1961
 
Files in c++
Files in c++
Selvin Josy Bai Somu
 
Operator overloading
Operator overloading
Pranali Chaudhari
 
Inline function
Inline function
Tech_MX
 
Type casting in java
Type casting in java
Farooq Baloch
 
Exception handling c++
Exception handling c++
Jayant Dalvi
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1
Prof. Dr. K. Adisesha
 
classes and objects in C++
classes and objects in C++
HalaiHansaika
 
C++ decision making
C++ decision making
Zohaib Ahmed
 
What is Constructors and Destructors in C++ (Explained with Example along wi...
What is Constructors and Destructors in C++ (Explained with Example along wi...
Pallavi Seth
 
friend function(c++)
friend function(c++)
Ritika Sharma
 
Constants, Variables, and Data Types
Constants, Variables, and Data Types
Rokonuzzaman Rony
 
C++ Programming Language
C++ Programming Language
Mohamed Loey
 
File handling in c
File handling in c
aakanksha s
 
User Defined Functions
User Defined Functions
Praveen M Jigajinni
 
Input and output in C++
Input and output in C++
Nilesh Dalvi
 
Functions in c language
Functions in c language
tanmaymodi4
 
Types of Constructor in C++
Types of Constructor in C++
Bhavik Vashi
 
OOP in C++
OOP in C++
ppd1961
 
Inline function
Inline function
Tech_MX
 
Type casting in java
Type casting in java
Farooq Baloch
 
Exception handling c++
Exception handling c++
Jayant Dalvi
 
classes and objects in C++
classes and objects in C++
HalaiHansaika
 
C++ decision making
C++ decision making
Zohaib Ahmed
 
What is Constructors and Destructors in C++ (Explained with Example along wi...
What is Constructors and Destructors in C++ (Explained with Example along wi...
Pallavi Seth
 
friend function(c++)
friend function(c++)
Ritika Sharma
 
Constants, Variables, and Data Types
Constants, Variables, and Data Types
Rokonuzzaman Rony
 
C++ Programming Language
C++ Programming Language
Mohamed Loey
 
File handling in c
File handling in c
aakanksha s
 
Input and output in C++
Input and output in C++
Nilesh Dalvi
 
Functions in c language
Functions in c language
tanmaymodi4
 
Types of Constructor in C++
Types of Constructor in C++
Bhavik Vashi
 

Similar to Operator Overloading in C++ (20)

Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
esuEthopi
 
Oops
Oops
ankush_kumar
 
Operator overloaing
Operator overloaing
zindadili
 
Operator overloading
Operator overloading
Kumar
 
Polymorphism and function overloading_new.ppt
Polymorphism and function overloading_new.ppt
ChetanyaChopra1
 
Oop05 6
Oop05 6
schwaa
 
Function overloading
Function overloading
Prof. Dr. K. Adisesha
 
Operator overloading
Operator overloading
ArunaDevi63
 
Operator overloading
Operator overloading
Garima Singh Makhija
 
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
drsomeshdewangan
 
Lec 28 - operator overloading
Lec 28 - operator overloading
Princess Sam
 
OOP_UnitIII.pdf
OOP_UnitIII.pdf
SuyogSabale1
 
C++ tutorial assignment - 23MTS5730.pptx
C++ tutorial assignment - 23MTS5730.pptx
sp1312004
 
Lec 26.27-operator overloading
Lec 26.27-operator overloading
Princess Sam
 
Function in C++
Function in C++
Prof Ansari
 
overloading in C++
overloading in C++
Prof Ansari
 
6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
OOPS-Seminar.pdf
OOPS-Seminar.pdf
Rithiga6
 
Chapter24 operator-overloading
Chapter24 operator-overloading
Deepak Singh
 
22 scheme OOPs with C++ BCS306B_module3.pdf
22 scheme OOPs with C++ BCS306B_module3.pdf
sindhus795217
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
esuEthopi
 
Operator overloaing
Operator overloaing
zindadili
 
Operator overloading
Operator overloading
Kumar
 
Polymorphism and function overloading_new.ppt
Polymorphism and function overloading_new.ppt
ChetanyaChopra1
 
Oop05 6
Oop05 6
schwaa
 
Operator overloading
Operator overloading
ArunaDevi63
 
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
drsomeshdewangan
 
Lec 28 - operator overloading
Lec 28 - operator overloading
Princess Sam
 
C++ tutorial assignment - 23MTS5730.pptx
C++ tutorial assignment - 23MTS5730.pptx
sp1312004
 
Lec 26.27-operator overloading
Lec 26.27-operator overloading
Princess Sam
 
overloading in C++
overloading in C++
Prof Ansari
 
6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
OOPS-Seminar.pdf
OOPS-Seminar.pdf
Rithiga6
 
Chapter24 operator-overloading
Chapter24 operator-overloading
Deepak Singh
 
22 scheme OOPs with C++ BCS306B_module3.pdf
22 scheme OOPs with C++ BCS306B_module3.pdf
sindhus795217
 
Ad

More from Mohammed Sikander (20)

Strings in C - covers string functions
Strings in C - covers string functions
Mohammed Sikander
 
Smart Pointers, Modern Memory Management Techniques
Smart Pointers, Modern Memory Management Techniques
Mohammed Sikander
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Mohammed Sikander
 
Python_Regular Expression
Python_Regular Expression
Mohammed Sikander
 
Modern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptx
Mohammed Sikander
 
Modern_cpp_auto.pdf
Modern_cpp_auto.pdf
Mohammed Sikander
 
Python Functions
Python Functions
Mohammed Sikander
 
Python dictionary
Python dictionary
Mohammed Sikander
 
Python exception handling
Python exception handling
Mohammed Sikander
 
Python tuple
Python tuple
Mohammed Sikander
 
Python strings
Python strings
Mohammed Sikander
 
Python set
Python set
Mohammed Sikander
 
Python list
Python list
Mohammed Sikander
 
Python Flow Control
Python Flow Control
Mohammed Sikander
 
Introduction to Python
Introduction to Python
Mohammed Sikander
 
Pointer basics
Pointer basics
Mohammed Sikander
 
Pipe
Pipe
Mohammed Sikander
 
Signal
Signal
Mohammed Sikander
 
File management
File management
Mohammed Sikander
 
Ad

Recently uploaded (20)

INDUCTIVE EFFECT slide for first prof pharamacy students
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
How to use search fetch method in Odoo 18
How to use search fetch method in Odoo 18
Celine George
 
How to Customize Quotation Layouts in Odoo 18
How to Customize Quotation Layouts in Odoo 18
Celine George
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Gladiolous Cultivation practices by AKL.pdf
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
Values Education 10 Quarter 1 Module .pptx
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
INDUCTIVE EFFECT slide for first prof pharamacy students
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
How to use search fetch method in Odoo 18
How to use search fetch method in Odoo 18
Celine George
 
How to Customize Quotation Layouts in Odoo 18
How to Customize Quotation Layouts in Odoo 18
Celine George
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Gladiolous Cultivation practices by AKL.pdf
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
Values Education 10 Quarter 1 Module .pptx
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 

Operator Overloading in C++

  • 2.  Most fundamental data types have pre-defined operations associated with them.  To make a user-defined data type as natural as a fundamental data type, the appropriate set of operators must be associated with it.  C++ allows to define additional meaning to an existing operator without changing its basic meaning.  It is known as operator overloading. Programming with Sikander 2
  • 3.  To add two objects of the same class , a member function needs to be invoked. - obj3 = obj1.add(obj2)  User have to remember functions names for all operations  Understanding of operations will be more easier when we use operators. Ie  Obj3 = obj1 + obj2;  The user can understand the operation more easily as compared to the function call because it is closer to a real-life implementation.  Thus by associating a set of meaningful operators, manipulation of an ADT can be done in a conventional and simple form. Associating operators with ADTs involves overloading the operators. Programming with Sikander 3
  • 4.  Only an expression containing a user defined type can have overloaded operator.  Only the predefined set of C++ operators can be overloaded, no new operator can be introduced.  By overloading, it is not possible to change the number of arguments the operator take.  By overloading, it is not possible to change the precedence of the operators. Programming with Sikander 4
  • 5.  An operator can be overloaded by defining an operator function for it.  Syntax of operator function keyword symbol of the operator  Return_type operator # (function arguments) { //definition of the function }  In most cases, the operator function can be either a member function or a friend function  However in a few cases, the operator function can be only a member or only a friend as mandated  In operator overloading, a single interface is again used for multiple implementation and the binding is taking place during compile time.  So operator overloading is considered as compile time polymorphism. Programming with Sikander 5
  • 6.  When a binary operator is overloaded as a member function, LHS object acts as the invoking object and RHS object acts as the argument. ie) the operator member function will take only a single argument.  When a binary operator is overloaded as global function, since global function need not be invoked using any object, both the LHS and RHS object will acts as arguments to the function. ie) the operator global function will take two arguments. Programming with Sikander 6
  • 7. Operator function Unary operator Binary operator As member function No Arguments One Argument As global function One Argument Two Arguments Programming with Sikander 7
  • 12.  A unary operator is one which takes only a single operand.  While overloading a unary operator as a member function, the single operand will take up the responsibility of invoking the function. i.e., the operator member function will take no arguments.  While overloading a unary operator as a global function, since the function need not be invoked by an object, the single operand will now act as an argument to this function. i.e., the operator global function will take one argument. Programming with Sikander 12
  • 16.  When you try to write the operator function of prefix and postfix operators (++ / --), you can see two functions with the same name which is not differing in terms of arguments. This violates the function overloading principles.  In-order to solve this problem, the operator function for postfix operations should have a integer dummy argument.  While overloading postfix operator as global function, the dummy argument should always be the second argument.  The compiler invokes the operator function without dummy argument for the prefix application of the operator.  The compiler invokes the operator function with int argument for the postfix application of the operator. Programming with Sikander 16
  • 18.  Operators which can be overloaded only as member functions are  =  [ ]  ( )  -> Programming with Sikander 18
  • 19.  The << (Insertion / put to) operator and >>(extraction / get from) operator is already overloaded to work with fundamental data types.  Put to operator / insertion operator (<< ) always works with “cout” which is a predefined object of class “ostream” and get from / extraction operator (>>) always works with the “cin” which is a predefined object of class “istream”.  Since cin and cout are objects of istream and ostream respectively, it cannot be used to invoke member functions of user’s class. i.e., overloading of these two operators are not possible with member function.  The only other possibility here is writing the operator function as a global / friend function. Programming with Sikander 19
  • 20.  Syntax for overloading << and >>  The arguments of ostream and istream should be taken by reference and returned by reference. This is because, the copy constructor of istream and ostream class is present in the non public section of the class. Programming with Sikander 20
  • 22.  The following operators cannot be overloaded  .  .*  : :  sizeof  ?: Programming with Sikander 22