Thursday, December 6, 2018
Home > OOP > Subscript Operator Overloading

Why is Subscript operator used?

We use Subscript operator [ ] to access elements of arrays. We pass an index with this operator to access the value at that particular index of the array. e.g. my_arr[5] this will give us the value at the 6th index of the array as array stores value from the 0th index. C++ allows us the feature of subscript operator overloading.

Use of Subscript operator Overloading

The most common use for subscript operator overloading is when we check index out of bound exception for an array. It helps us avoiding these type of errors.

How to overload Subscript operator C++?

We can overload the subscript operator in c++ by following the same syntax we use for operator overloading.

returnType operator [](int index)
{
     // body of overloaded operator.
}

returnType is same as for the array whose value we are going to access using subscript operator.
operator reserved keyword.
[ ] is the subscript operator.
It always takes an argument i.e. index which we are going to access.
Example program: –

#include "stdafx.h"
#include"iostream"

using namespace std;
class exStr
{
     char str[100];
     public:
          char  operator [](int i)
          {
               if (i < 0 || i > 100)
               {
                    cout << "index out of bound";
                    return NULL;
               }
               return str[i];
          }
          void input()
          {
               cout << "Please enter input string : ";
               cin >> str;
          }
          void display()
          {
               cout << endl << str;
          }
};

int main()
{
     exStr str1;
     str1.input();
     cout << "Value at 2nd place in array  = " << str1[1]<<endl;
     return 0;
}

Output: –
Subscript Operator c++

So far so good, when we overload it like this we can access the value of an array. But what if we want to store a value in a particular index or position in the array. Can we do with this overloaded definition?
No, we cannot assign the value to an array using this definition. What should we do know?
To solve this problem, we need to change our overloaded definition.
Here is how:

returnType & operator [](int index)
{
     // body of overloaded operator.
}

Here we added & operator i.e. we will return address of the index. In this way, we can assign a value as well.
Example program: –

#include "stdafx.h"
#include"iostream"

using namespace std;
class exStr
{
     char str[100];
     
     public:
          char & operator [](int i)
          {
               if (i < 0 || i > 100)
               {
                    cout << "index out of bound";
               }
               else
               {
                    return str[i];
               }
          }
          void input()
          {
               cout << "Please enter input string : ";
               cin >> str;
          }
          void display()
          {
               cout << endl << "String after Assigning value at an index = " <<str<<endl;
          }
};

int main()
{
     exStr str1;
     str1.input();
     cout << "Value at 2nd place in array  = " << str1[1];
     str1[1] = 'a';
     str1.display();
     return 0;
}

Output: –
Subscript Operator Overloading

In this way, we can both access and assign a value at a particular index of an array.
Note: – It is not necessary that argument to this overloaded function is always int, i.e. it can be a string value in case of a map (will be discussed later).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.