Deep Copy and Shallow Copy in C++

Deep Copy and Shallow Copy in C++

The definition of deep copy and shallow copy is given below:

Creating a copy of object by copying data of all member variables as it is, is called shallow copy while creating an object by copying data of another object along with the values of memory resources resides outside the object but handled by that object, is called deep copy.

Let us first understand some basic points that you should aware of before knowing about shallow copy and deep copy in details.


How can we create a copy of an object?

1. Default copy constructor is defined by the compiler when object of any class is created and there is no copy constructor defined in the program.

2. In addition to copy constructor, C++ compiler also overload copy assignment operator which is implemented when an object (after declaration) is initialized using "=" with another object of same class.


For Example:- copy assignment operator in c++
#include<iostream>
using namespace std;

class DummyClass
{
private:
int num1, num2;

public:
void setData(int x, int y)
{
num1 = x;
num2 = y;
}

void showData()
{
cout << num1 << " " << num2 << endl;
}
};

int main()
{
DummyClass D1;
D1.setData(5, 11);
D1.showData();

DummyClass D2 = D1; //Copy constructor (created by compiler) will came in action here.
D2.showData();

DummyClass D3;
D3.setData(1, 11);
D3.showData();

DummyClass D4;
D4 = D3; //Copy assignment operator overloaded by compiler came in action.
D4.showData();

return 0;
}


Output:

5 11
5 11
1 11
1 11


In the above example, object D4 is created and then it is initialized with object D3. In this case, copy assignment operator which is overloaded by compiler itself is implemented.


Points To Remember:-

When we create a copy of an object:
1. copy constructor will be called or
2. implicit copy assignment operator will be called.


When will Copy Constructor call?

When you initialize an object with another object of same class during its declaration, copy constructor will be called.

For Example:-
#include<iostream>
using namespace std;

class DummyClass
{
private:
int num1, num2;

public:
void setData(int x, int y)
{
num1 = x;
num2 = y;
}

void showData()
{
cout << num1 << " " << num2 << endl;
}
};

int main()
{
DummyClass D1;
D1.setData(5, 11);
D1.showData();

DummyClass D2 = D1; //Copy constructor (created by compiler) will came in action here.
D2.showData();
}


When will Copy Assignment Operator call?

When you do not initialize an object during its declaration but initialize it after its declaration with another object of same class then copy assignment operator will be called.

For Example:-
#include<iostream>
using namespace std;

class DummyClass
{
private:
int num1, num2;

public:
void setData(int x, int y)
{
num1 = x;
num2 = y;
}

void showData()
{
cout << num1 << " " << num2 << endl;
}
};

int main()
{
DummyClass D1;
D1.setData(5, 11);
D1.showData();

DummyClass D2;
D2 = D1; //Copy assignment operator overloaded by compiler came in action.
D4.showData();
}


Deep Copy and Shallow Copy in C++

1. Creating a copy of object by copying data of all member variables as it is, it is called shallow copy. The compiler will do shallow copy by default.

2. Creating an object by copying data of another object along with the values of memory resources resides outside the object but handled by that object, it is called deep copy.

Deep-copy-and-shallow-copy-in-c++-learning-mania


For Example:- deep copy and shallow copy in c++
#include<iostream>
using namespace std;

class DummyClass
{
private:
int num1, num2;
int *ptr;

public:
DummyClass()
{
ptr = new int;
}

void setData(int x, int y, int z)
{
num1 = x;
num2 = y;
*ptr = z;
}

void showData()
{
cout << "A = " << num1 << " B = " << num2 << endl;
}

DummyClass(DummyClass &D) //copy cnstructor -> it is responsible for deep copy.
{
num1 = D.num1;
num2 = D.num2;
*ptr = *(D.ptr);
}

~DummyClass() //destructor -> to deallocate memory consumed by new pointer ptr.
{
delete ptr;
}
};

int main()
{
DummyClass D1;
D1.setData(3, 5, 11);
D1.showData();

DummyClass D2 = D1; //Copy constructor declared above will came in action and do deep copy.
D2.showData();
}


Output:

A = 3 B = 5
A = 3 B = 5



What is Dangling Pointer in C++?

A pointer which is pointing to an invalid address is called dangling pointer. There is a chance of program crash due to dangling pointer.



<< Previous                                                                                      Next >>
Previous
Next Post »