Copy Constructor In C++

Share Your Love

The Copy Constructor In C++ an overloaded constructor is called a copy constructor used to declare and initialize an object from another object is called a Copy constructor.

Copy Constructor is of two types:

  • Default Copy constructor: The default copy constructor is defined by the compiler. If the user does not provide a copy constructor, the compiler will provide one.
  • User Defined constructor: The user-defined constructor is defined by the programmer.
Types Of Constructor

Syntax Of User-defined Copy Constructor:

class_name(const class_name &old_object);  

Consider another problem:

class A  
{  
    A(A &x) //  copy constructor.  
   {  
       // copyconstructor.  
   }  
}   

In the example above, the copy constructor may be invoked as follows:

how to call copy constructor

Let’s look at a basic copy constructor [ (){ } ] example:

// program of the copy constructor.

#include <iostream>  
using namespace std;  
class A  
{  
   public:  
    int x;  
    A(int a)                // parameterized constructor.  
    {  
      x=a;  
    }  
    A(A &i)               // copy constructor  
    {  
        x = i.x;  
    }  
};  
int main()  
{  
  A a1(20);               // Calling the parameterized constructor.  
 A a2(a1);                //  Calling the copy constructor.  
 cout<<a2.x;  
  return 0;  
} 

Output:

20

When Copy Constructor is called:

In the following instances, Copy Constructor is used:

  • When we use another object of the same class type to initialise the object. Student s1 = s2, for example, where Student is the class.
  • When an object of the same class is provided as an argument by value.
  • When the function returns a value for an object of the same class type.

Two types of copies are produced by the constructor:

  • Shallow copy
  • Deep copy

What is Shallow Copy?

  • Only the shallow copy may be made with the default copy constructor.
  • A shallow copy is described as the act of making a duplicate of an object by copying all of the member variables’ data in their current state.

Let’s look at an example to help you understand:

#include <iostream>  
  
using namespace std;  
  
class Demo  
{  
    int a;  
    int b;  
    int *p;  
    public:  
    Demo()  
    {  
        p=new int;  
    }  
    void setdata(int x,int y,int z)  
    {  
        a=x;  
        b=y;  
        *p=z;  
    }  
    void showdata()  
    {  
        std::cout << "value of a is : " <<a<< std::endl;  
        std::cout << "value of b is : " <<b<< std::endl;  
        std::cout << "value of *p is : " <<*p<< std::endl;  
    }  
};  
int main()  
{  
  Demo d1;  
  d1.setdata(40,50,70);  
  Demo d2 = d1;  
  d2.showdata();  
    return 0;  
}

Output:

value of a is : 40  
value of b is : 50  
value of *p is : 70
Shallow Copy
Shallow Copy

Because no Constructor has been declared by the programmer in the example above, the phrase Demo d2 = d1; invokes the compiler’s default Constructor.

The default Constructor makes an exact or shallow duplicate of the current object. As a result, both objects’ pointers p refer to the same memory address.

As a result, when one field’s memory is freed, another field’s memory is also freed since both fields link to the same memory address.

The user-defined Constructor that generates the Deep copy solves this problem.

What is Deep Copy?

Deep copy allocates memory for the copy dynamically before copying the actual value; both the source and copy have separate memory addresses.

Both the source and the copy will be separate in this manner, and they will not share the same memory address. Deep copy involves the creation of a user-defined Constructor.

Let’s look at an example to help you understand:

#include <iostream>  
using namespace std;  
class Demo  
{  
    public:  
    int a;  
    int b;  
    int *p;  
  
    Demo()  
    {  
        p=new int;  
    }  
    Demo(Demo &d)  
    {  
        a = d.a;  
        b = d.b;  
        p = new int;  
        *p = *(d.p);  
    }  
    void setdata(int x,int y,int z)  
    {  
        a=x;  
        b=y;  
        *p=z;  
    }  
    void showdata()  
    {  
        std::cout << "value of a is : " <<a<< std::endl;  
        std::cout << "value of b is : " <<b<< std::endl;  
        std::cout << "value of *p is : " <<*p<< std::endl;  
    }  
};  
int main()  
{  
  Demo d1;  
  d1.setdata(40,50,70);  
  Demo d2 = d1;  
  d2.showdata();  
  return 0;  
}  

Output:

value of a is : 40   
value of b is : 50   
value of *p is : 70
Deep Copy
Deep Copy

Because a programmer has written its own Constructor in the example above, the phrase Demo d2 = d1; invokes the user-defined copy Constructor.

It duplicates the value types data as well as the object referred to by the pointer p. A reference type variable is not copied while using deep copy.

Differences between Copy Constructor and Assignment Operator(=):

Copy ConstructorAssignment Operator
It is an overloaded constructor.It is a bitwise operator.
It uses the old object to initialize the new one.It transfers a value from one object to another.
Syntax of copy constructor:
Class_name(const class_name &object_name)
{
// body of the constructor.
}
Syntax of Assignment operator:
Class_name a,b;
b = a;
1. When the new object is initialised with the previous object, the copy constructor is called.

2. The object is provided to the function as an argument.

3. It gives you the thing back.
When we assign an existing object to a new object, we use the assignment operator.
The distinct memory addresses are shared by both the current object and the new object.The memory location for both the existing and new objects is the same.
The compiler will automatically construct the implicit default Copy Constructor if the programmer does not declare it.The bitwise copy will occur if the “=” operator is not overloaded.
Differences between Copy Constructor and Assignment Operator(=)

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter