#include <iostream>
#include <cstdlib>
using namespace std;
template <class T, int size> class MyClass {
T a[size]; // length of array is passed in size
public:
MyClass() {
register int i;
for(i=0; i<size; i++) a[i] = i;
}
T &operator[](int i);
};
template <class T, int size>
T &MyClass<T, size>::operator[](int i)
{
if(i<0 || i> size-1) {
cout << i << " is out-of-bounds.\n";
exit(1);
}
return a[i];
}
int main()
{
MyClass<int, 10> intob;
MyClass<double, 15> doubleob;
for(int i=0; i<10; i++)
intob[i] = i;
for(int i=0; i<10; i++)
cout << intob[i] << endl;
for(int i=0; i<15; i++)
doubleob[i] = (double) i/3;
for(int i=0; i<15; i++)
cout << doubleob[i] << " ";
return 0;
}
|