copy constructor in C++

Index:

  1. Definition
  2. Classification
  3. Constructor Copies

Let us begin by first understanding what a constructor is before we dive in what copy constructor in C++ is.

Definition: 

➢ It is defined as “A constructor is a special member function used to initialise objects of its class”. 

➢ It will be called automatically when the object is created. 

Let us see how a constructor is automatically executed at the time of object creation.

Output: 

Output

➢ In the above program, we created two objects ob1 and ob2, for the same class ABC. ➢ As you can see, we have called the display() function to execute, but we didn’t call for constructor ABC(). 

➢ A constructor is called automatically when we created the object ob1 and ob2, but for display(), we need to call unless we call it won’t execute explicitly. This is the advantage of using the constructor. It is easily accessible, and the user can easily understand the flow of the program.

Let us see the program to find the sum of N natural numbers. 

Output: 

➢ In the above example, we created an object ob1 which calls the sum() function automatically. 

➢ In the above example, we find the sum of the first five natural numbers, i.e., 1 to 5. ➢ Sum() is the constructor which is automatically called at the time of object creation. ➢ It gives us the result as 15 [1+2+3+4+5=15]. 

Classification

Constructors are classified as follows: 

I. Default Constructor. 

II. Parameterized Constructor. 

III. Constructor overloading. 

IV. Constructor with default arguments. 

V. Copy Constructor.

Let me give a brief introduction to the above topics. 

Parameterised constructor:

It is a constructor which has arguments or inputs. Syntax: 

Class_Name object_Name (argument1, argument2,…);

Constructor overloading: A class with two or more construct functions with the same name but with different parameters or arguments and other data types is called  Constructor overloading. 

let us discuss “Copy Constructors.” 

Copy Constructors:

Definition of copy constructor is given as “A copy constructor is a method or member function which initialise an object using another object within the same class”. 

A copy constructor is of two types: 

1. Default Copy Constructor. 

2. User Defined Copy Constructor. 

Default Constructor: C++ compiler will create a default constructor which copies all the member variables as it is when the copy constructor is not defined.  

User-Defined Constructor: The user defines the user-defined constructor. 

Syntax for user defined constructor: 

Class_Name (Class_Name &obj) { 

 // body of constructor 

 } 

Here obj is the reference that is being initialised to another object. 

Where copy constructors are used: 

1] When we initialise an object by another object of the same class. 

2] When we return an object as a function value

3] when the object is passed to a function as a non-reference parameter

Let’s see a program for copy constructor: 

User Defined Constructor

Output: 

Output

➢ In the above example, we are using both parameterised constructor and copy constructor. 

➢ In the above program, the variable ‘i’ is the object which stores the copy of variable x. 

Constructor Copies:

There are two ways in which copy constructor copies, and they are: 

i. Shallow copy. 

ii. Deep copy. 

Let’s go through these topics one by one. 

Shallow Copy

  • It is the process of creating a copy of an object by copying data of all the member variables as it is. 
  • Only a default Constructor produces a shallow copy. 
  • A Default Constructor is a constructor which has no arguments.

Let us understand deeply how it works through programming example

Output:

Output

Explanation for above one is given in figure below: 

 obj1 Shallow copy obj 2  

A  B  C 

12

 

 z 

Fig: illustration of shallow copy 

  • In the above figure, both ‘obj1’ and ‘obj2’ will be having the same input and both the object variables will be pointing to the same memory locations. 
  • The changes made to one object will affect another one. 
  • This particular problem is solved by using the user-defined constructor, which uses deep copy.   

Deep Copy: 

  • It dynamically allocates memory for the copy first and then copies the actual value. 
  • In a deep copy, both objects which have to copy and another which has to be copied will be having different memory locations
  • So, the changes made to one will not affect another. 
  • This is used by a user-defined copy constructor

The syntax for the user-defined copy constructor is mentioned above refer to that. Let us see the example for the same below 

Deep Copy

 Output: 

Output

 N1 N2 

n 100 x100

➢ In the above example, N1 and N2 are the two objects. ‘N2’ is the object which stores the value of objec’N1’. ‘N1’ takes 100 as input and will initialise to ‘N2’.  ➢ Both N1 and N2 will have different locations. 

➢ Changes made to one will not affect the other. 

 Copy Constructor vs Assignment Operator 

➢ C++ handles object copying and assignment through two functions called copy constructors and assignment operators

➢ C++ will automatically provide these functions if we do not define them; in many cases, we need to control manually how our objects are duplicated. 

Let us first look at the differences between assignment and initialisation. 

Consider the following code segment: 

 MYClass a; 

 MYClass b=a; 

➢ Here, the variable b is initialized to a because it is created as a copy of another variable. When b is created, it will go from containing garbage data directly to holding a copy of the value of a with no intermediate step.  

However, if we re-write the code as 

 MYClass a, b; 

 b=a; 

➢ Then two is assigned the value of one. Note that before we reach the line b=a, B already contains a value. This is the difference between assignment and initialisation.  When a variable is created is set to hold a new value, it is being assigned. 

 Note:

We can make the copy constructor private, but when we make the constructor private, that class’s objects cannot be copied. This is useful when our class has pointer variables or dynamically allocated resources. In such a situation, we can either write our copy constructor or make a private copy constructor so that the user gets compiler errors rather than something at runtime. 

This brings us to the end of the blog on Copy Constructor in C++. We hope that you were able to gain some knowledge from the same. If you wish to learn more such concepts about constructor in C++ or any other programming languages, you can join Great Learning Academy’s Free Online Courses today.

Also, if you are preparing for Interviews, check out these Interview Questions for C++ to ace it like a pro.

1

LEAVE A REPLY

Please enter your comment!
Please enter your name here

8 + 3 =