#include <iostream>
#include <cstdlib>
using namespace std;
// T defaults to int and size defaults to 10
template <class T=int, int size=10> class MyType {
T a[size];
public:
MyType() {
for(int i=0; i<size; i++)
a[i] = i;
}
T &operator[](int i){
if(i<0 || i> size-1) {
cout << "\nIndex value of ";
cout << i << " is out-of-bounds.\n";
exit(1);
}
return a[i];
}
};
int main()
{
MyType<int, 100> intarray;
MyType<double> doublearray;
MyType<> defarray;
cout << "int array: ";
for(int i=0; i<100; i++)
intarray[i] = i;
for(int i=0; i<100; i++)
cout << intarray[i] << " ";
cout << '\n';
cout << "double array: ";
for(int i=0; i<10; i++)
doublearray[i] = (double) i/3;
for(int i=0; i<10; i++)
cout << doublearray[i] << " ";
cout << '\n';
cout << "defarray array: ";
for(int i=0; i<10; i++)
defarray[i] = i;
for(int i=0; i<10; i++)
cout << defarray[i] << " ";
cout << '\n';
return 0;
}
|