Sunteți pe pagina 1din 15

Operator Overloading

Definition

Operator overloading provides a flexible


option for the creation of new definition for
most of the C++ operator. This means that
C++ has the ability to provide the operator
with a special meaning for a data type. The
mechanism of giving such special meaning to
an operator is known as operator overloading
Caution…
 Member access operator (. , *. )
 Scope resolution operator (::)
 sizeof operator
 Conditional operator (? :)

Note:
Although the semantics of an operators can be extended we can not
change its syntax & grammatical rules such as no of operator
precedence & associatively.
Syntax for Operator Overloading
<return type> operator <op> ( arguments )
{
//body of the function ;
}

Rules:

1. return type is the type of value return by the operator function


2. op : is the operator that is to be overloaded
the op is preceded by the keyword operator

Hence operator op is the function name.


Operator functions must be either member functions or friend functions. A
basic difference between them is that a friend function will have 2
operands. A friend function can have one argument for unary(one)
operator & two for binary operator while a member function has no
arguments for unary & only one for binary operators.
Overloading of unary minus
class A int main()
{ int x,y ; {
public: A obj1;
void get(int a, int b) obj1.get(5,-3);
{
obj1.put();
x=a;
-obj1; // obj1.operator –();
y=b; }
void put()
obj1.put();
{ }
cout<<x<<y;
}
void operator – ()
O/P:
{
x=5 x=-5
x= -x;
y=-3 y=3
y= -y ;
}
};
Overloading of binary (+)
operators(Arithmetic)
class A A operator + (A obj)
{ int x,y ; {
A temp;
public:
temp.x=x+obj.x;
A( ) { } temp.y=y+obj.y;
A(int a, int b) return temp;
{ x=a; }
y=b; };
} int main()
void show() {
{ A o1(5,2),o2(3,7),o3;
cout<<x<<y; o3=o1+o2;
} o3.show();
O/P: }
o1 o2
x=5 x=3
y=2 y=7
Note:-
In case of binary operator overloading the object present to the left of the
operator is responsible for invoking operator function whereas the object
present right of the operator is passed as an argument to the operator
function
Overloading of Assignment Operators
class Sample
void main()
{ int a ;
{
float y;
Sample o1(5,2.5),o2(6,7.5);
public:
o1=o2 ; // o1.operator = (o2);
sample(int a, float b) {
o1.display();
x=a;
}
y=b;
}
void display()
{cout<<x<<y; }
void operator = (sample obj)
O/P:
{ o1 o2
x=obj.x; x=5 x=6
y=2 .5 y=7.5
y=obj.y
}
};
Overloading of Assignment Operators
class Sample
void main()
{ {
int x ; Sample o1(5,2.5), o2(6,7.5);
o1+=o2 ; // o1.operator +=(o2);
float y;
o1.display();
public: }
sample(int a,float b) {
x=a;
y=b;
}
void display()
{cout<<x<<y; }
void operator += (sample obj)
{ x+=obj.x; O/P:
o1
y+=obj.y x=11
} y=10
};
Overloading of Relational operators (<=)
class Sample
void main()
{ int x ; {
public: Sample obj1(10),obj2(20),obj3;
cout<< (obj1< obj2); // obj1.operator < (obj2);
Sample() {x=0; }
cout<< (obj2 < obj1);
Sample(int a) {x=a; } }
int operator < (Sample obj)
{ return x < obj.x ;
}
};

O/P:
1 0
Overloading of Increment & Decrement
Operators (Prefix)(++)
class A
void main()
{ int x ; {
A obj(2);
public: ++obj; // obj.operator ++ ;
A(int a) {x=a; } obj.display();
}
void display()
{cout<<x; }
void operator ++ ()
{
++x;
}
};
Overloading of Increment& Decrement
operators (Postfix)(++)
class A void main()
{
{ int x ;
A obj(5);
public: obj++; // obj.operator ++(int) ;
obj.show();
A(int a) {x=a; } }
void show()
{cout<<x; }
void operator ++ (int)
x++;
}
};
Overloading Operators using Friend
Functions
Using friend function we can not overload certain
operators. These are
 Assignment operator ( = )
 Function call operators ()
 Subscripting operator [ ]
 Class member access operator ->
Overloading Minus using Friend Function
class A
void main()
{ int x, y; {
public: A ob1;
void get(int a, int b) ob1.get(-11,-12);
ob1.disp();
{ x=a; y=b; }
-ob1; //operator -(ob1);
friend void operator -(A &); ob1.disp();
void disp() }
{ cout<<x<<" "<<y; }
};
void operator -(A &o1)
{
o1.x = -o1.x;
o1.y = -o1.y;
}
Overloading Arithmetic operator using
Friend Function
void main()
class A
{
{ int x, y;
A o1(10,20), o2(50,60),o3;
public:
o3=o1+o2; //o3=operator + (o1,o2);
A() { }
o3.show();
A(int a, int b)
}
{ x=a; y=b; }
void show()
{ cout<<x<<y; }
friend A operator +(A,A);
};
A operator +(A obj1 ,A obj2)
{
A temp;
temp.x=obj1.x+obj2.x;
temp.y=obj1.y+obj2.y;
return temp;
}
Rules for Operator Overloading

 Only existing operator can be overloaded, new


operators can not be created.
 The overloaded operator must have at least
one operand that is of user defined type.
 We can not change the basic meaning of an
operator.
For exp- we can not redefine the plus(+)
operator to subtracts one value from another.

S-ar putea să vă placă și