CJCoding With Joseph
15free / daySign inCreate account

Inventory Copy Constructor Deep-Copies the Names

Inventory is written for you except its copy constructor. m_items is a simple pointer: it holds the address of the first string in ONE heap array, so the whole array is released with a single delete[]. Write Inventory(const Inventory& other) so the new object gets its OWN array of the same size with every name copied across in a loop; afterwards changing one inventory must never change the other.

The hidden driver reads n followed by n item names (one per line), copy-constructs a second inventory from the first, renames the copy's first item to CHANGED, prints both lists with each name in square brackets, then prints separate if slot 0 of the two objects lives at different addresses (or shared if not).

For the input:
``
3
Hammer
Nail Box
Saw
``

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:

original: [Hammer] [Nail Box] [Saw]
copy: [CHANGED] [Nail Box] [Saw]
separate

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Pointers
📥 Auto-Input:
3\nHammer\nNail Box\nSaw\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.