Home | | Programming and Data Structure II | Operator Overloading

## Chapter: Programming and Data structures : Object Oriented Programming Concepts

Operator overloading is a very important feature of Object Oriented Programming. Curious to know why!!? It is because by using this facility programmer would be able to create new definitions to existing operators.

Operator overloading is a very important feature of Object Oriented Programming. Curious to know why!!? It is because by using this facility programmer would be able to create new definitions to existing operators. In other words a single operator can take up several functions as desired by programmers depending on the argument taken by the operator by using the operator overloading facility.

Broadly classifying operators are of two types namely:

ü   Unary Operators

ü   Binary Operators

Unary Operators:

As the name implies, it operates on only one operand. Some unary operators are named ++ also called the Increment operator, -- also called the Decrement Operator, ! , ~ are called unary minus.

Binary Operators:

It operates on two operands. Some binary operators are arithmetic operators, comparison operators, and arithmetic assignment operators.

Operator overloading helps the programmer to define a new functionality for the existing operator. This is done by using the keyword operator.

The general syntax for defining an operator overloading is as follows:

return_type classname :: operator operator_symbol(argument)

{

…….

Statements;

}

Operator overloading is defined as a member function by making use of the keyword operator.

In the above:

ü   return_type - is the data type returned by the function

ü   class name - is the name of the class

ü   operator - is the keyword

ü   operator symbol - is the symbol of the operator which is being overloaded or defined for new functionality

ü   :: - is the scope resolution operator which is used to use the function definition outside the class. The usage of this is clearly defined in our earlier section of How to define class members.

For example

Suppose we have a class say Exforsys and if the programmer wants to define a operator overloading for unary operator say ++, the function is defined as, Inside the class Exforsys the data type that is returned by the overloaded operator is defined as,

class Exforsys

{

private:

…..

public:

void operator ++();

…….

};

The important steps involved in defining an operator overloading in case of unary operators are:

Inside the class the operator overloaded member function is defined with the return data type as member function or a friend function. The concept of friend function we will define in later sections. If in this case of unary operator overloading if the function is a member function then the number of arguments taken by the operator member function is none as seen in the below example. In case if the function defined for the operator overloading is a friend function which we will discuss in later section then it takes one argument.

The operator overloading is defined as member function outside the class using the scope resolution operator with the keyword operator.

#include <iostream.h>

using namespace std;

class Exforsys

{

private: int x;

public:

Exforsys( ) { x=0; } //Constructor void display();

void operator ++( );

};

void Exforsys :: display()

{

cout << "nValue of x is: " << x;

}

void Exforsys :: operator ++( ) //Operator Overloading for operator ++ defined

{

++x;

}

void main( )

{

Exforsys e1,e2; //Object e1 and e2 created

cout << "Before Increment";

cout << "nObject e1: "; e1.display();

cout << "nObject e2: "; e2.display();

++e2;

cout << "n After Increment";

cout << "nObject e1: "; e1.display();

cout << "nObject e2: "; e2.display();

}

In the above example we have created 2 objects e1 and e2 f class Exforsys. The operator ++ is overloaded and the function is defined outside the class Exforsys.

When the program starts the constructor Exforsys of the class Exforsys initialize the values as zero and so when the values are displayed for the objects e1 and e2 it is displayed as zero. When the object ++e1 and ++e2 is called the operator overloading function gets applied and thus value of x gets incremented for each object separately. So now when the values are displayed for objects e1 and e2 it is incremented once each and gets printed as one for each object e1 and e2.

Binary operators, when overloaded, are given new functionality. The function defined for binary operator overloading, as with unary operator overloading, can be member function or friend function.

The difference is in the number of arguments used by the function. In the case of binary operator overloading, when the function is a member function then the number of arguments used by the operator member function is one (see below example). When the function defined for the binary operator overloading is a friend function, then it uses two arguments.

Binary operator overloading, as in unary operator overloading, is performed using a keyword operator.

Sample Code

#include <iostream>

using namespace std;

class Exforsys

{

private: int x; int y;

public:

Exforsys()   //Constructor

{

x=0; y=0;

}

void getvalue( )      //Member Function for Inputting Values

{

cout << "n Enter value for x: ";

cin >> x;

cout << "n Enter value for y: ";

cin>> y;

}

void displayvalue( )  //Member Function for Outputting Values

{

cout << "value of x is: " << x << "; value of y is: " << y;

}

Exforsys operator +(Exforsys);

};

Exforsys Exforsys :: operator + (Exforsys e2)

{

Exforsys rez; //declaring an Exforsys object to retain the final values

int x1 = x+ e2.x;

int y1 = y+e2.y;

rez.x=x1;

rez.y=y1;

return rez;

}

void main( )

{

Exforsys e1,e2,e3;     //Objects e1, e2, e3 created

cout << "nEnter value for Object e1:";

e1.getvalue( );

cout << "nEnter value for Object e2:";

e2.getvalue( );

e3= e1+ e2; //Binary Overloaded operator used

cout << "nValue of e1 is: "; e1.displayvalue();

cout << "nValue of e2 is: " ; e2.displayvalue();

cout << "nValue of e3 is: "; e3.displayvalue();

}

In the above example, the class Exforsys has created three objects e1, e2, e3. The values are entered for objects e1 and e2. The binary operator overloading for the operator '+' is declared as a member function inside the class Exforsys. The definition is performed outside the class Exforsys by using the scope resolution operator and the keyword operator.

e3= e1 + e2; The binary overloaded operator '+' is used. In this statement, the argument on the left side of the operator '+', e1, is the object of the class Exforsys in which the binary overloaded operator '+' is a member function. The right side of the operator '+' is e2. This is passed as an argument to the operator '+' . Since the object e2 is passed as argument to the operator '+' inside the function defined for binary operator overloading, the values are accessed as e2.x and e2.y. This is added with e1.x and e1.y, which are accessed directly as x and y. The return value is of type class Exforsys as defined by the above example.

There are important things to consider in operator overloading with C++ programming language. Operator overloading adds new functionality to its existing operators. The programmer must add proper comments concerning the new functionality of the overloaded operator. The program will be efficient and readable only if operator overloading is used only when necessary.

ü   scope resolution operator denoted by ::

ü   Member access operator or the dot operator denoted by .

ü   The conditional operator denoted by ?:

ü   And pointer to member operator denoted by .*

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Programming and Data structures : Object Oriented Programming Concepts : Operator Overloading |

Related Topics