overload operator []

#define MaxInt 101
#include <iostream>
using namespace std;
class IntegerSet
{
public:
IntegerSet(int a1=-1, int a2=-1, int a3=-1, int a4=-1, int a5=-1)
{
for (int i =0; i<MaxInt; i++)
{
if ((i ==a1) || (i==a2) || (i==a3)||(i==a4)||(i==a5))
a[i] =1;
else
a[i] =0;
}
}
private:
int a[MaxInt];
};

void main()
{
IntegerSet s(2, 88, 17, 33);
for (int i= 0; i<5; i++)
cout << "s{"<< i << "} = " << s[i] << endl;
}
// prgram result
s[0] = 2
s[1] = 17
s[2] = 33
s[3] = 88
s[4] = -1
how do i implement an overload operator [] as a public member function for the class IntegerSet to get the above result
The first part of this tutorial goes over operator overloading, including the [].
http://cplusplus.com/doc/tutorial/classes2/

Good day!

-Albatross
1
2
3
4
5
6
class IntergerSet {
  ...
 public:
  ...
  int& operator[](int index); //return a reference
};
Topic archived. No new replies allowed.