#include struct safearray { protected: // protected things can only be accessed from within int data[100]; // safearray's own functions. Nobody else may do anything // with the array public: // public means back to normal, everything accessible now int size; int get(int pos) // so this is the only function in the world that can show { if (pos<0 || pos>=size) // us the contents of the array. It is impossible to bypass { cerr << "error in get\n"; // the check for a valid position exit(1); } return data[pos]; } void put(int pos, int val) // and this is the only function that can change the { if (pos<0 || pos>=size) // array. We are safe from the other example's terrors. { cerr << "error in put\n"; exit(1); } data[pos] = val; } }; void main() { safearray A, B; A.size = 20; B.size = 90; cout << "A\n"; for (int i=0; i < A.size; i+=1) A.put(i, i * 10001); cout << "B\n"; for (int i=0; i < 123; i+=1) B.put(i, i*2); B.data[345] = 6; } // this last statement is illegal, we will get an error