Operator Overloading
Operator overloading is one of the important and useful feature of Object
oriented programming which gives an extra ability to an operator to act on a
User-defined operand (Objects).
Operators such as +,-,<=,>= etc., can be used only with basic data types int ,
float etc., but the same is not applicable for objects.
Operator overloading is used these operators with the objects and classes.
DEFINITION:
The capability to relate the existing operator with a member function and use the
resulting operator with objects of its operand is called Operator overloading
Prof E.MALATHY SCORE
Why Operator Overloading
int i, j, k; // integers
float m, n, p; // floats The compiler overloads the
+ operator for built-in integer
and float types by default,
k = i + j; producing integer addition
// integer addition and assignment with i+j, and floating addition
p = m + n; with m+n.
// floating addition and assignment
We can make object operation look like individual int
variable operation, using operator functions
Date a,b,c;
c = a + b;
Prof E.MALATHY SCORE
SYNTAX:
KEYWORD
Return type operator operator symbol(arguments)
{ + - ++ -- ==
Statement 1; Prototype of operator functions:
Statement 2; Void operator ++ ();
} Void operator - ();
Overloaded operators are redefined in c++ class using the keyword operator
followed by an operator symbol.
when an operator is overloaded, the produced symbol is called the operator
function name.
Operator function should be either member function or friend function.
Prof E.MALATHY SCORE
Steps involved in operator overloading
1. Define a class which is to be used with overloading operations.
2. Declaring the operator function, operator operator symbol (arguments) :
This must be done in public section of the class.
3. Define the definition of the operator () function with proper operations for
which it is declared.
Prof E.MALATHY SCORE
OVERLOADABLE OPERATORS
Arithmetic operators Bitwise operators
+ (addition) ^ (XOR)
- (subtraction) | (OR)
* (multiplication) & (AND)
/ (division) ~ (complement)
% (modulus) << (shift left, insertion to stream)
>> (shift right, extraction from stream)
Relational operators
Assignment operator =
== (equality)
!= (inequality)
> (greater-than)
< (less-than)
>= (greater-than-or-equal-to)
<= (less-than-or-equal-to)
Prof E.MALATHY SCORE
Compound assignment operators
+= (addition-assignment)
-= (subtraction-assignment) Increment and decrement operators
*= (multiplication-assignment)
/= (division-assignment) ++ (increment)
%= (modulus-assignment) -- (decrement)
&= (AND-assignment)
|= (OR-assignment) comma operator ,
^= (XOR-assignment)
>>= (shift-right-assignment) subscript operator []
<<= (shift-left-assignment)
Memory management operators function call operator ()
new (allocate memory for object)
new[ ] (allocate memory for array)
delete (deallocate memory for object)
delete[ ] (deallocate memory for array)
Prof E.MALATHY SCORE
Operators which cannot be overloaded
?: (conditional)
. (member selection)
.* (member selection with pointer-to-member)
:: (scope resolution)
sizeof (object size information)
All the operators can be overloaded using friend function except () [] -> and =.
These operators must be defined by using a member function.
Prof E.MALATHY SCORE
Overloading Binary Operators
• A binary operator can be overloaded as a non-
staticmember function with one parameter or as
a non-member function with two parameters
(one of those parameters must be either a class
object or a reference to a class object).
• As a non-member function, binary operator <
must take two arguments—one of which must be
an object (or a reference to an object) of the class.
Prof E.MALATHY
SCORE
Syntax for overloading a binary
operator
Prof E.MALATHY
SCORE
Arithmetic operators
Date a,b,c;
c = a + b;
Rules for overloading binary operator
The left-hand operand is used to invoke the operator function and the
right-hand operand is passed as an argument to the operator function.
The binary overloaded operator function takes the first object as an implicit
operand and the second operand must be passed explicitly.
The data members of first object are accessed without using the dot
operator, the second argument members can be accessed using the dot
operator if the argument is an object, otherwise it can be accessed directly.
Prof E.MALATHY
SCORE
#include<iostream>
class add main()
{ {
int x,y; add a,b,c;
public: a.data();
void data() b.data();
{ c=a+b;
cout<<"enter the value of x and y:"; c.show();
cin>>x>>y;
} }
void show()
{
cout<<"x="<<x<<endl<<"y="<<y<<endl; Eg:
} a: 2 3
add operator + (add m)
{ b: 1 5
add t; c: 3 8
t.x=x+m.x;
t.y=y+m.y;
return t;
}
};
class s
{
public: USING FRIEND FUNCION
int i,j;
s()
{ int main()
i=j=0; {
} s s1,s2,s3;
void disp() s1.getdata();
{ cout << i <<" " << j; s2.getdata();
} s3 = s1+s2;
void getdata() s3.disp();
{ return 0;
cin>>i>>j; }
}
friend s operator+(s s1,s s2);
};
s operator+(s s1, s s2)
{
s k;
k.i = s1.i+s2.i;
k.j = s1.j+s2.j;
return k; ProfE.MALATHY
Prof E.MALATHY
SITE
} SCORE
#include<iostream>
class add add operator - (add m)
{ {
int x,y; add t;
public: t.x=x-m.x;
void data() t.y=y-m.y;
{ return t;
cout<<"enter the value of x and y:"; }
cin>>x>>y; add operator * (add m)
} {
void show() add t;
{ t.x=x*m.x;
t.y=y*m.y;
cout<<"x="<<x<<endl<<"y="<<y<<endl; return t;
} }
add operator + (add m) add operator / (add m)
{ {
add t; add t;
t.x=x+m.x; t.x=x/m.x;
t.y=y+m.y; t.y=y/m.y;
return t; return t;
} }
};
Prof E.MALATHY
SCORE
main()
{
add a,b,c;
a.data();
b.data();
c=a+b;
c.show();
c=a-b;
c.show();
c=a*b;
c.show();
c=a/b;
c.show();
}
Prof E.MALATHY
SCORE
Overloading Unary Operator
A unary operator for a class can be overloaded as a non-static member
function with no arguments or as a non-member function with one argument
that must be an object (or a reference to an object) of the class.
The prefix and postfix versions of the increment and decrement operators can all be
overloaded.
To overload the increment operator to allow both prefix and postfix increment
usage, each overloaded operator function must have a distinct signature, so that the
compiler will be able to determine which version of ++ is intended.
The prefix versions are overloaded exactly as any other prefix unary operator would
be.
The same syntax is with a dummy argument is used to differentiate between the
prefix and postfix decrement operator functions.
Prof E.MALATHY SCORE
Syntax for overloading unary
operators
Prof E.MALATHY
SCORE
OVERLOADING UNARY OPERATORS
The operators ++ -- and – are unary operators.
The unary operators ++ -- can be used as prefix or suffix with the function.
#include<iostream>
class num
{
void num::operator -()
int a,b,c;
{
public:
a=-a;
num(int p,int q,int r)
b=-b;
{
c=-c;
a=p;
}
b=q;
main()
c=r;
{
}
num x(1,2,3);
void operator -();
x.show();
void show();
x.operator-(); or –x;
};
x.show();
void num::show()
}
{
cout<<a<<b<<c;
} Prof E.MALATHY
SCORE
class Integer {
private:
// Pre-increment Operator
int value;
void Integer::operator++()
public:
{
void data()
++value;
{
}
cin>>value;
}
// Post-increment Operator
void operator++();
void Integer::operator++(int)
void operator++(int);
{
void show()
value++;
{
cout<<value;
}
}
};
Prof E.MALATHY
SCORE