Cpp Tutorial: Copy Constructor in Cpp

In this tutorial we shall understand about below topics.

1. Types of copy constructor
2. Overloaded copy constructor
3. Program for copy constructor
4. Why we need user defined copy constructor?
5. Why the argument to copy constructor should be pass by reference?
6. More complex example of user defined copy constructor:
7. what is the difference between copy constructor and copy assignment operator?
8. If there are no pointer as data member, then there is no need to create a user defined copy constructor.

1. Types of copy constructor?

There are 2 types of copy constructor.

Default Copy constructor: The compiler will create a default copy constructor, if there are no user defined copy constructor defined.

User Defined Copy Constructor: You can define your own user defined copy constructor.

2. Syntax of copy constructor:

Class_name(const class_name &old_object);

3. Program for copy constructor

#include <iostream>
// for more tutorials check www.prodevelopertutorial.com
using namespace std;

class MyClass 
{ 
    int num;
    
    public:  
        MyClass(int a) 
        {  
          num = a;  
        }
    
        // copy constructor
        MyClass(MyClass &obj) 
        {  
            cout<<"In copy constructor"<<endl;
            num = obj.num;  
        }  
        
        void display()
        {
            cout<<"Num = "<<num<<endl;
        }
};  

int main()  
{  
    MyClass obj1(20);               // Calling parameterized constructor.  
    MyClass obj2(obj1);                //  Calling copy constructor.  
    
    obj2.display();  
    return 0;  
} 

Output:

In copy constructor
Num = 20

4. Why we need user defined copy constructor?

Default constructor does only shallow copy.

User defined constructor will make a deep copy.

If you make a copy constructor private, the objects of that class becomes non copyable.

5. Why the argument to copy constructor should be pass by reference?

A copy constructor will be called when an object is passed by value.

As copy constructor itself is a function, if we pass the argument by value, a call to copy constructor would be made and it will become a non-terminating chain of calls.

Hence we need to call by reference.

6. More complex example of user defined copy constructor:

In this example, we have a pointer as a data member. As we are defining the copy constructor, we create a new memory location for the pointer. Hence the deep copy, instead of shallow copy.

We shall see both shallow copy and deep copy as an example:

Shallow Copy:

#include <iostream>  

// for more tutorial in C++ visit www.prodevelopertutorial.com
using namespace std;  
  
class MyClass  
{  
    int num;  
    int *ptr;
    
public:  
    MyClass()  
    {  
        ptr = new int;  
    }  
    
    void setdata(int x, int z)  
    {  
        num = x;  
        *ptr = z;  
    }  
    void display()  
    {  
        std::cout << "value of num is : " <<num<< std::endl;  
        std::cout << "value of *ptr is : " <<*ptr<< std::endl;  
    }  
};  

int main()  
{  
  MyClass d1;  
  d1.setdata(10, 20);  

  MyClass d2 = d1;  
  d2.display();  
  
    return 0;  
}  

Deep Copy:

#include <iostream>  

// for more tutorial in C++ visit www.prodevelopertutorial.com
using namespace std;  
  
class MyClass  
{  
    int num;  
    int *ptr;
    
public:  
    MyClass()  
    {  
        ptr = new int;  
    }  
    
    void setdata(int x, int z)  
    {  
        num = x;  
        *ptr = z;  
    }  
    void display()  
    {  
        std::cout << "value of num is : " <<num<< std::endl;  
        std::cout << "value of *ptr is : " <<*ptr<< std::endl;  
    }  
    
    MyClass(MyClass &d)  
    {  
        num = d.num;  
        ptr = new int;  
        *ptr = *(d.ptr);  
    }  
};  

int main()  
{  
  MyClass d1;  
  d1.setdata(10, 20);  

  MyClass d2 = d1;  
  d2.display();  
  
    return 0;  
}  

7. what is the difference between copy constructor and copy assignment operator?

A copy constructor is used to initialize a new object from some other object of the same class.

Example:

A(const A& obj): data(obj.data) { }

A obj;

A obj_1 = obj;// copy constructor will be called.

An assignment operator is used to replace the data of a previously initialized object with some other objects data.

A& operator= (const A& rhs)
{
	data = rhs.data;
	return.this;
}

A aa;
A a;

a = aa; // assignment operator
Write a Comment

Leave a Comment

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