/*********************************************************************
example 7 - safearray.h
putting count into the class
*********************************************************************/
#include <iostream>
using namespace std;
#include <process.h> //for the exit() function
const int maxsize = 100;
//class definition
class safearray {
private:
int array[maxsize];
int used;
public:
safearray() {
used = 0;
}
const int& count() const {
return used;
}
int& 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 int& operator[](const int& pos) const {
if ((pos < 0) || (pos >= maxsize) || (pos+1 > used)) {
cerr << endl << "Index out of bounds" << endl;
exit(1);
}
return array[pos];
}
};