Concept of Shallow Copy and Deep Copy in OOP of C++

Mannan Ul Haq
0

In C++, shallow copy and deep copy are two different ways of copying objects. Let me explain each of them with examples:


1. Shallow Copy:

Shallow copy creates a new object and copies the values of all the member variables from the source object to the destination object. However, if the member variables are pointers, only the memory address is copied, not the actual data pointed to. As a result, both the source and destination objects will point to the same memory location, leading to potential issues if one object modifies the data.


Here's an example:


#include <iostream>
using namespace std;

class ShallowCopyExample
{
private:
    int* data;

public:
    ShallowCopyExample(int value)
    {
        data = new int;
        *data = value;
    }

    // Shallow copy constructor
    ShallowCopyExample(const ShallowCopyExample& other)
    {
        data = other.data;  // Only the memory address is copied
    }

    // Getter function
    int getData() const
    {
        return *data;
    }

    // Setter function
    void setData(int value)
    {
        *data = value;
    }

    // Destructor
    ~ShallowCopyExample()
    {
        delete data;
    }
};

int main()
{
    ShallowCopyExample obj1(5);
    ShallowCopyExample obj2 = obj1;  // Shallow copy

    cout << "Obj1 data: " << obj1.getData() << endl;  // Output: Obj1 data: 5
    cout << "Obj2 data: " << obj2.getData() << endl;  // Output: Obj2 data: 5

    obj2.setData(10);

    cout << "Obj1 data: " << obj1.getData() << endl;  // Output: Obj1 data: 10
    cout << "Obj2 data: " << obj2.getData() << endl;  // Output: Obj2 data: 10

    return 0;
}


In this example, the `ShallowCopyExample` class has a member variable `data` that is a pointer to an integer. When `obj2` is created using the copy constructor (shallow copy) with `obj1`, the `data` pointer in both objects will point to the same memory address. As a result, modifying `obj2`'s data also affects `obj1`.


2. Deep Copy:

Deep copy creates a new object and copies the values of all the member variables from the source object to the destination object, including the data pointed to by any pointers. This ensures that the source and destination objects are completely independent of each other.


Here's an example:


#include <iostream>
using namespace std;

class DeepCopyExample
{
private:
    int* data;

public:
    DeepCopyExample(int value)
    {
        data = new int;
        *data = value;
    }

    // Deep copy constructor
    DeepCopyExample(const DeepCopyExample& other)
    {
        data = new int;
        *data = *(other.data);  // Copies the actual data
    }

    // Getter function
    int getData() const
    {
        return *data;
    }

    // Setter function
    void setData(int value)
    {
        *data = value;
    }

    // Destructor
    ~DeepCopyExample()
    {
        delete data;
    }
};

int main()
{
    DeepCopyExample obj1(5);
    DeepCopyExample obj2 = obj1;  // Deep copy

    cout << "Obj1 data: " << obj1.getData() << endl;  // Output: Obj1 data: 5
    cout << "Obj2 data: " << obj2.getData() << endl;  // Output: Obj2 data: 5

    obj2.setData(10);

    cout << "Obj1 data: " << obj1.getData() << endl;  // Output: Obj1 data: 5
    cout << "Obj2 data: " << obj2.getData() << endl;  // Output: Obj2 data: 10

    return 0;
}


In this example, the `DeepCopyExample` class is similar to the previous example, but the copy constructor performs a deep copy by allocating new memory for `data` and copying the actual data. As a result, modifying `obj2` does not affect `obj1`.


Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !