/* At last we are using the heap, and the size of our safearray is completely under our control. We can even change its size whenever we feel like it. */ #include struct safearray { protected: int * data; int size; public: void create(int s) { data = new int [s]; size = s; } int getsize() { return size; } void setsize(int newsize) { delete [] data; data = new int [newsize]; size = newsize; } int get(int pos) { if (pos<0 || pos>=size) { cerr << "error in get\n"; exit(1); } return data[pos]; } void put(int pos, int val) { if (pos<0 || pos>=size) { cerr << "error in put\n"; exit(1); } data[pos] = val; } }; void main() { safearray A, B; int N; cout << "How big should A be? "; cin >> N; A.create(N); cout << "How big should B be? "; cin >> N; B.create(N); cout << "A\n"; for (int i=0; i < A.getsize(); i+=1) A.put(i, i * 10001); cout << "B\n"; for (int i=0; i < B.getsize(); i+=1) B.put(i, i*2); for (int i = 0; i < 100; i += 1) cout << A.get(i) << " " << B.get(i) << "\n"; }