SlideShare a Scribd company logo
1 of 9
Download to read offline
Operator
Overloading
C++
What is an operator?
An operator is a symbol which designed to
operate on data.
They can be a single symbol, di-graphs,
tri-graphs or keywords.
Operators can be classified in different ways.
Based on a number of
arguments
• Unary operators
• Binary operators
• Ternary operators
Based on the location
• prefix operators
• postfix operators
• infix operators
Operator overloading
This is similar to function overloading.
Remember:
Programmers are not allowed to change the
meaning of existing operator but can define on
a new domain.
(eg: cannot force “+” to substract int or float. But can introduce into
“String” which is a new domain)
C++ does not allow programmer to define completely new operator.
(eg: cannot define new operator “$#”)
Overloaded operator is treated as very specific function.
Implement operator
overloading
An operator function can be implemented in two different ways.
• As a member function of a class
(Implicitly assumed that an object of that class is one of the
required operator’s argument)
• As a standalone function
(Function must explicitly specify the types of all arguments)
Operator overloading will not increase either code efficiency or
reliability. But that may improve code readability.
Number of
arguments
Number of arguments are strictly restricted.
Key factors are
• location – (where the operator function is defined)
• operator – (overloading operator)
Keep in mind !
Don’t
• define new operators that is not exist
in C++
• change the priority
• Overload operators that working in
existing data types
Operators
Operator type Operator
Implementation
Return type
As global
function
As a member
function
Arithmetic operator + - * / % Yes Yes Depending on context
Bitwise operator ^| & ~ << >> Yes Yes Depending on context
Assignment operator = No Yes
Reference to an object
or l-value
Relational operator
== != > >= <=
&& ||
Yes Yes Boolean
! Yes Yes -
Compound
assignment operator
+= -= *= %= /= &=
|= ^= >>= <<=
No Yes
Reference to an object
or l-value
Prefix increment and
decrement
++ -- No Yes
Reference to an object
or l-value
Postfix increment and
decrement
++ -- No Yes
Reference to an object
or l-value
Subscript operator [] No Yes
Reference to an object
or l-value
Function invocation
operator
() No Yes Any
& * Yes Yes Any
Non-overloaded
operators
?:
.
::
sizeof
Other overloaded
operators
,
->
new
new[]
delete
delete[]
typename
This is an introduction for operator
overloading. It is better to try these
things and feel free to experiment
with them on your own.
Lahiru Dilshan

More Related Content

What's hot (20)

Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Association agggregation and composition
Association agggregation and compositionAssociation agggregation and composition
Association agggregation and composition
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Java constructors
Java constructorsJava constructors
Java constructors
 
C functions
C functionsC functions
C functions
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Data types in java
Data types in javaData types in java
Data types in java
 

Similar to Operator Overloading in C++ - Understanding What Operators Are and How to Overload Them

Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)Tirthika Bandi
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdfRithiga6
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingArunaDevi63
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaingzindadili
 
Programming course slides c++ (Prof Mansoor Bhatti)
Programming course slides c++ (Prof Mansoor Bhatti)Programming course slides c++ (Prof Mansoor Bhatti)
Programming course slides c++ (Prof Mansoor Bhatti)Syed Arslan Rizvi
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadngpreethalal
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
OperatorOverloading.ppt
OperatorOverloading.pptOperatorOverloading.ppt
OperatorOverloading.pptasadmujtaba001
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANINikul4470
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 

Similar to Operator Overloading in C++ - Understanding What Operators Are and How to Overload Them (20)

Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
c++
c++c++
c++
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Oops
OopsOops
Oops
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Programming course slides c++ (Prof Mansoor Bhatti)
Programming course slides c++ (Prof Mansoor Bhatti)Programming course slides c++ (Prof Mansoor Bhatti)
Programming course slides c++ (Prof Mansoor Bhatti)
 
Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
OperatorOverloading.ppt
OperatorOverloading.pptOperatorOverloading.ppt
OperatorOverloading.ppt
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 

More from Lahiru Dilshan

"Capture" in lambda expression.
"Capture" in lambda expression."Capture" in lambda expression.
"Capture" in lambda expression.Lahiru Dilshan
 
CAD vs CAM vs CAE software.pdf
CAD vs CAM vs CAE software.pdfCAD vs CAM vs CAE software.pdf
CAD vs CAM vs CAE software.pdfLahiru Dilshan
 
Degeneracies in 3D modeling.pdf
Degeneracies in 3D modeling.pdfDegeneracies in 3D modeling.pdf
Degeneracies in 3D modeling.pdfLahiru Dilshan
 
What does Buffer in C++ means.pdf
What does Buffer in C++ means.pdfWhat does Buffer in C++ means.pdf
What does Buffer in C++ means.pdfLahiru Dilshan
 
Open CASCADE for your project.pdf
Open CASCADE for your project.pdfOpen CASCADE for your project.pdf
Open CASCADE for your project.pdfLahiru Dilshan
 
Linkage mechanisms - Presentation
Linkage mechanisms - PresentationLinkage mechanisms - Presentation
Linkage mechanisms - PresentationLahiru Dilshan
 
Industrial Training Experience
Industrial Training ExperienceIndustrial Training Experience
Industrial Training ExperienceLahiru Dilshan
 
Small scale business analysis
Small scale business analysisSmall scale business analysis
Small scale business analysisLahiru Dilshan
 
Computational and experimental investigation of aerodynamics of flapping aero...
Computational and experimental investigation of aerodynamics of flapping aero...Computational and experimental investigation of aerodynamics of flapping aero...
Computational and experimental investigation of aerodynamics of flapping aero...Lahiru Dilshan
 
Experimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structureExperimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structureLahiru Dilshan
 
Experimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structureExperimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structureLahiru Dilshan
 
Transient three dimensional cfd modelling of ceilng fan
Transient three dimensional cfd modelling of ceilng fanTransient three dimensional cfd modelling of ceilng fan
Transient three dimensional cfd modelling of ceilng fanLahiru Dilshan
 
Payload safety and related human factors
Payload safety and related human factorsPayload safety and related human factors
Payload safety and related human factorsLahiru Dilshan
 
Human factors consideration in emergency evacuation for commercial aircaft
Human factors consideration in emergency evacuation for commercial aircaftHuman factors consideration in emergency evacuation for commercial aircaft
Human factors consideration in emergency evacuation for commercial aircaftLahiru Dilshan
 
Human factors in payload safety of fighter aircrafts
Human factors in payload safety of fighter aircraftsHuman factors in payload safety of fighter aircrafts
Human factors in payload safety of fighter aircraftsLahiru Dilshan
 
HUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONS
HUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONSHUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONS
HUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONSLahiru Dilshan
 
Human factors - Maintenance and inspection
Human factors - Maintenance and inspectionHuman factors - Maintenance and inspection
Human factors - Maintenance and inspectionLahiru Dilshan
 
Fire safety of passenger aircraft
Fire safety of passenger aircraftFire safety of passenger aircraft
Fire safety of passenger aircraftLahiru Dilshan
 
Displays and controls arrangement of military aircraft
Displays and controls arrangement of military aircraftDisplays and controls arrangement of military aircraft
Displays and controls arrangement of military aircraftLahiru Dilshan
 
Considerations of human factors on commercial aircraft
Considerations of human factors on commercial aircraftConsiderations of human factors on commercial aircraft
Considerations of human factors on commercial aircraftLahiru Dilshan
 

More from Lahiru Dilshan (20)

"Capture" in lambda expression.
"Capture" in lambda expression."Capture" in lambda expression.
"Capture" in lambda expression.
 
CAD vs CAM vs CAE software.pdf
CAD vs CAM vs CAE software.pdfCAD vs CAM vs CAE software.pdf
CAD vs CAM vs CAE software.pdf
 
Degeneracies in 3D modeling.pdf
Degeneracies in 3D modeling.pdfDegeneracies in 3D modeling.pdf
Degeneracies in 3D modeling.pdf
 
What does Buffer in C++ means.pdf
What does Buffer in C++ means.pdfWhat does Buffer in C++ means.pdf
What does Buffer in C++ means.pdf
 
Open CASCADE for your project.pdf
Open CASCADE for your project.pdfOpen CASCADE for your project.pdf
Open CASCADE for your project.pdf
 
Linkage mechanisms - Presentation
Linkage mechanisms - PresentationLinkage mechanisms - Presentation
Linkage mechanisms - Presentation
 
Industrial Training Experience
Industrial Training ExperienceIndustrial Training Experience
Industrial Training Experience
 
Small scale business analysis
Small scale business analysisSmall scale business analysis
Small scale business analysis
 
Computational and experimental investigation of aerodynamics of flapping aero...
Computational and experimental investigation of aerodynamics of flapping aero...Computational and experimental investigation of aerodynamics of flapping aero...
Computational and experimental investigation of aerodynamics of flapping aero...
 
Experimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structureExperimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structure
 
Experimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structureExperimental and numerical stress analysis of a rectangular wing structure
Experimental and numerical stress analysis of a rectangular wing structure
 
Transient three dimensional cfd modelling of ceilng fan
Transient three dimensional cfd modelling of ceilng fanTransient three dimensional cfd modelling of ceilng fan
Transient three dimensional cfd modelling of ceilng fan
 
Payload safety and related human factors
Payload safety and related human factorsPayload safety and related human factors
Payload safety and related human factors
 
Human factors consideration in emergency evacuation for commercial aircaft
Human factors consideration in emergency evacuation for commercial aircaftHuman factors consideration in emergency evacuation for commercial aircaft
Human factors consideration in emergency evacuation for commercial aircaft
 
Human factors in payload safety of fighter aircrafts
Human factors in payload safety of fighter aircraftsHuman factors in payload safety of fighter aircrafts
Human factors in payload safety of fighter aircrafts
 
HUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONS
HUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONSHUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONS
HUMAN FACTOR CONSIDERATIONS IN MILITARY AIRCRAFT MAINTENANCE AND INSPECTIONS
 
Human factors - Maintenance and inspection
Human factors - Maintenance and inspectionHuman factors - Maintenance and inspection
Human factors - Maintenance and inspection
 
Fire safety of passenger aircraft
Fire safety of passenger aircraftFire safety of passenger aircraft
Fire safety of passenger aircraft
 
Displays and controls arrangement of military aircraft
Displays and controls arrangement of military aircraftDisplays and controls arrangement of military aircraft
Displays and controls arrangement of military aircraft
 
Considerations of human factors on commercial aircraft
Considerations of human factors on commercial aircraftConsiderations of human factors on commercial aircraft
Considerations of human factors on commercial aircraft
 

Recently uploaded

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 

Recently uploaded (20)

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 

Operator Overloading in C++ - Understanding What Operators Are and How to Overload Them

  • 2. What is an operator? An operator is a symbol which designed to operate on data. They can be a single symbol, di-graphs, tri-graphs or keywords. Operators can be classified in different ways. Based on a number of arguments • Unary operators • Binary operators • Ternary operators Based on the location • prefix operators • postfix operators • infix operators
  • 3. Operator overloading This is similar to function overloading. Remember: Programmers are not allowed to change the meaning of existing operator but can define on a new domain. (eg: cannot force “+” to substract int or float. But can introduce into “String” which is a new domain) C++ does not allow programmer to define completely new operator. (eg: cannot define new operator “$#”) Overloaded operator is treated as very specific function.
  • 4. Implement operator overloading An operator function can be implemented in two different ways. • As a member function of a class (Implicitly assumed that an object of that class is one of the required operator’s argument) • As a standalone function (Function must explicitly specify the types of all arguments) Operator overloading will not increase either code efficiency or reliability. But that may improve code readability.
  • 5. Number of arguments Number of arguments are strictly restricted. Key factors are • location – (where the operator function is defined) • operator – (overloading operator)
  • 6. Keep in mind ! Don’t • define new operators that is not exist in C++ • change the priority • Overload operators that working in existing data types
  • 7. Operators Operator type Operator Implementation Return type As global function As a member function Arithmetic operator + - * / % Yes Yes Depending on context Bitwise operator ^| & ~ << >> Yes Yes Depending on context Assignment operator = No Yes Reference to an object or l-value Relational operator == != > >= <= && || Yes Yes Boolean ! Yes Yes - Compound assignment operator += -= *= %= /= &= |= ^= >>= <<= No Yes Reference to an object or l-value Prefix increment and decrement ++ -- No Yes Reference to an object or l-value Postfix increment and decrement ++ -- No Yes Reference to an object or l-value Subscript operator [] No Yes Reference to an object or l-value Function invocation operator () No Yes Any & * Yes Yes Any
  • 9. This is an introduction for operator overloading. It is better to try these things and feel free to experiment with them on your own. Lahiru Dilshan