How to Implement a Copy Constructor for Deep Copying?
Last Updated :
27 Mar, 2024
In C++, the copy constructor enables us to initialize an object using another object of the same class. In this article, we will learn, how to implement a copy constructor for deep copying.
Implementing a Copy Constructor for Deep Copying
In C++, a copy constructor is used to create a copy of an object from an existing object of the same class. By default, the compiler-generated copy constructor creates a shallow copy which means if the object contains pointers to dynamically allocated memory, both the original and the copied objects will point to the same memory location so the changes made through one object's pointer will affect the other object, which might lead to unintended behavior or errors.
To ensure each object has its own independent copy of dynamically allocated memory, we can explicitly define a deep copy constructor that allocates separate memory for the copied object's pointers, thus ensuring changes in one object do not affect the other.
C++ Program to Implement a Copy Constructor for Deep Copying
The below example implements a copy constructor to create a deep copy of an object that has a character array as a data member.
C++
// C++ Program that illustrates the Implementation of Copy
// Constructor for Deep Copying
#include <cstring>
#include <iostream>
using namespace std;
class Student {
public:
int roll;
char* name;
Student(int roll)
: roll(roll)
{
name = new char[100]; // Allocate memory for name
}
void setName(const char* name)
{
strcpy(this->name, name);
}
// Implementing a deep copy constructor
Student(const Student& other)
: roll(other.roll)
{
name = new char[strlen(other.name) + 1];
strcpy(name, other.name);
}
// Implementing the copy assignment operator for deep
// copying
Student& operator=(const Student& other)
{
if (this != &other) { // Prevent self-assignment
roll = other.roll;
delete[] name; // Free existing name memory
name = new char[strlen(other.name) + 1];
strcpy(name, other.name);
}
return *this;
}
// Implementing a destructor to deallocate dynamically
// allocated memory
~Student() { delete[] name; }
};
int main()
{
// Create an object
Student s1(100);
s1.setName("John");
cout << "Original Object: " << s1.name << endl;
// Creating a copy of the object s1
Student s2 = s1; // Use copy constructor
cout << "Copy Object: " << s2.name << endl;
// Making changes in the data member of the copy object
s2.name[0] = 'S';
// Printing data members of both objects after making
// changes in copy object
cout << "After Changes:" << endl;
cout << "Original Object: " << s1.name << endl;
cout << "Copy Object: " << s2.name << endl;
return 0;
}
OutputOriginal Object: John
Copy Object: John
After Changes:
Original Object: John
Copy Object: Sohn
Time Complexity: O(N), here N denotes the size of the dynamic object.
Auxiliary Space: O(N)
Similar Reads
How to Implement a Copy Constructor in a Derived Class in C++ In object-oriented programming, a copy constructor is a special member function that initializes a new object as a copy of an existing object. In this article, we will learn how to implement a copy constructor in a derived class. Implementing Copy Constructor in a Derived Class in C++ In C++, when w
3 min read
How to Declare a Copy Constructor in a Derived Class? In C++, a copy constructor is a constructor that initializes an object as a copy of an existing object of the same class. In this article, we will learn how to declare a copy constructor in a derived class. Declaring a Copy Constructor in a Derived ClassWe can declare a copy constructor in a derived
3 min read
How to Define a Move Constructor in C++? In C++, we have a move constructor which is a part of C++11 move semantics and is used to handle resources for temporary and rvalue objects. In this article, we will learn how to write a move constructor in C++. How to Write Move Constructor in C++?The move constructor is defined similarly to a copy
2 min read
How to Copy a One Deque to Another in C++? In C++, STL provides a container called deque (short for double-ended queue) that allows fast insertion and deletion at both its beginning and its end. In some scenarios, we may need to copy the contents of one deque to another. In this article, we will learn how to copy one deque to another in C++.
2 min read
Shallow Copy and Deep Copy in C++ In general, creating a copy of an object means to create an exact replica of the object having the same literal value, data type, and resources. There are two ways that are used by C++ compiler to create a copy of objects.Copy ConstructorAssignment Operator// Copy ConstructorGeeks Obj1(Obj);orGeeks
6 min read