Copy Constructor in C++

  • October 19, 2022
  • C++
Accessing Elements

A copy constructor is a type of constructor that creates a copy of another object. If we want one object to resemble another, we can use a copy constructor. If no copy constructor is written in the program, the compiler will supply its own copy constructor.
The copy constructor is used to −

  • Initialize one object from another of the same type.
  • Copy an object to pass it as an argument to a function.
  • Copy an object to return it from a function.
Syntax:
classname (const classname &obj) {
   // body of constructor
}
Example:
#include<iostream>
using namespace std;


class Number{
    int a;
    public:
        Number(){
            a = 0;
        }

        Number(int num){
            a = num;
        }
        // When no copy constructor is found, compiler supplies its own copy constructor
        Number(Number &obj){
            cout<<"Copy constructor"<<endl;
            a = obj.a;
        }

        void display(){
            cout<<"The number for this object is "<< a <<endl;
        }
};
    
    int main()
{
    Number x, y, z(45), z2;
    x.display();
    y.display();
    z.display();

    Number z1(z); // Copy constructor invoked
    z1.display();

    z2 = z; // Copy constructor not called
    z2.display();

    Number z3 = z; // Copy constructor invoked
    z3.display();

    // z1 should exactly resemble z  or x or y

    return 0;
}
Output:
The number for this object is 0
The number for this object is 0
The number for this object is 45
Copy constructor
The number for this object is 45
The number for this object is 45
Copy constructor
The number for this object is 45
Shallow copy & Deep copy:

A shallow copy is made by copying the data of all member variables as they are. In contrast, a deep copy is made by copying the data of another object along with the values of memory resources that reside outside the object but are handled by that object.

Shallow copy creates an object by simply copying the data of all variables from the original object. This is useful if none of the object’s variables are defined in the heap section of memory. If some variables are allocated memory dynamically from the heap section, then copied object variables will also reference the same memory location.

The dangling pointer will cause ambiguity and run-time errors. Because both objects will refer to the exact memory location, any changes made by one will be reflected in the other. The shallow copy will not serve our purpose because we wanted to create a replica of the object.

Example of shallow copy:
#include <iostream>
using namespace std;

class Test
{
    public:
    int a;
    int *p;

    Test (int x)
    {
        a = x;
        p = new int[a];
    }
    Test (Test & t)
    {
        a = t.a;
        p = t.p;
    }
};

int main()
{
    Test t (5);
    t.p[0] = 1;
    Test t2 (t);
    cout << "t: " << t.a << " " << t.p[0] << endl;
    cout << "t2: " << t2.a << " " << t2.p[0] << endl;
}
Output:
t: 5 1
t2: 5 1
Example of Deep copy:
#include <iostream>
using namespace std;

class Test
{
    public:
    int a;
    int *p;

    Test (int x)
    {
        a = x;
        p = new int[a];
    }
    Test (Test & t)
    {
        a = t.a;
        p = new int[a];
        if (p)
        {
         for (int i = 0; i < a; i++)
         {
             p[i] = t.p[i];
         }
        }
    }
};

int main()
{
    Test t (5);
    t.p[0] = 1;
    Test t2 (t);
    cout << "t: " << t.a << " " << t.p[0] << endl;
    cout << "t2: " << t2.a << " " << t2.p[0] << endl;
}
Output:
t: 5 1
t2: 5 1

Note: also read about References in C++

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *