Need a project done?

C++ Programming Developer

Search This Blog

Overloading Subscript operator [] C++


#include <iostream>
using namespace std;

class a{
public:
double& operator[](int index);
double get(int index);//returns value at required index.
private:
double array[20];
};

double& a::operator[](int index){
return array[index];
}

double a::get(int index){
return array[index];
}

void main(){
a obj;
obj[5] = 4;

cout << "obj[5] = " << obj.get(5) << endl;

double var = obj[5];
cout << "var = " << var << endl;
}
"Don't let anyone ever make you feel like you don't deserve what you want."