Open In App

Difference between Shallow and Deep copy of a class

Shallow Copy: Shallow repetition is quicker. However, it’s “lazy” it handles pointers and references. Rather than creating a contemporary copy of the particular knowledge the pointer points to, it simply copies over the pointer price. So, each of the first and therefore the copy can have pointers that reference constant underlying knowledge.

Deep Copy: Deep repetition truly clones the underlying data. It is not shared between the first and therefore the copy.

Below is the tabular Difference between the Shallow Copy and Deep Copy:
 

Shallow Copy Deep Copy
Shallow Copy stores the references of objects to the original memory address.    Deep copy stores copies of the object’s value.
Shallow Copy reflects changes made to the new/copied object in the original object. Deep copy doesn’t reflect changes made to the new/copied object in the original object.
Shallow Copy stores the copy of the original object and points the references to the objects. Deep copy stores the copy of the original object and recursively copies the objects as well.
A shallow copy is faster. Deep copy is comparatively slower.

Below is the program to explain the shallow and deep copy of the class.




// C++ program to illustrate the deepcopy and shallow copy
#include <algorithm>
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
 
// Class of Car
class Car {
public:
    string name;
    vector<string> colors;
 
    Car(string name, vector<string> colors)
    {
        this->name = name;
        this->colors = colors;
    }
};
 
int main()
{
    // Create a Honda car object
    vector<string> honda_colors = { "Red", "Blue" };
    Car honda = Car("Honda", honda_colors);
 
    // Deepcopy of Honda
    Car deepcopy_honda = honda;
    deepcopy_honda.colors.push_back("Green");
    cout << "Deepcopy: ";
    for (string color : deepcopy_honda.colors) {
        cout << color << " ";
    }
    cout << endl << "Original: ";
    for (string color : honda.colors) {
        cout << color << " ";
    }
    cout << endl;
 
    // Shallow Copy of Honda
    Car* copy_honda = &honda;
    copy_honda->colors.push_back("Green");
    cout << "Shallow Copy: ";
    for (string color : copy_honda->colors) {
        cout << color << " ";
    }
    cout << endl << "Original: ";
    for (string color : honda.colors) {
        cout << color << " ";
    }
    cout << endl;
 
    return 0;
}




// Java program to illustrate the difference between shallow
// and deep copy
import java.util.ArrayList;
 
// Class of Car
class Car {
    public String name;
    public ArrayList<String> colors;
 
    public Car(String name, ArrayList<String> colors)
    {
        this.name = name;
        this.colors = colors;
    }
}
 
public class Main {
    public static void main(String[] args)
    {
        // Create a Honda car object
        ArrayList<String> hondaColors = new ArrayList<>();
        hondaColors.add("Red");
        hondaColors.add("Blue");
        Car honda = new Car("Honda", hondaColors);
 
        // Deep copy of Honda
        Car deepcopyHonda = new Car(
            honda.name, new ArrayList<>(honda.colors));
        deepcopyHonda.colors.add("Green");
        System.out.print("Deepcopy: ");
        for (String color : deepcopyHonda.colors) {
            System.out.print(color + " ");
        }
        System.out.println("\nOriginal: ");
        for (String color : honda.colors) {
            System.out.print(color + " ");
        }
        System.out.println();
 
        // Shallow Copy of Honda
        Car copyHonda = honda;
        copyHonda.colors.add("Green");
        System.out.print("Shallow Copy: ");
        for (String color : copyHonda.colors) {
            System.out.print(color + " ");
        }
        System.out.println("\nOriginal: ");
        for (String color : honda.colors) {
            System.out.print(color + " ");
        }
        System.out.println();
    }
}




# Python program to illustrate the difference between shallow and deep copy
import copy
 
 
class Car:
    def __init__(self, name, colors):
        self.name = name
        self.colors = colors
 
 
# Create a Honda car object
honda_colors = ["Red", "Blue"]
honda = Car("Honda", honda_colors)
 
# Deepcopy of Honda
deepcopy_honda = copy.deepcopy(honda)
deepcopy_honda.colors.append("Green")
print("Deepcopy:", deepcopy_honda.colors)
print("Original:", honda.colors)
 
# Shallow Copy of Honda
copy_honda = copy.copy(honda)
copy_honda.colors.append("Green")
print("Shallow Copy:", copy_honda.colors)
print("Original:", honda.colors)

Output
Deepcopy: ['Red', 'Blue', 'Green']
Original: ['Red', 'Blue']
Shallow Copy: ['Red', 'Blue', 'Green']
Original: ['Red', 'Blue', 'Green']

Article Tags :