Binary operator are operators which requires two operands to performs the operation. When they are overloaded they takes one argument. Say we want to perform the addition of two object as
ob2 = ob + ob1
Observe the above statement here the two objects ob and ob1 are added and result is stored back in ob2. so here the ‘+’ overloaded function will be called on object ob and it will take ob1 as argument. And it will return the object of type ob &ob1 which contain the addition of object ob & ob1 ;
Code:
For ex :-
class x
{
int k ;
public: x ( )
{
k =10 ;
}
void show ( )
{
cout << k ;
}
x operator + + ( x ob3 )
{
x temp ;
temp.k = k + ob3.k ;
return ( temp)
}
} ;
main ( )
{
x ob, ob1;
x ob3 ;
ob3 = ob + ob1 ;
ob3 .show ( ) ;
}
Bookmarks