SN Operator Overloading in C++ II 2023
SN Operator Overloading in C++ II 2023
“Operator overloading is just syntactic sugar, which means it is simply another way for a
user to make a function call” – Bruce Eckel (Thinking in C++, Vol 1)
Recap……
Many of the existing C++ operators are already overloaded for built-in datatypes;
Overload means assign multiple responsibilities based on the context
matrix p, q, r;
p = q.AddMatrix(q); p = q + r; Easier to read
r = p.InvMatrix(); r = ~p;
cout<<p<<q<<r;
• To make it possible
The behavior of the operator function need to be defined in the class definition
a binary operator function takes one argument (the 2 nd operand) when implemented as member function
a binary operator function takes two arguments (1 st operand and 2nd operand) when implemented as friend function
class complex
{ int main(void)
float rl, img; // private members {
public: complex a, b(2,3.5),d;
float arg, amp;
complex(float f1=1.0,float f2=1.0) {..} // subtracting two complex objects
~complex() {}
// other required methods……… d = a.SubComplex ( b );
• An unary operator function takes either no arguments (member function) or one argument (friend function)
• Overloading increment (++) and decrement (--) operators
Overloading Operator Function Options Operator function call
Syntax name
++a operator++ i) Member function a.operator++()
ii) Friend function operator++(a);
a++ operator++ i) Member function a.operator++(int)
dummy int argument is
passed to post-inc/dec to
ii) Friend function v int);
operator++(a,
distinguish it from
--a operator- - i) Member function a.operator- -()
pre-inc/dec case
ii) Friend function operator- -(a);
a-- operator- - i) Member function a.operator- -(int)
ii) Friend function operator-v-(a, int);
Overloadable operators…
and -> to be members. It seemed a harmless restriction that eliminated the possibility of some obscure errors because these operators invariably depend on and typically modify the state of their left-
• Except existing operators no new operators like +-, ^^, ^% can be overloaded
• Operators , || && when overloaded losses their special properties (short-circuit evaluation and sequencing); better to avoid
overloading them
• The new and delete operators can also be overloaded like other operators in C++
complex a, b;
cout<<a;
• Options
(i) member function - cout.operator<<(a) // NOT possible to include operator<< in ostream class
(ii) friend function - operator<<(cout,a) // ONLY OPTION is to implement a friend operator function
class complex
{ int main(void)
{
float rl, img;
public: int x, y=2;
float arg, amp;
complex(float f1=1.0,float f2=1.0) {..} x=y;
~complex() {}
float a, b=3.0;
{
rl=f1; img=f2; a=b;
ptr = new float(ri+img); // dy alloc from heap }
}
~complex()
{ return 0;
delete ptr; // deloc from heap }
}
{
rl=f1; img=f2; a=b;
ptr = new float(ri+img); // dy alloc from heap }
}
Here, in absence of operloaded
~complex() behavior of =; Shallow bitwise
{ return 0; copying has been performed by
delete ptr; // deloc from heap } the functionality provided by
} compiler
• If dynamic allocation is used in constructor or the class is having a pointer/reference member overloading = is essential to
implement deep copy
• Deep copying – allocates new (fresh) space for the contents pointed and then copies the original pointed data to it
• Shallow copying – merely copies the value of the pointer member; hence the new and original pointer continues to point to
the same data; if one modified some of its pointed portion the other gets affected; if one goes out of scope the other
becomes a dangling pointer
• If copy constructor is needed (when the class is having atleast one reference/pointer member) one must overload operator =
explicitly to enable deep copying and vice-versa