How to write overloading function using operator [] in C++

The overloading of [] is helpful to check for index out of bound. we need to return by reference in function because an expression like “arr[i]” can be used as an lvalue.

Let us see how the implementation of the operator [] in the below program

Header files of the class.

Class implementation:

Testing the array class:

See the below sample of calling :

The output is :

Then program called exit (0).