CJCoding With Joseph
15free / daySign inCreate account

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 = new int(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) int getValue() const that returns *data

The tester creates BadCopy a(42), copies it with BadCopy b(a), and prints b.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
Topics:
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.