/*********************************************************************
safearray.h
container class implementation: templates for generic typing
*********************************************************************/
#include <iostream>
using namespace std;
#include <process.h> //for the exit() function
//class definition
template <class T> class safearray {
public:
enum { maxsize = 100 }; //replaces const int maxsize = 100;
private:
T array[maxsize];
int used;
public:
safearray() {
used = 0;
}
const int& count() const {
return used;
}
T& operator[](const int& pos) {
if ((pos < 0) || (pos >= maxsize)) {
cerr << endl << "Index out of bounds" << endl;
exit(1);
}
else if (used < pos+1)
used = pos+1;
return array[pos];
}
const T& operator[](const int& pos) const {
if ((pos < 0) || (pos >= maxsize) || (pos+1 > used)) {
cerr << endl << "Index out of bounds" << endl;
exit(2);
}
return array[pos];
}
void remove(const int& pos) {
if ((pos < 0) || (pos >= maxsize) || (pos+1 > used)) {
cerr << endl << "Index out of bounds" << endl;
exit(3);
}
for (int i = pos; i < used; i++)
array[i] = array[i+1];
used--;
}
};