#include using namespace std; struct Vector { int * v; int size; Vector() // no return type, same name as object = CONSTRUCTOR { size = 0; v = new int[0]; } // Functions inside an object are called METHODS void setsize(int sz) { if (sz < 0) { cout << "Can't create an array of size " << sz << "\n"; exit(1); } int * temp = new int[sz]; int amt = min(size, sz); for (int i = 0; i < amt; i += 1) temp[i] = v[i]; v = temp; size = sz; } int getsize() { return size; } int & access(int pos) { if (pos < 0 || pos >= size) { cout << "Index to array is out of bounds\n"; exit(1); } return v[pos]; } }; int main() { Vector R; int n; cout << "How big would you like the array to be? "; cin >> n; R.setsize(n); for (int i = 0; i < R.getsize(); i += 1) R.access(i) = 1000 + i; /* pretend to use the array here */ // I discover that the array needs to be even bigger, let's say N*2 R.setsize(n*2); // fill in the rest of the array now for (int i = n; i < R.getsize(); i += 1) R.access(i) = 1000 + i; cout << "The array contains:\n"; for (int i = 0; i < R.getsize(); i += 1) cout << " R[" << i << "] = " << R.access(i) << "\n"; }