← Back to Pointers
Question 732
Inventory Copy Constructor Deep-Copies the Names
Inventory Copy Constructor Deep-Copies the Names
Inventoryis written for you except its copy constructor.m_itemsis a simple pointer: it holds the address of the firststringin ONE heap array, so the whole array is released with a singledelete[]. WriteInventory(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 readsnfollowed bynitem names (one per line), copy-constructs a second inventory from the first, renames the copy's first item toCHANGED, prints both lists with each name in square brackets, then printsseparateif slot 0 of the two objects lives at different addresses (orsharedif 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
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.