Shallow Copy Constructor Vs Deep Copy Constructor

Copy Constructor 
It is a type of constructor which is used to create a copy of an already existing object. A copy constructor is a member function which initialises an object using another object of the same class.To implement this in coding we define a member function class which initialises an object using another object of the same class. The function prototype for copy constructor is as below:
ClassName (const ClassName &existing_obj);

copyConstruct.cpp
#include<iostream>
using namespace std;
class Point
{
    private:
    int x, y;   //data members
    
    public:
    Point(int x1, int y1)
    {
        x = x1;
        y = y1;
    }
    
    /* Copy constructor */
    Point (const Point &sam)
    {
        x = sam.x;
        y = sam.y;
    }
    
    void display()
    {
        cout<<x<<" "<<y<<endl;
    }
};
/* main function */
int main()
{
    Point obj1(10, 15);     // Normal constructor
    Point obj2 = obj1;      // Copy constructor
    cout<<"Normal constructor : ";
    obj1.display();
    cout<<"Copy constructor : ";
    obj2.display();
    return 0;
}

Shallow Copy Constructor 
I will explain the concept of shallow copy constructor using example. Suppose you want to update your resume with the help of your friend. You shared your resume doc on google drive with your friend. Now you and your friend both have copy of resume. Now changes made by both of you will be reflected in the same doc. Because same doc is opened in both locations. Same thing happens with shallow copy constructor.  Irrespective of objects different copy,  memory location is same. Shallow copy copies references to original objects. The compiler provides a default copy constructor. 

ShallowCopyConstruct.cpp
#include<iostream>
#include<cstring>
using namespace std;
class shallowConstructor
{
    char *s_copy;
    public:
    shallowConstructor(const char *str)
    {
        s_copy = new char[16]; //Dynamic memory allocation
        strcpy(s_copy, str);
    }
    /* concatenate method */
    void concatenate(const char *str)
    {
        strcat(s_copy, str); //Concatenating two strings
    }
    /* copy constructor */
    ~shallowConstructor ()
    { 
        delete [] s_copy;
    }
    void display()
    {
        cout<<s_copy<<endl;
    }
};
/* main function */
int main()
{
    shallowConstructor c1("Vision");
    shallowConstructor c2 = c1; //Copy constructor
    c1.display();
    c2.display();
    c1.concatenate("3DTech");    //c1 is invoking concatenate()
    c1.display();
    c2.display();
    return 0;
}

Output:
Vision
Vision
Vision3DTechVision3DTech
Deep copy constructor
Let's consider an example for explaining deep copy constructor.  'Engineering' and 'assignment submission' how do you relate ? Most of the students while doing engineering thinks that assignment submission is just a waste of time, and they do it for sake of formality by copying their friends assignment. But as teacher don't allow copying assignments of others, while copying assignment every student add his own deliberate changes in the assignment to make it different from his friends master copy. So in the class even though their is a single master copy, multiple derived copies are formed. And more important thing is that changes made in derived copy will not reflect in master copy. This is what happens in deep copy constructor. As shown in figure below deep copy allocates separate memory for copied information. So the source and copy are different. Any changes made in one memory location will not affect copy in the other location.  To allocate separate memory in run time using pointers we take help of user defined copy constructor. 

DeepcopyConstruct.cpp
#include<iostream>
#include<cstring>
using namespace std;
class deepConstructor
{
    char *s_copy;
    public:
    deepConstructor (const char *str)
    {
        s_copy = new char[16];  //Dynamic memory alocation
        strcpy(s_copy, str);
    }
    
    deepConstructor (const deepConstructor &str)
    {
        s_copy = new char[16]; //Dynamic memory alocation
        strcpy(s_copy, str.s_copy);
    }
    
    void concatenate(const char *str)
    {
        strcat(s_copy, str); //Concatenating two strings
    }

    ~deepConstructor()
    { 
        delete [] s_copy;
    }

    void display()
    {
        cout<<s_copy<<endl;
    }
};
/* main function */
int main()
{
    deepConstructor c1("Vision");
    deepConstructor c2 = c1;    //copy constructor
    c1.display();
    c2.display();
    cout << "Changes made in c1 will reflect in c1 object only \n";
    c1.concatenate("3DTech");    //c1 is invoking concatenate()
    c1.display();
    c2.display();
    return 0;
}
Output:
Vision
Vision
Changes made in c1 will reflect in c1 object only 
Vision3DTech
Vision
That's all for now !!

No comments:

Post a Comment

Rendering 3D maps on the web using opesource javascript library Cesium

This is a simple 3D viewer using the Cesium javascript library. The example code can be found here . Click on question-mark symbol on upp...