Operator
Overloading
Operator Overloading- 2
Introduction
Use operators with objects (operator overloading)
Clearer than function calls for certain classes
Operator sensitive to context
Examples
<<
Stream insertion, bitwise left-shift
+
Performs arithmetic on multiple types (integers, floats, etc.)
Introduction 3
Types
Built in (int, char) or user-defined
Can use existing operators with user-defined types
Cannot create new operators
Definition: Operator overloading means to redefine the
standard operators so that they can also perform
operations on classes as in fundamental types.
Introduction 4
Overloading operators
Create a function for the class
Name function operator followed by symbol
Return_type operator op_sign(list of arg/s);
Operator+ is the ‘operator function’ for the addition operator +
Overloading provides concise notation
object2 = object1.add(object2);
object2 = object2 + object1;
Restrictions 5
Operators that cannot be overloaded
. .* :: ?: sizeof
Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
new[] delete[]
Restrictions 6
Cannot change
How operators act on built-in data types
I.e., cannot change integer addition
Precedence of operator (order of evaluation)
Use parentheses to force order-of-operations
Associativity (left-to-right or right-to-left)
Number of operands
++ is unitary, only acts on one operand
Cannot create new operators
Operators must be overloaded explicitly
Overloading + does not overload +=
Operator functions 7
Member functions
When operators are overloaded as member functions, they
must be non-static, because they must be called on an
object of the class and operate on that object.
Use this keyword to implicitly get argument
Non member functions
Need parameters for both operands (for binary operators)
Must be a friend to access private or protected data
Arguments can be passed by value or by reference
Operator functions 8
An operator is overloaded by writing a non-static member function
definition or non-member function definition as you normally would, except
that the function name starts with the keyword operator followed by the
symbol for the operator being overloaded.
For example, the function name operator+ would be used to overload the addition operator (+)
for use with objects of a particular class.
void operator ++( ) // ++ obj -> obj.operator++( )
friend void operator ++ (complex) ) // ++ obj -> operator++(obj )
complex operator + (complex) ) // obj3=obj1+ obj2 -> obj3=ob1.operator+(obj2 )
friend void operator + (complex, complex) // obj3=obj1+ obj2 -> obj3=operator+(obj1,obj2 )
Overloading Unary operator ‘-’ 9
class abc
main()
{
{
int m; abc x(10);
public: x.show();
abc(){} -x;
x.show();
abc(int n): m(n){}
}
void show()
{
cout<<m;
}
void operator - ()
{
m=-m;
}
};
Overloading Unary operator ‘-’ 10
class abc abc abc::operator - ()
{
{ m=-m;
int m; return *this;
}
public:
abc(){} main()
{
abc(int n): m(n){} abc x(10),y;
x.show();
void show()
-x;
{ x.show();
y = -x;
cout<<m; y.show();
} }
abc operator - ();
};
Overloading Unary operators ++,-- 11
Increment/decrement operators can be overloaded
Add 1 to a Date object, d1
Prototype (member function)
Date &operator++();
++d1 same as d1.operator++()
Prototype (non-member)
Friend Date &operator++( Date &);
++d1 same as operator++( d1 )
Overloading Unary operators ++,-- 12
To distinguish pre/post increment
Post increment has a dummy parameter
int of 0
Prototype (member function)
Date operator++( int );
d1++ same as d1.operator++( 0 )
Prototype (non-member)
friend Date operator++( Data &, int );
d1++ same as operator++( d1, 0 )
Integer parameter does not have a name
Not even in function definition
Overloading Unary operators ++,-- 13
class counter{
counter counter:: operator ++()
int cnt;
{ return counter(++cnt) ; }
public:
counter operator++ (counter c,int)
counter():cnt(10) { }
{ return counter (c.cnt++ ); }
counter(int c):cnt(c){ }
main()
void show()
{
{ cout<<cnt; }
counter x,y;
void operator -- ()
x.show();
{ - -cnt; }
- -x; x.show();
void operator -- (int)
x- -; x.show();
{ cnt- -; }
y=++x; y.show();
counter operator ++();
y=x++; y.show();
friend counter operator ++(counter,int);
}
};
Overloading Binary operators +,-,*,/,%
class comp
14
{ int real, imag;
main()
public:
{
comp():real(0),imag(0){}
comp n1(10,20),n2(30,40), n3;
comp(int r, int i):real(r), imag(i){}
n3= n1 + n2;
comp operator + (const comp&);
cout<<"First number:";
friend comp operator -(const comp &,const comp&);
n1.display();
void display()
cout<<"Second number:";
{ cout<<real<<"+"<<imag<<"i"<<endl; }
n2.display();
};
cout<<"After addition:";
n3.display();
comp comp :: operator +(const comp &c2)
n3= n2 - n1;
{ return comp ( real + c2.real, imag + c2.imag ); }
cout<<"After subtraction:";
n3.display();
comp operator -(const comp &c1, const comp &c2)
}
{ return comp ( c1.real - c2.real,c1.imag - c2.imag ); }
Overloading Binary operators +,-,*,/,% 15
In above example, operator+ () using member function has only one
parameter even though it overloads the binary + operator.
The reason that operator+ () takes only one parameter is that the
operand on the left side of the + is passed implicitly to the function
through the this pointer. The operand on the right is passed in the
parameter c.
The statement n3= n1 + n2; is same as n3=n1.operator+(n2).
Similarly, operator - () using non-member function has both
parameters passed explicitly.
The statement n3= n2 - n1; is same as n3=operator-(n2, n1).
Overloading Relational operators 16
==, !=, <, >, >=, <= int length :: operator >( length &c2)
{ if(mtr>c2.mtr) main()
return true; {
class length length l1(10,20),l2(30,40), l3(30,50);
else if(mtr==c2.mtr && cmtr>c2.cmtr)
{ int mtr, cmtr;
cout<<"l1="; l1.display();
return true;
public: cout<<"l2="; l2.display();
else cout<<"l3="; l3.display();
length():mtr(0),cmtr(0){}
return false; if(l1>l2)
length(int m, int cm): mtr(m), cmtr(cm){ }
} cout<<" l1 is greater than l2"<<endl;
int operator > ( length&); else
bool operator <( length &c1, length &c2) cout<<" l1 is not greater than l2"<<endl;
friend bool operator < (length &,length&);
{ if(c1.mtr<c2.mtr)
void display() if(l2<l3)
return true; cout<<" l2 is less than l3"<<endl;
{ else
else if(c1.mtr==c2.mtr && c1.cmtr<c2.cmtr) cout<<" l2 is less not than l3"<<endl;
cout<<mtr<<"m"<<cmtr<<"cm"<<endl;
return true; }
}
else
};
return false;
}