---
title: "Overload index operator [] for array class"
subject: "Object Oriented Programming"
chapter: 5
key: "semester_3/object_oriented_programming:index-operator-array-class"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/index-operator-array-class
site: BCT Engineering
---

# Overload index operator [] for array class

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2068 Chaitra, 6 marks
- Video: [Overloading Special [ ]  C++ Array Subscript Operator | Cpp Video Tutorial](https://www.youtube.com/watch?v=pbxxtg3JQc8) — LearningLad
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/index-operator-array-class
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

Create a class having an array as member. Overload index operator ([ ]) to input and display the elements in the array.

## Short answer

Overload operator[] with a bounds check, returning an int& reference to the array element. This allows both input (cin >> a[i]) and output (cout << a[i]). Sample elements 10 20 30 40 50 are read and displayed correctly.

## Answer

The subscript operator `[]` is overloaded as a member function that takes the index and returns a **reference** (`int&`). Returning a reference lets the same function be used on both sides of an assignment: `a[2] = 10;` (input) and `cout << a[2];` (display). A bounds check makes the class safer than a plain array.

```cpp
#include <iostream>
#include <cstdlib>
using namespace std;

const int SIZE = 5;

class Array {
    int arr[SIZE];
public:
    int& operator[](int i) {
        if (i < 0 || i >= SIZE) {
            cout << "Index out of range" << endl;
            exit(1);
        }
        return arr[i];
    }
};

int main() {
    Array a;
    cout << "Enter " << SIZE << " elements:" << endl;
    for (int i = 0; i < SIZE; i++)
        cin >> a[i];                 // a.operator[](i) used as lvalue

    cout << "Elements are: ";
    for (int i = 0; i < SIZE; i++)
        cout << a[i] << " ";         // a.operator[](i) used as rvalue
    cout << endl;
    return 0;
}
```

**Sample run:**

```text
Enter 5 elements:
10 20 30 40 50
Elements are: 10 20 30 40 50
```

`a[i]` is translated to `a.operator[](i)`. The subscript operator can be overloaded only as a member function.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why does operator[] return int& instead of int?
   A. To make it faster only
   B. So it can be used on the left of an assignment as well as to read
   C. Because arrays hold references
   D. To prevent bounds checking
   Answer: B. A reference return makes a[i] an lvalue, so cin >> a[i] or a[2] = 10 works. (Key point: Return type)

2. How is a[i] translated?
   A. operator[](a, i)
   B. a.operator[](i)
   C. i.operator[](a)
   D. operator(a[i])
   Answer: B. The object before [ ] is the caller and the index is the argument. (Key point: Translation)

3. Which statement about overloading [] is true?
   A. It can be a friend
   B. It can be a global function
   C. It must be a member function
   D. It cannot be overloaded
   Answer: C. The subscript operator can only be overloaded as a member function. (Key point: Member only)

4. What advantage does the overloaded [] give over a plain array?
   A. Larger arrays
   B. Faster access
   C. Automatic sorting
   D. It can check the index range
   Answer: D. The function can test i against SIZE and report out-of-range access. (Key point: Bounds check)

## Related in Object Oriented Programming

- [City class with operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acity-class-operator-overload) — Object Oriented Programming, chapter 5
- [Binary operator member function one argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-member-one-argument) — Object Oriented Programming, chapter 5
- [Array class template with max, min, sort](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-array-max-min-sort) — Object Oriented Programming, chapter 9
- [Member function as friend of another class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amember-function-friend-of-other-class) — Object Oriented Programming, chapter 4
- [Time class: +, <= and << overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-operator-overload) — Object Oriented Programming, chapter 5
- [Vector class addition by object argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avector-class-addition) — Object Oriented Programming, chapter 1

## Related in other subjects

- [Create classes from design class diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aclasses-from-design-class-diagram) — Object Oriented Analysis and Design, chapter 4, asked 2×
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Reverse array and display via pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-reverse-array) — Computer Programming, chapter 9
- [Accessing array elements using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-access-array-elements) — Computer Programming, chapter 9
