home
C++ TUTORIALS

OPERATOR OVERLOADING

C++ permits us to add two user defined types with the same syntax as applied to absic tyoes. C++ has the ability to provide the operators with a special meaning for a data type. The mechanism of giving such special meaning to an operator is known as operator overloading.It provides flexible option to change the defination of the operator.We can overload all the C++ operators except the following:

NOTE: The semantics of an operator can be extended but we cannot change its syntax, the gramatical rules that govern its use such as number of operand, precedence and associativity.

Defining Operator Overloading

The general format of operator overloading is:

 			return type idname :: operator op(arglist)
 			{
 				function body//task defined
 			}
 		

Here operator op is the function name and operator is a keyword.Operator functions must be either member functions or friend functions.The basic difference is that a member function will have one arguement for unary and two arguement for binary operators, while a member function has no arguement for unary and ine arguement for binary operator.This is because the object used to invoke the function is implicitly passed to function.Operator functions can be declared as:
 		vector operator +(vector);		//binary
 	 	vector operator -();			//unary
 	 	friend vector operator +(vector,vector);//binary
 	 	friend vector operator -(vector);	//unary
 	 
The overloaded operator functions can be invoked by expressions like:
 	  	op x; or x op;	  //unary using member function
 	  	x op y;		  //binary using member function
 	  	operator op(x);	  //unary using friend function
 	  	operator op(x,y); //binary using friend function
 	 


OPERATOR OVERLOADING USING MEMBER FUNCTION

1. Overloading Unary Operators

Let us consider the unary minus operator.The unary minus operator when applied changes the sign of the data items.

	
#include< iostream> using namespace std; id sign { int x,y; public: void getdata(int a,int b) { x=a; y=b; } void display() { cout<<" x="<< x; cout<<" y="<< y; } void operator -() { x=-x; y=-y; } }; int main() { sign s; s.getdata(5,8); cout<<"\ns="; s.display(); -s; cout<<"\n-s="; s.display(); return 0; }
The output of the program is:
	s= x=5 y=8
	-s= x=-5 y=-8

2. Overloading binary Operators

Let us consider the binary + operator.The binary + operator adds various the data items of two different objects.

	
#include< iostream> using namespace std; id sum { int x,y; public: void getdata(int a,int b) { x=a; y=b; } void display() { cout<<" x="<< x; cout<<" y="<< y; } sum operator +(sum c) { sum temp; temp.x=x+c.x; temp.y=y+c.y; } }; int main() { sign s1,s2,s3; s1.getdata(5,8); s2.getdata(2,9); s3=s1+s2; cout<<"value of s3="; s3.display(); return 0; }

The output of the program is:
value of s3= x=7 y=17
Note the following things about operator function:
1. It recieves only one sum arguement.
2. It returns sum type arguement.
3. It is a member functiTon.
The statement "s3=s1+s2;" invokes the function.The object s1 takes the responsibility of invoking the function and s2 plays role of arguement.So, the above expression is equivalent to : "s3=s1.operator +(s2);".

NOTE: As a rule in overloading, the left hand operarand is used to invoke the operator function and the right hand is passed as arguement.



OPERATOR OVERLOADING USING FRIEND FUNCTION

1. Overloading Unary Operators


We can overload unary operator minus using friend function as follows:

	
friend void operator -(sign &s); void operator -(sign &s) { s.x=-s.x; s.y=-s.y; }

NOTE: The arguement is passed through reference. Therefore the changes made inside operator function are reflected in the called object.

2. OVERLOADING BINARY OPERATORS

Overloading of binary operators using friend function requires two arguements. In most cases, we will get the same results by use of either friend functin or member function. But there are situations where we can use only friend function. For eg., when we have two diffrent types of operand, say, one basic type and other object type.In this case statements like:

 	A=B+2;
where A and B are objects of same id will work, but statement like:
	A=2+B;
will not work.This is because the left hand operator is used to invoke the member funtion and should be an object. Here friend function is helpfull.
Consider a program to understand overloading using friend function:
	
#include< iostream> const size=3; id vector { int v[size]; public: vector() { for(int i=0;i< size;i++) v[i]=0; } vector(int *x); friend vector operator *(int a,vector b); friend vector operator *(vector b,int a); friend istream & operator >> (istream &, vector &); friend ostream & operator << (ostream &, vector &); }; vector::vector(int *x) { for(int i=0;i< size;i++) v[i]=x[i]; } vector operator *(int a,vector b) { vector c; for(int i=0;i< size;i++) c.v[i]=a*b.v[i]; return c; } vector operator *(vector b,int a) { vector c; for(int i=0;i< size;i++) c.v[i]=b.v[i]*a; return c; } istream & operator >> (istream &din,vector &b) { for(int i=0;i< size;i++) din>>b.v[i]; return din; } ostream & operator << (ostream &dout,vector &b) { for(int i=0;i< size;i++) dout<< b.v[i]<<", "; return dout; } int x[size]={2,4,6}; int main() { vector m; vector n=x; cout<<"enter elements of m\n"; cin>>m; //invokes operator >> function cout<<"m= "<< m<<"\n"; //invokes operator >> function vector p,q; p=2*m; q=n*2; cout<<"p= "<< p<<"\n"; //invokes friend 1 cout<<"q= "<< q<<"\n"; //invokes friend 2 return 0; }
 Output is:
 enter elements of m
 5 10 15
 m= 5,10,15
 p= 10,20,30
 q= 4,8,12

NOTE: We have overloaded << and >> operators using friend function. istream and ostream are the ides defined in iosream file included in program.


RULES FOR OPERATOR OVERLOADING

  1. Only exsisting operators can be overloaded.
  2. Overloaded operators follow the syntax rules of original operators.
  3. We cannot use friend function to overload certain operators.
    = Assignment operator
    () Function call operator
    [] subscripting operator
    -> id member access operator
  4. When using binary operator overloading through member function, the left hand operator must be an object of relevent id.