Professional Documents
Culture Documents
C++ Operator Overloading
C++ Operator Overloading
Operator Overloading
• Examples:
– << bitwise left-shift operator and stream insertion
operator
– >> bitwise right-shift operator and stream extraction
operator
– + & - overloaded to perform differently depending on
their context in integer, floating-point arithmetic and
pointer arithmetic
• Prototype:
Return type operator<operator-to-be-overloaded>(parameter
list)
{ …}
• //array example
• Example declarations:
Date d1;
Pre-incrementing
Member function:
++d1 = d1.operator(); Prototype Date operator ++()
Non-member function:
++d1 = operator(d1); Prototypefriend Date operator++(Date
&)
Post-incrementing
Member function:
d1++ = d1.operator(0); Prototype Date operator ++(int)
Non-member function:
d1 ++= d1.operator(d1,0); Prototypefriend Date operator++
(Date &,int)
Overloading ++ and -- …
• Similar is the case for pre-and post decrementing
• http://www.cplusplus-naturally.
blogspot.com
• http://zainvi.sf.googlepages.co
m/index.htm
• http://www.computer-science-n
otes.blogspot.com
u
Y o
a nk
Th
? ns
s tio s ?
ue t ion
Q es
ugg
S