CJCoding With Joseph
15free / daySign inCreate account

NumberBag That Doubles Its Buffer

Write a class NumberBag that grows its own storage — without std::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), add first allocates a new array of capacity * 2, copies the existing values into it, releases the OLD array, and keeps the new one. Then it stores the value and increases count. If not full, just store the value — no reallocation.

The driver reads initialCapacity n then n values, 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
Topics:
Pointers
📥 Auto-Input:
2 5\n4 8 15 16 23\n
This input is automatically fed to your program's stdin
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.