Ads block

Banner 728x90px

Object Oriented Programming using C++



Operator Overloading


Operator overloading is one of the many exciting features of C++ language.


To know operator overloading, we have to understand about operator and overloading one by one.


An operator is a symbol that represent an action to be performed.

Overloading is a language feature that allows a function or operator to be given more than one definition.


This overloading features is used to add two user defined data types such as objects, with the same syntax, just as a basic data types.


Defining Operator Overloading


The process of overloading involves the following steps:

It creates a class that defines the data types that is to be used in the overloading operator.

It declares the operator function operator op() in the public part of the class.
It may be either a member function or a friend function.

It defines the operator function to implement the required operations.


return type class name:: operator op(arg)
  {
       function body    // task defined
  }

Where op is a operator being overloaded. The op is preceded by the keyword operator.

Operator op is a function name. It indicates that an arithmetic operator is used at the place of op.


Overloaded operator function can be invoked by expression such as

op x or x op means it represents an unary operator such as, +x or x+
Like this an expression x op y represents a Binary operator such as x - y


All the doubts related to the operator overloading will be clear in this example

#include<iostream>

using namespace std;

class space
{
      int x;
      int y;
      int z;
  public:
      void getdata(int a, int b, int c);
      void display(void);
      void operator-();      // overload unary minus
};
 void space :: getdata(int a, int b, int c)
{
      x = a;
      y = b;
      z = c;
}
 void space :: display(void)
{
      cout << x << " ";
      cout << y << " ";
      cout << z << "\n";
}
void space :: operator-()
{
      x = -x;
      y = -y;
      z = -z;
}

int main()
{
      space S;
      S.getdata(10, -20, 30);
      cout << "S : ";
      S.display();

      -S;           // activates operator-() function

      cout << "S : ";
      S.display();

      return 0;
}


S : 10 -20 30
S : -10 20 -30



Some points need to be noted from this program.

It is possible to change the sign of data members of the object S, because operator-() is a member function.

Note: The function operator-( ) takes no argument. It changed the sign of data members of the object S because this function is a member function of the same class, it can directly access the member of the object which activated it.


But in such a case

S2 = -S1;

It will not work because, the function operator-( ) does not return any value. It needs to modify to return an object.

It is possible to overload a unary minus operator using a friend function as follows:

Example

friend   void operator-(space &s);      // declaration
void operator-(space &s)         // definition
  {
      s.x = -s.x;
      s.y = -s.y;
      s.z = -s.z;
}

Note that the argument is passed by reference. It will not work if we pass argument by value because only a copy of the object that activated the call is passed to operator-( ).



Overloading Of Binary Operator



Binary operators overloaded by means of member functions take one formal argument which is the value to the right of the operator. Binary operators, overloaded by means of friend functions take two arguments.

Arithmetic operators are binary operators, because they require two operands to perform the operation.
Whenever an arithmetic operator is used for overloading, the operator overloading function is invoked with single class objects.

#include<iostream.h>

using namespace std;

class space
{
     int x;
  public:
     space( )
      {
        x = 0;
      }
     space(int y)
      {
        x = y;
      }
     space operator+(space obj)
      {
        space sumobj;
        sumobj.x= x + obj.x;
        return(sumobj);
      }
     void display( )
      {
        cout << "
};