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.

Operator Overloading

  • 2.
    Operator Overloading- Itis 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- Theunary 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 exampleto 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-- BBiinnaarryyooppeerraattoorrss 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 AArriitthhmmeettiiccooppeerraattoorr-- 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 aassssiiggnnmmeennttooppeerraattoorr-- 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 overloadingoperator- 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.