← Back to Pointers
Question 701
NumberBag That Doubles Its Buffer
NumberBag That Doubles Its Buffer
Write a classNumberBagthat grows its own storage — withoutstd::vector. Private members:int* data;,int count;(values stored),int capacity;(what the buffer can hold). Public interface: ``NumberBag(int initialCapacity); ~NumberBag(); void add(int value); int get(int index) const; int size() const; // count int getCapacity() const; // capacity`The growth rule, exactly: if the buffer is full (count == capacity),addfirst allocates a new array ofcapacity * 2, copies the existing values into it, releases the OLD array, and keeps the new one. Then it stores the value and increasescount. If not full, just store the value — no reallocation. The driver readsinitialCapacity nthennvalues, adds them all, and prints the size, the capacity, then the values on one line separated by single spaces. For the input:`2 5 4 8 15 16 23`` the exact output is what is shown in Expected Output. Free the memory you allocate — this question checks for memory leaks. Do NOT write a main function.
Expected Output:
size=5 capacity=8 4 8 15 16 23
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Pointers
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.