Operator Overloading in C++

What is operator overloading?

Operator overloading is compile time polymorphism. Operator overloading provides a flexible option for the creation of new definitions for most of the C++ operators. We can overload all the C++ operators except the following:

  1. Class members access operator (. , .*)
  2. Scope resolution operator ( : : )
  3. Size operator(sizeof)
  4. Conditional operator ( ? : )

Although the semantics of an operator can be extended, we can’t change its syntax, the grammatical rules that govern its use such as the no of operands precedence and associativity.

For example the multiplication operator will enjoy higher precedence than the addition operator. When an operator is overloaded, its original meaning is not lost.

For example, the operator +, which has been overloaded to add two vectors, can still be used to add two integers.

That is 2+3 means 5 (addition), where as
"2"+"3" means 23 (concatenation).

Syntax:

To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied. This is done with the help of a special function called operator function, which describes the task.

return-type class-name :: operator op(arg-list)
{
    function body
}

Where ,

  • return type is the type of value returned by the specified operation
  • op is the operator being overloaded. The op is preceded by the keyword operator, operator op is the function name.
  • Arguments may be either by value or by reference.

Here, operator functions must be either member function, or friend function. A basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators, This is because the object used to invoke the member function is passed implicitly and therefore is available for the member functions.

The process of overloading involves the following steps:

  1. Create a class that defines the data type that is used in the overloading operation.
  2. Declare the operator function operator op() in the public part of the class
  3. It may be either a member function or friend function.
  4. Define the operator function to implement the required operations.

Rules for operator overloading:

  1. Only existing operator can be overloaded.
  2. The overloaded operator must have at least one operand that is user defined type.
  3. We cannot change the basic meaning and syntax of an operator.
  4. We cannot use friend function to overload certain operators. However member function can be used to overload them.
  5. Unary operators, overloaded by means of a member function, take no explicit argument s and return no explicit value, but, those overloaded by means of a friend function, take one reference argument.
  6. Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments.
  7. When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class.

operator functions are declared in the class using prototypes as follows:

  • vector operator + (vector); // vector addition
  • vector operator-( ); //unary minus
  • friend vector operator + (vector, vector); // vector add
  • friend vector operator -(vector); // unary minus
  • vector operator – ( vector &a); // subtraction
  • int operator = =(vector); //comparison
  • friend int operator = =(vector ,vector); // comparison

vector is a data type of class and may represent both magnitude and direction or a series of points called elements.

Overloaded operator functions can be invoked by expressions such as:

op x or x op;for unary operators
x op y ;for binary operators
operator op(x);for unary operator using friend function
operator op(x,y);for binary operator using friend function
Operator overloading syntax

Example for Unary Operator Overloading

#include<iostream>
using namespace std;
class sample
{
	int a,b; public:
	void getdata()
	{
		a=10;
		b=20;
	}
	void operator -()	//Unary Member Function
	{
		a = a - 5;
		b = b - 5;
	}
	void disp()
	{
	     cout<<"\nThe value of a="<<a; 
             cout<<"\nThe value of b="<<b;
	}
};

int main()
{
	sample S;
	S.getdata();
	-S;		//Call Unary Member Function 
        S.disp();
	getch(); 
        return 0;
}

Example for Binary Operator Overloading

#include <iostream>
using namespace std; 
class complex
{
		float real, imag; 
	public:
	complex(float _real, float _imag) // constructor
	{
		real = _real; 
		imag = _imag;
	}
	void disp()
	{
		cout<<"The value of real="<<real; 
		cout<<"The value of imag="<<imag;
	}
	void operator +(complex c) //Binary Member function
	{
		real = real + c.real; 
	        imag = imag + c.imag;
	}
};
int main()
{
	complex x(4,4); 
	complex y(6,6);
	x + y; 	// Call Binary Member Function 
	x.disp();
	getch(); 
	return 0;
}

Note: Operator overloading has been criticized because it allows programmers to give operators completely different functionality depending on the types of their operands, usage of the << operator is an example of this problem.

Related posts