Overloading Special Array Subscript Operator [ ] in c++
This video tutorial explains how to overload the array subscript operator in c++.
You are gonna learn what are the special operators available in c++ for overloading, how to overload array subscript operator, what are the important things to remember when overloading array subscript operator in detail with example.
source code for this tutorial
#include <iostream> using namespace std; class Marks{ int subjects[3]; public: Marks(int sub1,int sub2,int sub3){ subjects[0] = sub1; subjects[1] = sub2; subjects[2] = sub3; } int operator[](int position){ return subjects[position]; } }; int main() { Marks anil(22,33,55); cout << anil[0] <<endl; cout << anil[1] <<endl; cout << anil[2] <<endl; return 0; }