SlideShare a Scribd company logo
5
Most read
6
Most read
8
Most read
Aruna Devi P.
Assistant Professor, Department of CS
Operator Overloading
 It Facilitates the use of Compile time Polymorphism
 When overloading the meaning of the operator should
not be changed.
 Allowing C++’s operators to work with user defined
data type such as objects
 Use all operators with user-defined objects
 Requires great care while overloading operators.
 Program will be difficult to understand
 Examples of already overloaded operators
 Operator << is both the stream-insertion operator and the
bitwise left-shift operator
 + and -, perform arithmetic on multiple types
 Compiler generates the appropriate code based on the
manner in which the operator is used
Overloading …
 Operator Overloading
 Function definition is written as usual
 Operator keyword is used in declaration followed by
the symbol for the operator being overloaded
 operator+ is used to overload the addition operator
(+)
 Using operators
 To use an operator on a class object it must be
overloaded first
 Assignment operator by default performs member wise
assignment
 Address operator (&) by default returns the address of an object
Restrictions on Operator Overloading
 C++ operators that can be overloaded
 C++ Operators that cannot be overloaded
Operators that cannot be overloaded
. .* :: ?: sizeof
Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
new[] delete[]
Restrictions on Operator Overloading
 Overloading restrictions
 Precedence of an operator cannot be changed
 Associativity of an operator cannot be changed
 Arity (number of operands) cannot be changed
 Unary operators remain unary, and binary operators remain
binary
 Operators &, *, + and - each have unary and binary versions
 Unary and binary versions can be overloaded separately
 No new operators can be created
 Use only existing operators
 No overloading operators for built-in types
 Cannot change how two integers are added
 Produces a syntax error
Operator Function
 Uses the Operator Keyword for function declaration
 Overloading the operator is done by statements in the
operator function definition.
 Operator function can be
 Member function
 Non member function as Friend
The way of defining the function differs for both the
method.
Syntax for Binary operator
overloading
 Member function
Return-type Class-name :: operator +(arglist)
{
//Definition of operator overloading
}
Argument list will have only one argument and other
argument is implicitly used when the function is called.
 Friend Function
Return-type operator +(arglist)
{
}
Argument list will have 2 argument for binary operator
overloading
Operator Functions as Class Members vs.
as friend Functions
 Member vs non-member
 Operator functions can be member or non-member
functions
 When overloading ( ), [ ], -> or any of the assignment
operators, it could be done with a member function
 Operator functions as member functions
 Leftmost operand must be an object (or reference to an
object) of the class
 If left operand of a different type, operator function must be a non-
member function
 Operator functions as non-member functions
 Must be friend if needs to access private or protected
members
 Enable the operator to be commutative
Overloading Stream-Insertion and
Stream-Extraction Operators
 Overloaded << and >> operators
 Overloaded to perform input/output for user-defined
types
 Left operand of types ostream & and istream &
 Must be a non-member function because left operand is
not an object of the class
 Must be a friend function to access private data
members
Overloading Unary Operators
 Operators that accept only one operand is unary
 Can be overloaded with no arguments or one argument
 No argument when it is a member function
 One argument when it is friend function or non member
function
 Should usually be implemented as member functions
 Avoid friend functions and classes because they violate the
encapsulation of a class
Syntax for Unary operator
overloading
 Member function
Return-type Class-name :: operator +()
{
//Definition of operator overloading
}
Argument list will be empty since the argument is implicit in
its call
 Friend Function
Return-type operator +(arglist)
{
}
Argument list will have 1 argument for unary operator
overloading
Conclusion
 Operator overloading is one of the feature of C++
that’s implements Compile time polymorphism.
 Overloading code should be written with greater care.
 Meaning of the operator should be preserved
 Use any of the two method for declaring the operator
function

More Related Content

PPTX
Polymorphism In c++
PDF
Constructor and Destructor
PDF
Exception handling
PPTX
Function overloading
PPTX
Method overloading
PPTX
Interface java
PPTX
Abstract Class & Abstract Method in Core Java
PPT
08 c++ Operator Overloading.ppt
Polymorphism In c++
Constructor and Destructor
Exception handling
Function overloading
Method overloading
Interface java
Abstract Class & Abstract Method in Core Java
08 c++ Operator Overloading.ppt

What's hot (20)

PPTX
Abstraction java
PPTX
Java exception handling
PPT
Java Programming for Designers
PPT
Operator Overloading
PPT
Function overloading(c++)
PPTX
Access modifiers in java
PPTX
Unary operator overloading
PPT
Introduction to method overloading &amp; method overriding in java hdm
PDF
Java threads
PPTX
07. Virtual Functions
PDF
Differences between c and c++
PPT
Scanner class
PPT
16 virtual function
PPT
Binary operator overloading
PPTX
Inline function
PPT
friend function(c++)
PPTX
Friend function & friend class
PPTX
Interface in java
PPSX
Exception Handling
PDF
Function overloading ppt
Abstraction java
Java exception handling
Java Programming for Designers
Operator Overloading
Function overloading(c++)
Access modifiers in java
Unary operator overloading
Introduction to method overloading &amp; method overriding in java hdm
Java threads
07. Virtual Functions
Differences between c and c++
Scanner class
16 virtual function
Binary operator overloading
Inline function
friend function(c++)
Friend function & friend class
Interface in java
Exception Handling
Function overloading ppt
Ad

Similar to Operator overloading (20)

PPTX
Operator overloaing
PPTX
Operator overloading
PDF
Lec 8.pdf a
PPTX
PPTX
Operator overloading
PPT
Lec 28 - operator overloading
PDF
OOPS-Seminar.pdf
PPTX
Operator overloading (binary)
PPTX
OPERATOR OVERLOADING IN C++
PPTX
Cpp (C++)
PPTX
Operator overloadng
PPTX
Presentation on overloading
PPT
OperatorOverloading.ppt
PPT
Lec 26.27-operator overloading
PDF
Operator overloading C++
PDF
Operator_Overloaing_Type_Conversion_OOPC(C++)
PDF
Operator Overloading in C++
PDF
NIKUL SURANI
PPT
C++_overloading.ppt
Operator overloaing
Operator overloading
Lec 8.pdf a
Operator overloading
Lec 28 - operator overloading
OOPS-Seminar.pdf
Operator overloading (binary)
OPERATOR OVERLOADING IN C++
Cpp (C++)
Operator overloadng
Presentation on overloading
OperatorOverloading.ppt
Lec 26.27-operator overloading
Operator overloading C++
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator Overloading in C++
NIKUL SURANI
C++_overloading.ppt
Ad

More from ArunaDevi63 (8)

PPTX
FuzzySet.pptx
PPT
Instructioncycle.ppt
PPTX
Asynchronous Data Transfer.pptx
PPTX
Mf formulation and parameterization
PPTX
Soft Computing
PPTX
Cookies in php
PPTX
Laws of boolean algebra
PPTX
Computer Organisation - Addressing Modes
FuzzySet.pptx
Instructioncycle.ppt
Asynchronous Data Transfer.pptx
Mf formulation and parameterization
Soft Computing
Cookies in php
Laws of boolean algebra
Computer Organisation - Addressing Modes

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Pre independence Education in Inndia.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Pharma ospi slides which help in ospi learning
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
RMMM.pdf make it easy to upload and study
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Cell Structure & Organelles in detailed.
Week 4 Term 3 Study Techniques revisited.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial disease of the cardiovascular and lymphatic systems
Pre independence Education in Inndia.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Renaissance Architecture: A Journey from Faith to Humanism
Pharma ospi slides which help in ospi learning
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
RMMM.pdf make it easy to upload and study
O5-L3 Freight Transport Ops (International) V1.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial diseases, their pathogenesis and prophylaxis
TR - Agricultural Crops Production NC III.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES

Operator overloading

  • 1. Aruna Devi P. Assistant Professor, Department of CS
  • 2. Operator Overloading  It Facilitates the use of Compile time Polymorphism  When overloading the meaning of the operator should not be changed.  Allowing C++’s operators to work with user defined data type such as objects  Use all operators with user-defined objects  Requires great care while overloading operators.  Program will be difficult to understand  Examples of already overloaded operators  Operator << is both the stream-insertion operator and the bitwise left-shift operator  + and -, perform arithmetic on multiple types  Compiler generates the appropriate code based on the manner in which the operator is used
  • 3. Overloading …  Operator Overloading  Function definition is written as usual  Operator keyword is used in declaration followed by the symbol for the operator being overloaded  operator+ is used to overload the addition operator (+)  Using operators  To use an operator on a class object it must be overloaded first  Assignment operator by default performs member wise assignment  Address operator (&) by default returns the address of an object
  • 4. Restrictions on Operator Overloading  C++ operators that can be overloaded  C++ Operators that cannot be overloaded Operators that cannot be overloaded . .* :: ?: sizeof Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[]
  • 5. Restrictions on Operator Overloading  Overloading restrictions  Precedence of an operator cannot be changed  Associativity of an operator cannot be changed  Arity (number of operands) cannot be changed  Unary operators remain unary, and binary operators remain binary  Operators &, *, + and - each have unary and binary versions  Unary and binary versions can be overloaded separately  No new operators can be created  Use only existing operators  No overloading operators for built-in types  Cannot change how two integers are added  Produces a syntax error
  • 6. Operator Function  Uses the Operator Keyword for function declaration  Overloading the operator is done by statements in the operator function definition.  Operator function can be  Member function  Non member function as Friend The way of defining the function differs for both the method.
  • 7. Syntax for Binary operator overloading  Member function Return-type Class-name :: operator +(arglist) { //Definition of operator overloading } Argument list will have only one argument and other argument is implicitly used when the function is called.  Friend Function Return-type operator +(arglist) { } Argument list will have 2 argument for binary operator overloading
  • 8. Operator Functions as Class Members vs. as friend Functions  Member vs non-member  Operator functions can be member or non-member functions  When overloading ( ), [ ], -> or any of the assignment operators, it could be done with a member function  Operator functions as member functions  Leftmost operand must be an object (or reference to an object) of the class  If left operand of a different type, operator function must be a non- member function  Operator functions as non-member functions  Must be friend if needs to access private or protected members  Enable the operator to be commutative
  • 9. Overloading Stream-Insertion and Stream-Extraction Operators  Overloaded << and >> operators  Overloaded to perform input/output for user-defined types  Left operand of types ostream & and istream &  Must be a non-member function because left operand is not an object of the class  Must be a friend function to access private data members
  • 10. Overloading Unary Operators  Operators that accept only one operand is unary  Can be overloaded with no arguments or one argument  No argument when it is a member function  One argument when it is friend function or non member function  Should usually be implemented as member functions  Avoid friend functions and classes because they violate the encapsulation of a class
  • 11. Syntax for Unary operator overloading  Member function Return-type Class-name :: operator +() { //Definition of operator overloading } Argument list will be empty since the argument is implicit in its call  Friend Function Return-type operator +(arglist) { } Argument list will have 1 argument for unary operator overloading
  • 12. Conclusion  Operator overloading is one of the feature of C++ that’s implements Compile time polymorphism.  Overloading code should be written with greater care.  Meaning of the operator should be preserved  Use any of the two method for declaring the operator function