SlideShare a Scribd company logo
1 of 16
OOP WITH C++
NAME: AABHA.A.TIWARI
BRANCH: COMPUTER ENGINEERING
TOPIC: OPERATOR OVERLOADING
CONTENTS:
• Introduction
• Concept of Operator Overloading
• Syntax
• Example
• Types
• Rules for Overloading Operators
INTRODUCTION:
• An overloaded declaration is a declaration that has been
declared with the same name as a previously declared
declaration in the same scope, except that both declarations
have different arguments and obviously different definition
i.e. implementation.
• When you call an overloaded function or operator, the
compiler determines the most appropriate definition to use
by comparing the argument types you used to call the
function or operator with the parameter types specified in the
definitions. The process of selecting the most appropriate
overloaded function or operator is called overload resolution.
• Each C++ operator has a predefined meaning. Operator
Overloading is a type of polymorphism in which an operator is
overloaded to give user defined meaning to it or say to give an
additional meaning to it.
• For example, + operator can be overloaded to perform an
operation of string concatenation along with its pre-defined
job of adding two numeric values.
• In simple words, overloading an operator means assigning
additional operation or job to it; relative to a specific class.
• When an operator is overloaded, none of its original meaning
will be lost.
SYNTAX:
• Overloaded operators are functions with special names the
keyword operator followed by the symbol for the operator
being defined. Like any other function, an overloaded
operator has a return type and a parameter list.
CONSIDER FOLLOWING PIECE OF CODE. CONSIDERING THE +
OPERATOR IS OVERLOADED IN SUCH A WAY WITH CLASS STRING
THAT IT WILL CONCATENATE TWO STRING OBJECTS :
int main()
{
int n1 = 11
2, n2 = 23
4, n3;
String s1 = “Hello”, s2=“World”, s3;
n3 = n1 + n2; // addition
s3 = s1 + s2; // concatenation
}
RULES FOR OVERLOADING OPERATORS:
• Only existing operators can be overloaded.New operators cannot be
created.
• The overloaded operator must have at least one operand that is of user-
defined type.
• We cannot change the basic meaning of an operator.
• You cannot change any operators precedence.
• Overloaded operators follow the syntax rules of the original operators.
• Binary operators overloaded through a member function take one explicit
argument and those which are overloaded through a friend function take
two explicit arguments.
• When using binary operators overloaded through a member function, the
left hand operand must be an object of the relevant class.
• Binary arithmetic operators such as +, -, * and / must explicitly return a
value. They must not attempt to change their own arguments.
TYPES OF OPERATORS:
• Unary (++, --)
• Binary (+, -)
• Ternary (?:)
You are free to overload most of the built-in operators, but you cannot
create new operators of your own.Therefore, although you can give your
class an increment operator (++), you cannot create a squared
operator.You can overload these operators to do anything you want, but it
is a good programming practice for them to make sense.That means you
can have the ++ operator to decrement, but it would make no sense to do
so.
OPERATOR FUNCTIONS:
• Operators are overloaded by creating operator functions.
• An operator function defines the operations that the overloaded operator
will perform relative to the class upon which it will work.
• An operator function is created using the keyword operator.
• Operator functions can be either
I. Member Functions of a class
II. Friend Functions of a class
• The way operator functions are written differs between member and non-
member functions.
• You can overload most of Unary and Binary Operators, but you cannot
overload the ternary operator (?:).Binary as well as Unary operators can be
overloaded by both approaches namely Member Functions Approach and
Friend Function Approach.
A MEMBER OPERATOR FUNCTION FOR BINARY OPERATORS:
• RetType ClassName::operator#(arg-list)
{
// operations
}
• For example, if you are overloading the + operator, use
operator+.
• While overloading binary operators using member function,
thearg-list will contain one parameter.
A MEMBER OPERATOR FUNCTION FOR UNARY OPERATORS:
RetType ClassName::operator#()
{
// operations
}
• While overloading an unary operator, arg-list will be empty.
A FRIEND OPERATOR FUNCTION FOR BINARY OPERATORS:
RetType operator#(arg-list)
{
// operations
}
• While overloading a binary operator using friend function;
argument list must take 2 arguments, one of them must be
of userdefined type.
A FRIEND OPERATOR FUNCTION FOR UNARY OPERATORS:
RetType operator#(arg-list)
{
// operations
}
• While overloading an unary operator using friend function;
argument list must have 1 argument as reference to the
object.
DIFFERENCE BETWEEN MEMBER FUNCTION AND FRIEND
FUNCTION:
• A basic difference between them is that a friend function will
have only one argument for unary operators and two for
binary operators,while a member function has no arguments
for unary operators and only one for binary operators.
• The friend function takes one argument more than member
function because the invoking appears as an explicit
parameter to the friend function whereas in member
functions it is passed as an implicit parameter.
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++

More Related Content

What's hot

classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingArunaDevi63
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversionsAmogh Kalyanshetti
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract classAmit Trivedi
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Friend function
Friend functionFriend function
Friend functionzindadili
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++Sujan Mia
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpprajshreemuthiah
 

What's hot (20)

classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Friend function
Friend functionFriend function
Friend function
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Method overriding
Method overridingMethod overriding
Method overriding
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Inline function
Inline functionInline function
Inline function
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
 

Viewers also liked

operator overloading in c++
operator overloading in c++operator overloading in c++
operator overloading in c++harman kaur
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadngpreethalal
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKamal Acharya
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++Ilio Catallo
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++gourav kottawar
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloadingHaresh Jaiswal
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++Danial Mirza
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingRai University
 
Type conversions
Type conversionsType conversions
Type conversionssanya6900
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritanceshatha00
 

Viewers also liked (20)

operator overloading in c++
operator overloading in c++operator overloading in c++
operator overloading in c++
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Function Overlaoding
Function OverlaodingFunction Overlaoding
Function Overlaoding
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
 
polymorphism
polymorphism polymorphism
polymorphism
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Type conversions
Type conversionsType conversions
Type conversions
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 

Similar to OPERATOR OVERLOADING IN C++

Similar to OPERATOR OVERLOADING IN C++ (20)

c++
c++c++
c++
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
 
Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Oops
OopsOops
Oops
 
Operator Overloading & Function Overloading
Operator Overloading & Function OverloadingOperator Overloading & Function Overloading
Operator Overloading & Function Overloading
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
OOP_UnitIII.pdf
OOP_UnitIII.pdfOOP_UnitIII.pdf
OOP_UnitIII.pdf
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
B.sc CSIT 2nd semester C++ Unit4
B.sc CSIT  2nd semester C++ Unit4B.sc CSIT  2nd semester C++ Unit4
B.sc CSIT 2nd semester C++ Unit4
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3
 

Recently uploaded

Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257subhasishdas79
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxNANDHAKUMARA10
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...josephjonse
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptAfnanAhmad53
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...ssuserdfc773
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...ronahami
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesRashidFaridChishti
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information SystemsAnge Felix NSANZIYERA
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesChandrakantDivate1
 

Recently uploaded (20)

Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .ppt
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 

OPERATOR OVERLOADING IN C++

  • 1. OOP WITH C++ NAME: AABHA.A.TIWARI BRANCH: COMPUTER ENGINEERING TOPIC: OPERATOR OVERLOADING
  • 2. CONTENTS: • Introduction • Concept of Operator Overloading • Syntax • Example • Types • Rules for Overloading Operators
  • 3. INTRODUCTION: • An overloaded declaration is a declaration that has been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition i.e. implementation. • When you call an overloaded function or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.
  • 4. • Each C++ operator has a predefined meaning. Operator Overloading is a type of polymorphism in which an operator is overloaded to give user defined meaning to it or say to give an additional meaning to it. • For example, + operator can be overloaded to perform an operation of string concatenation along with its pre-defined job of adding two numeric values. • In simple words, overloading an operator means assigning additional operation or job to it; relative to a specific class. • When an operator is overloaded, none of its original meaning will be lost.
  • 5. SYNTAX: • Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.
  • 6. CONSIDER FOLLOWING PIECE OF CODE. CONSIDERING THE + OPERATOR IS OVERLOADED IN SUCH A WAY WITH CLASS STRING THAT IT WILL CONCATENATE TWO STRING OBJECTS : int main() { int n1 = 11 2, n2 = 23 4, n3; String s1 = “Hello”, s2=“World”, s3; n3 = n1 + n2; // addition s3 = s1 + s2; // concatenation }
  • 7. RULES FOR OVERLOADING OPERATORS: • Only existing operators can be overloaded.New operators cannot be created. • The overloaded operator must have at least one operand that is of user- defined type. • We cannot change the basic meaning of an operator. • You cannot change any operators precedence. • Overloaded operators follow the syntax rules of the original operators. • Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments. • When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class. • Binary arithmetic operators such as +, -, * and / must explicitly return a value. They must not attempt to change their own arguments.
  • 8. TYPES OF OPERATORS: • Unary (++, --) • Binary (+, -) • Ternary (?:) You are free to overload most of the built-in operators, but you cannot create new operators of your own.Therefore, although you can give your class an increment operator (++), you cannot create a squared operator.You can overload these operators to do anything you want, but it is a good programming practice for them to make sense.That means you can have the ++ operator to decrement, but it would make no sense to do so.
  • 9. OPERATOR FUNCTIONS: • Operators are overloaded by creating operator functions. • An operator function defines the operations that the overloaded operator will perform relative to the class upon which it will work. • An operator function is created using the keyword operator. • Operator functions can be either I. Member Functions of a class II. Friend Functions of a class • The way operator functions are written differs between member and non- member functions. • You can overload most of Unary and Binary Operators, but you cannot overload the ternary operator (?:).Binary as well as Unary operators can be overloaded by both approaches namely Member Functions Approach and Friend Function Approach.
  • 10. A MEMBER OPERATOR FUNCTION FOR BINARY OPERATORS: • RetType ClassName::operator#(arg-list) { // operations } • For example, if you are overloading the + operator, use operator+. • While overloading binary operators using member function, thearg-list will contain one parameter.
  • 11. A MEMBER OPERATOR FUNCTION FOR UNARY OPERATORS: RetType ClassName::operator#() { // operations } • While overloading an unary operator, arg-list will be empty.
  • 12. A FRIEND OPERATOR FUNCTION FOR BINARY OPERATORS: RetType operator#(arg-list) { // operations } • While overloading a binary operator using friend function; argument list must take 2 arguments, one of them must be of userdefined type.
  • 13. A FRIEND OPERATOR FUNCTION FOR UNARY OPERATORS: RetType operator#(arg-list) { // operations } • While overloading an unary operator using friend function; argument list must have 1 argument as reference to the object.
  • 14. DIFFERENCE BETWEEN MEMBER FUNCTION AND FRIEND FUNCTION: • A basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators,while a member function has no arguments for unary operators and only one for binary operators. • The friend function takes one argument more than member function because the invoking appears as an explicit parameter to the friend function whereas in member functions it is passed as an implicit parameter.