SlideShare una empresa de Scribd logo
1 de 13
Operator Overloading- It is one of the interesting 
and useful feature of OOP. 
Definition - The ability to associate an existing operator 
with a member function and use it with objects of its 
classes as its operand is called operator overloading. 
Format of the member function using the operator keyword: 
return_ type operator op (arguments/parameters) 
Description of operator overloading- 
 Urinary operators. 
 Binary operators. 
o Arithmetic operators 
o Compound assignment operators 
o Comparison operators.
Unary operator- 
The unary operators operate on a single operand 
and following are the examples of Unary 
operators: 
The increment (++) and decrement (--) operator. 
The unary minus (-) operator. 
The logical not (!) operator. 
The unary operators operate on the object for 
which they were called and normally, this operator 
appears on the left side of the object, as in ‘!’obj , 
‘-’obj , and ‘++’obj but sometime they can be 
used as postfix as well like obj ‘++’ or obj ‘--’ .
/* Simple example to demonstrate the working of operator overloading*/ 
#include <iostream> 
using namespace std; 
class temp 
{ 
private: 
int count; 
public: 
temp():count(5) {} 
void operator +() 
{ count=count*2; } 
void Display() 
{ 
cout<<"Count: "<<count; 
}} 
; 
int main() 
{ 
temp t; 
+t; /* operator function void operator +() is called */ 
t.Display(); 
return 0; 
}
BBiinnaarryy ooppeerraattoorr-- 
BBiinnaarryy ooppeerraattoorrss aarree ooppeerraattoorrss wwiitthh ttwwoo ddiiffffeerreenntt 
ooppeerraannddss.. TThheeyy ccaann aallssoo bbee oovveerrllooaaddeedd jjuusstt lliikkee 
uunnaarryy ooppeerraattoorr.. 
TTwwoo wwaayyss ttoo oovveerrllooaadd aa bbiinnaarryy ooppeerraattoorr-- 
►AAss ffrriieenndd ffuunnccttiioonn tthheeyy ttaakkee ttwwoo aarrgguummeennttss-- 
ooppeerraattoorr ++ ((ssaammppllee oobbjj11,, ssaammppllee oobbjj22 )) 
-- OOnnee aarrgguummeenntt mmuusstt bbee ccllaassss oobbjjeecctt oorr rreeffeerreennccee ttoo aa 
ccllaassss oobbjjeecctt.. 
►AAss mmeemmbbeerr ffuunnccttiioonn tthheeyy ttaakkee oonnee aarrgguummeenntt-- 
ooppeerraattoorr ++ ((ssaammppllee oobbjj11))
11.. BBiinnaarryy AArriitthhmmeettiicc ooppeerraattoorr-- 
AArriitthhmmeettiicc ooppeerraattoorrss aarree bbiinnaarryy ooppeerraattoorr ssoo tthheeyy rreeqquuiirree ttwwoo 
ooppeerraannddss ttoo ppeerrffoorrmm tthhee ooppeerraattiioonnss.. 
SSaammppllee SSaammppllee :::: ooppeerraattoorr ++((ssaammppllee aa)) 
{{ 
SSaammppllee tteemmpp;; ////tteemmppoorraarryy oobbjjeecctt 
TTeemmpp..ccoouunntteerr==ccoouunntteerr++aa..ccoouunntteerr;; ////aaddddiittiioonn 
RReettuurrnn tteemmpp;; ////rreettuurrnn tteemmpp oobbjjeecctt 
}} 
NNooww wwee aarree aabbllee ttoo ppeerrffoorrmm tthhee aaddddiittiioonn ooff tthhee oobbjjeeccttss wwiitthh aa 
ssttaatteemmeenntt,, 
oobbjj33==oobbjj11++oobbjj22;; ////oobbjjeeccttss ooff ccllaassss ssaammppllee
22.. CCoommppoouunndd aassssiiggnnmmeenntt ooppeerraattoorr-- 
YYoouu ccaann oovveerrllooaadd tthhee aassssiiggnnmmeenntt ooppeerraattoorr ((==)) jjuusstt 
aass yyoouu ccaann ootthheerr ooppeerraattoorrss aanndd iitt ccaann bbee uusseedd ttoo 
ccrreeaattee aann oobbjjeecctt jjuusstt lliikkee tthhee ccooppyy ccoonnssttrruuccttoorr.. 
vvooiidd ssaammppllee :::: ooppeerraattoorr ++==((ssaammppllee aa)) 
{{ ccoouunntteerr ++== aa..ccoouunntteerr;; }} ////aassssiiggnnmmeenntt aanndd aaddddiittiioonn 
IInn tthhiiss eegg.. tthheerree iiss nnoo nneeeedd ffoorr aa tteemmpp.. oobbjjeecctt.. TThhee ffuunnccttiioonn 
aallssoo nneeeeddss nnoo rreettuurrnn vvaalluuee bbeeccaauussee tthhee rreessuulltt ooff tthhee 
aassssiiggnnmmeenntt ooppeerraattoorr iiss nnoott aassssiiggnneedd ttoo aannyytthhiinngg..
33.. CCoommppaarriissoonn ooppeerraattoorr-- 
CCoommppaarriissoonn aanndd llooggiiccaall ooppeerraattoorr aarree bbiinnaarryy 
ooppeerraattoorrss tthhaatt nneeeedd ttwwoo oobbjjeeccttss ttoo bbee 
CCoommppaarreedd.. TThhee ccoommppaarriissoonn ooppeerraattoorrss tthhaatt 
ccaann bbee oovveerrllooaaddeedd iinncclluuddee <<,,>>,,<<==,,>>==,,====,,!!==..
Overload able / non Overload able Operator 
Overload able operator- Non-overload able 
+ - / 
& | ! 
< > >= 
<< >> != 
+= -= %= 
|= *= >>= 
-> ->* new [ ] 
^ * % 
= ~ , 
-- <= ++ 
|| == && 
&= /= ^= 
() <<= [ ] 
delete [ ] new delete 
operator- 
:: .* 
. ?:
EExxaammppllee-- 
#include <iostream> 
class example 
{ 
public: 
int a; 
int b; 
example operator+(const example& obj); 
void operator=(const example& obj); 
}; 
void example::operator=(const example& obj) 
{ 
(*this).a = obj.a; 
(*this).b = obj.b; 
return; 
} 
Continued ....
example example::operator+(const example& obj2) 
{ 
example tmp_obj = *this; 
tmp_obj.a = tmp_obj.a + obj2.a; 
tmp_obj.b = tmp_obj.b + obj2.b; 
return tmp_obj; 
} 
int main(void) 
{ 
example obj1, obj2, obj3; 
obj1.a = 3; 
obj1.b = 2; 
obj2.a = 5; 
obj2.b = 4; 
obj3.a = 0; 
obj3.b = 0; 
obj3 = obj1 + obj2; 
std::cout<<obj3.a<<“n"<<obj3.b<<"n"; 
return 0; 
}
Rules for overloading operator- 
It looks simple to redefine the operators, 
but 
there are some restriction in over loading 
them. 
1. Only existing operators can be 
overloaded. 
2. One operand must be of user defined 
type. 
3. Syntax rule is same for both original 
operators and overloaded operators. 
4. Some operators cannot be overloaded. 
5. Friend function cannot be used to
6. Unary operator, overloaded by the member 
function take no explicit argument and return no 
value. however friend function require one 
argument 
7. Binary operators require one argument and 
those which are overloaded through a friend 
function require two arguments. 
8. When using a binary operator overloaded 
through the member function, the left hand 
operator must be an object of the relevant class. 
9. Binary operators such as +,-,* and / must 
return a value. They must not attempt to change 
there own argument.

Más contenido relacionado

La actualidad más candente

JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
Yasuharu Nakano
 
An example of bubble sort written in c
An example of bubble sort written in cAn example of bubble sort written in c
An example of bubble sort written in c
Sumedha
 
C++totural file
C++totural fileC++totural file
C++totural file
halaisumit
 
Reverse Engineering: C++ "for" operator
Reverse Engineering: C++ "for" operatorReverse Engineering: C++ "for" operator
Reverse Engineering: C++ "for" operator
erithion
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
웅식 전
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
Moriyoshi Koizumi
 
From OCaml To Javascript At Skydeck
From OCaml To Javascript At SkydeckFrom OCaml To Javascript At Skydeck
From OCaml To Javascript At Skydeck
Jake Donham
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
Richard Paul
 

La actualidad más candente (20)

Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
 
"let ECMAScript = 6"
"let ECMAScript = 6" "let ECMAScript = 6"
"let ECMAScript = 6"
 
What is recursion?
What is recursion? What is recursion?
What is recursion?
 
C++ tutorial
C++ tutorialC++ tutorial
C++ tutorial
 
Rcpp11 useR2014
Rcpp11 useR2014Rcpp11 useR2014
Rcpp11 useR2014
 
OOP in Rust
OOP in RustOOP in Rust
OOP in Rust
 
An example of bubble sort written in c
An example of bubble sort written in cAn example of bubble sort written in c
An example of bubble sort written in c
 
Python meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/OPython meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/O
 
C++totural file
C++totural fileC++totural file
C++totural file
 
Алексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhereАлексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhere
 
R/C++ talk at earl 2014
R/C++ talk at earl 2014R/C++ talk at earl 2014
R/C++ talk at earl 2014
 
Reverse Engineering: C++ "for" operator
Reverse Engineering: C++ "for" operatorReverse Engineering: C++ "for" operator
Reverse Engineering: C++ "for" operator
 
Primi passi con Project Tango
Primi passi con Project TangoPrimi passi con Project Tango
Primi passi con Project Tango
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
Class ‘increment’
Class ‘increment’Class ‘increment’
Class ‘increment’
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
Javascript OOP
Javascript OOPJavascript OOP
Javascript OOP
 
From OCaml To Javascript At Skydeck
From OCaml To Javascript At SkydeckFrom OCaml To Javascript At Skydeck
From OCaml To Javascript At Skydeck
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
 

Destacado

Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
preethalal
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
Princess Sam
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Kamal Acharya
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Kumar
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
Charndeep Sekhon
 

Destacado (16)

operator overloading
operator overloadingoperator overloading
operator overloading
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator 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
Operator overloadingOperator overloading
Operator overloading
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
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++
 
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 Overlaoding
Function OverlaodingFunction Overlaoding
Function Overlaoding
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
Function overloading
Function overloadingFunction overloading
Function overloading
 

Similar a Operator Overloading

李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义
yiditushe
 
Operator overloading in c++ is the most required.
Operator overloading in c++ is the most required.Operator overloading in c++ is the most required.
Operator overloading in c++ is the most required.
iammukesh1075
 

Similar a Operator Overloading (20)

Synapse india complain sharing info on chapter 8 operator overloading
Synapse india complain sharing info on chapter 8   operator overloadingSynapse india complain sharing info on chapter 8   operator overloading
Synapse india complain sharing info on chapter 8 operator overloading
 
ITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
ITGM #9 - Коварный CodeType, или от segfault'а к работающему кодуITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
ITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
 
Journey of a C# developer into Javascript
Journey of a C# developer into JavascriptJourney of a C# developer into Javascript
Journey of a C# developer into Javascript
 
李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义
 
4front en
4front en4front en
4front en
 
Коварный code type ITGM #9
Коварный code type ITGM #9Коварный code type ITGM #9
Коварный code type ITGM #9
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Time and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxTime and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptx
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
Asterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAsterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up Telephony
 
Zope component architechture
Zope component architechtureZope component architechture
Zope component architechture
 
OpenMP
OpenMPOpenMP
OpenMP
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cpp
 
Operator overloading in c++ is the most required.
Operator overloading in c++ is the most required.Operator overloading in c++ is the most required.
Operator overloading in c++ is the most required.
 
lecture12.ppt
lecture12.pptlecture12.ppt
lecture12.ppt
 
Enumerable
EnumerableEnumerable
Enumerable
 
Kotlin For Android - Constructors and Control Flow (part 2 of 7)
Kotlin For Android - Constructors and Control Flow (part 2 of 7)Kotlin For Android - Constructors and Control Flow (part 2 of 7)
Kotlin For Android - Constructors and Control Flow (part 2 of 7)
 

Último

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 

Último (20)

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 

Operator Overloading

  • 1.
  • 2. Operator Overloading- It is one of the interesting and useful feature of OOP. Definition - The ability to associate an existing operator with a member function and use it with objects of its classes as its operand is called operator overloading. Format of the member function using the operator keyword: return_ type operator op (arguments/parameters) Description of operator overloading-  Urinary operators.  Binary operators. o Arithmetic operators o Compound assignment operators o Comparison operators.
  • 3. Unary operator- The unary operators operate on a single operand and following are the examples of Unary operators: The increment (++) and decrement (--) operator. The unary minus (-) operator. The logical not (!) operator. The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object, as in ‘!’obj , ‘-’obj , and ‘++’obj but sometime they can be used as postfix as well like obj ‘++’ or obj ‘--’ .
  • 4. /* Simple example to demonstrate the working of operator overloading*/ #include <iostream> using namespace std; class temp { private: int count; public: temp():count(5) {} void operator +() { count=count*2; } void Display() { cout<<"Count: "<<count; }} ; int main() { temp t; +t; /* operator function void operator +() is called */ t.Display(); return 0; }
  • 5. BBiinnaarryy ooppeerraattoorr-- BBiinnaarryy ooppeerraattoorrss aarree ooppeerraattoorrss wwiitthh ttwwoo ddiiffffeerreenntt ooppeerraannddss.. TThheeyy ccaann aallssoo bbee oovveerrllooaaddeedd jjuusstt lliikkee uunnaarryy ooppeerraattoorr.. TTwwoo wwaayyss ttoo oovveerrllooaadd aa bbiinnaarryy ooppeerraattoorr-- ►AAss ffrriieenndd ffuunnccttiioonn tthheeyy ttaakkee ttwwoo aarrgguummeennttss-- ooppeerraattoorr ++ ((ssaammppllee oobbjj11,, ssaammppllee oobbjj22 )) -- OOnnee aarrgguummeenntt mmuusstt bbee ccllaassss oobbjjeecctt oorr rreeffeerreennccee ttoo aa ccllaassss oobbjjeecctt.. ►AAss mmeemmbbeerr ffuunnccttiioonn tthheeyy ttaakkee oonnee aarrgguummeenntt-- ooppeerraattoorr ++ ((ssaammppllee oobbjj11))
  • 6. 11.. BBiinnaarryy AArriitthhmmeettiicc ooppeerraattoorr-- AArriitthhmmeettiicc ooppeerraattoorrss aarree bbiinnaarryy ooppeerraattoorr ssoo tthheeyy rreeqquuiirree ttwwoo ooppeerraannddss ttoo ppeerrffoorrmm tthhee ooppeerraattiioonnss.. SSaammppllee SSaammppllee :::: ooppeerraattoorr ++((ssaammppllee aa)) {{ SSaammppllee tteemmpp;; ////tteemmppoorraarryy oobbjjeecctt TTeemmpp..ccoouunntteerr==ccoouunntteerr++aa..ccoouunntteerr;; ////aaddddiittiioonn RReettuurrnn tteemmpp;; ////rreettuurrnn tteemmpp oobbjjeecctt }} NNooww wwee aarree aabbllee ttoo ppeerrffoorrmm tthhee aaddddiittiioonn ooff tthhee oobbjjeeccttss wwiitthh aa ssttaatteemmeenntt,, oobbjj33==oobbjj11++oobbjj22;; ////oobbjjeeccttss ooff ccllaassss ssaammppllee
  • 7. 22.. CCoommppoouunndd aassssiiggnnmmeenntt ooppeerraattoorr-- YYoouu ccaann oovveerrllooaadd tthhee aassssiiggnnmmeenntt ooppeerraattoorr ((==)) jjuusstt aass yyoouu ccaann ootthheerr ooppeerraattoorrss aanndd iitt ccaann bbee uusseedd ttoo ccrreeaattee aann oobbjjeecctt jjuusstt lliikkee tthhee ccooppyy ccoonnssttrruuccttoorr.. vvooiidd ssaammppllee :::: ooppeerraattoorr ++==((ssaammppllee aa)) {{ ccoouunntteerr ++== aa..ccoouunntteerr;; }} ////aassssiiggnnmmeenntt aanndd aaddddiittiioonn IInn tthhiiss eegg.. tthheerree iiss nnoo nneeeedd ffoorr aa tteemmpp.. oobbjjeecctt.. TThhee ffuunnccttiioonn aallssoo nneeeeddss nnoo rreettuurrnn vvaalluuee bbeeccaauussee tthhee rreessuulltt ooff tthhee aassssiiggnnmmeenntt ooppeerraattoorr iiss nnoott aassssiiggnneedd ttoo aannyytthhiinngg..
  • 8. 33.. CCoommppaarriissoonn ooppeerraattoorr-- CCoommppaarriissoonn aanndd llooggiiccaall ooppeerraattoorr aarree bbiinnaarryy ooppeerraattoorrss tthhaatt nneeeedd ttwwoo oobbjjeeccttss ttoo bbee CCoommppaarreedd.. TThhee ccoommppaarriissoonn ooppeerraattoorrss tthhaatt ccaann bbee oovveerrllooaaddeedd iinncclluuddee <<,,>>,,<<==,,>>==,,====,,!!==..
  • 9. Overload able / non Overload able Operator Overload able operator- Non-overload able + - / & | ! < > >= << >> != += -= %= |= *= >>= -> ->* new [ ] ^ * % = ~ , -- <= ++ || == && &= /= ^= () <<= [ ] delete [ ] new delete operator- :: .* . ?:
  • 10. EExxaammppllee-- #include <iostream> class example { public: int a; int b; example operator+(const example& obj); void operator=(const example& obj); }; void example::operator=(const example& obj) { (*this).a = obj.a; (*this).b = obj.b; return; } Continued ....
  • 11. example example::operator+(const example& obj2) { example tmp_obj = *this; tmp_obj.a = tmp_obj.a + obj2.a; tmp_obj.b = tmp_obj.b + obj2.b; return tmp_obj; } int main(void) { example obj1, obj2, obj3; obj1.a = 3; obj1.b = 2; obj2.a = 5; obj2.b = 4; obj3.a = 0; obj3.b = 0; obj3 = obj1 + obj2; std::cout<<obj3.a<<“n"<<obj3.b<<"n"; return 0; }
  • 12. Rules for overloading operator- It looks simple to redefine the operators, but there are some restriction in over loading them. 1. Only existing operators can be overloaded. 2. One operand must be of user defined type. 3. Syntax rule is same for both original operators and overloaded operators. 4. Some operators cannot be overloaded. 5. Friend function cannot be used to
  • 13. 6. Unary operator, overloaded by the member function take no explicit argument and return no value. however friend function require one argument 7. Binary operators require one argument and those which are overloaded through a friend function require two arguments. 8. When using a binary operator overloaded through the member function, the left hand operator must be an object of the relevant class. 9. Binary operators such as +,-,* and / must return a value. They must not attempt to change there own argument.