Explain shallow copy, deep copy, and the copy constructor in C++.
Show answer and explanation
A copy constructor initializes a new object from an existing object. An implicitly generated copy performs memberwise copy: value members become independent values, while a raw pointer member copies only its address, which can look like a shallow copy and can break ownership. A deep-copying owner allocates a distinct resource and copies its contents, but the preferred design is the Rule of Zero—store resources in value-semantic RAII members so generated copy and destruction already have the right behavior.
A copy constructor initializes a new object from an existing object. An implicitly generated copy performs memberwise copy: value members become independent values, while a raw pointer member copies only its address, which can look like a shallow copy and can break ownership. A deep-copying owner allocates a distinct resource and copies its contents, but the preferred design is the Rule of Zero—store resources in value-semantic RAII members so generated copy and destruction already have the right behavior.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.