What is the main purpose of a copy constructor in C++?

Options

  • A. To initialize an object from another object of the same or compatible class type
  • B. To destroy an existing object
  • C. To allocate an array automatically
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. To initialize an object from another object of the same or compatible class type

Detailed Explanation

Answer: To initialize an object from another object of the same or compatible class type

A copy constructor initializes a new object using an existing object.

A typical form is:

ClassName(const ClassName& other);

It is particularly important when a class manages resources because copying an object may require careful handling of those resources.