← Back to Classes
Question 47
Fix Shallow Copy
Fix Shallow Copy
Create a class BadCopy that owns an int* data pointer. Implement: 1) A constructor BadCopy(int v) that allocates the value on the heap: data = newint(v)2) A destructor that releases it: delete data 3) A copy constructor BadCopy(const BadCopy& other) that allocates its OWN memory and copies the value, so the copy is deep (the two objects never share a pointer) 4) intgetValue()const that returns *data The tester creates BadCopya(42), copies it with BadCopyb(a), and printsb.getValue(). Do NOT write a main function.
Expected Output:
42= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Classes
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.