Shallow Copy and Deep Copy in C++

16 Jun, 2022
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.
Copy Constructor
Default assignment operator
// Copy ConstructorGeeks Obj1(Obj);orGeeks Obj1 = Obj;
// Default assignment operatorGeeks Obj2;Obj2 = Obj1;
Depending upon the resources like dynamic memory held by the object, either we need to perform Shallow Copy or Deep Copy in order to create a replica of the object. In general, if the variables of an object have been dynamically allocated, then it is required to do a Deep Copy in order to create a copy of the object.
In shallow copy, an object is created by simply copying the data of all variables of the original object. This works well if none of the variables of the object are defined in the heap section of memory. If some variables are dynamically allocated memory from heap section, then the copied object variable will also reference the same memory location.This will create ambiguity and run-time errors, dangling pointer. Since both objects will reference to the same memory location, then change made by one will reflect those change in another object as well. Since we wanted to create a replica of the object, this purpose will not be filled by Shallow copy. Note: C++ compiler implicitly creates a copy constructor and overloads assignment operator in order to perform shallow copy at compile time. 
Shallow Copy of object if some variables are defined in heap memory, then:
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <iostream>using namespace std; // Box Classclass box {private:    int length;    int breadth;    int height; public:    // Function that sets the dimensions    void set_dimensions(int length1, int breadth1,                        int height1)    {        length = length1;        breadth = breadth1;        height = height1;    }     // Function to display the dimensions    // of the Box object    void show_data()    {        cout << " Length = " << length             << "\n Breadth = " << breadth             << "\n Height = " << height             << endl;    }}; // Driver Codeint main(){    // Object of class Box    box B1, B3;     // Set dimensions of Box B1    B1.set_dimensions(14, 12, 16);    B1.show_data();     // When copying the data of object    // at the time of initialization    // then copy is made through    // COPY CONSTRUCTOR    box B2 = B1;    B2.show_data();     // When copying the data of object    // after initialization then the    // copy is done through DEFAULT    // ASSIGNMENT OPERATOR    B3 = B1;    B3.show_data();     return 0;}
Length = 14
 Breadth = 12
 Height = 16
 Length = 14
 Breadth = 12
 Height = 16
 Length = 14
 Breadth = 12
 Height = 16
In Deep copy, an object is created by copying data of all variables, and it also allocates similar memory resources with the same value to the object. In order to perform Deep copy, we need to explicitly define the copy constructor and assign dynamic memory as well, if required. Also, it is required to dynamically allocate memory to the variables in the other constructors, as well.
Below is the implementation of the above approach:
C++
// C++ program to implement the// deep copy#include <iostream>using namespace std; // Box Classclass box {private:    int length;    int* breadth;    int height; public:    // Constructor    box()    {        breadth = new int;    }     // Function to set the dimensions    // of the Box    void set_dimension(int len, int brea,                       int heig)    {        length = len;        *breadth = brea;        height = heig;    }     // Function to show the dimensions    // of the Box    void show_data()    {        cout << " Length = " << length             << "\n Breadth = " << *breadth             << "\n Height = " << height             << endl;    }     // Parameterized Constructors for    // for implementing deep copy    box(box& sample)    {        length = sample.length;        breadth = new int;        *breadth = *(sample.breadth);        height = sample.height;    }     // Destructors    ~box()    {        delete breadth;    }}; // Driver Codeint main(){    // Object of class first    box first;     // Set the dimensions    first.set_dimension(12, 14, 16);     // Display the dimensions    first.show_data();     // When the data will be copied then    // all the resources will also get    // allocated to the new object    box second = first;     // Display the dimensions    second.show_data();     return 0;}
Length = 12
 Breadth = 14
 Height = 16
 Length = 12
 Breadth = 14
 Height = 16
Let us see the differences in a tabular form -:
mayank007rawa
jayanth_mkv
Constructors
cpp-constructor
C++
C++ Programs
Difference Between
Programming Language
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set in C++ Standard Template Library (STL)
vector erase() and clear() in C++
unordered_map in C++ STL
Priority Queue in C++ Standard Template Library (STL)
Substring in C++
Header files in C/C++ and its uses
Sorting a Map by value in C++ STL
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
C++ program for hashing with chaining