Shallow and Deep Copy
Tags: cpp, class, programming, dsa
Published: 2022-01-26 15:17:11

**Shallow Copy**

An object is created by copying all of the member field values. Here, the pointer will be copied but not the memory it points to. It means that the original object and the created copy will now point to the same memory address, which is generally not preferred. Since both objects will reference the same memory location, then changes made by one will reflect those changes in another object. Since we wanted to create a replica of the object, this purpose will not be filled by Shallow copy. 

 

Note: The assignment operator and the default copy constructor make a shallow copy.

 

Example:

```
#include <iostream>
using namespace std;
class box {
    private:
    int length;
    int breadth;
    int height;
    public:
    void set_values(int a, int b,
            int c) {
            length = a;
            breadth = b;
            height = c;
    }
    void show_data() {
        cout << "Length = " << length << endl <<
            "Breadth = " << breadth << endl <<
            "Height = " << height <<
            endl;
    }
};
int main() {
    box B1, B3;
    // Set dimensions of Box B1 
    B1.set_values(5, 10, 15);
    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;
}
Output:
Length = 5
Breadth = 10
Height = 15
Length = 5
Breadth = 10
Height = 15
Length = 5
Breadth = 10
Height = 15
 
```

 

**Deep Copy**
 

An object is created by copying all the fields, and it also allocates similar memory resources with the same value to the object. To perform Deep copy, we need to explicitly define the copy constructor and assign dynamic memory as well if required. Also, it is necessary to allocate memory to the other constructors’ variables dynamically.

 

Example:
```
#include <iostream>
using namespace std;
class box {
    private:
    int length;
    int * breadth;
    int height;
    public:
    box() {
            breadth = new int;
    }
    void set_values(int a, int b,
        int c) {
        length = a;
        * breadth = b;
        height = c;
    }
    void show_data() {
        cout << "Length = " << length << endl <<
            "Breadth = " << breadth << endl <<
            "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;
        }
        ~box() {
            delete breadth;
        }
};
int main() {
    box B1;
    // Set the dimensions of box B1
    B1.set_values(5, 10, 15);
    B1.show_data();
    /* When the data will be copied, then all the resources will also get 
     allocated to the new object */
    box B2 = B1;
    // Display the dimensions 
    B2.show_data();
    return 0;
}
Output:
Length = 5
Breadth = 0x1b94c20
Height = 15
Length = 5
Breadth = 0x1b94c40
Height = 15
```