C++ Copy Constructor3 min read

A copy constructor in C++ is used to create a copy of an already existed object. It is also used to Initialize one object from another of the same type.

There are two types of copy constructor:

  • Default Copy constructor
  • User-Defined constructor

Default Copy constructor:

The default copy constructor is defined by the compiler when the user do not define a copy constructor.

Syntax for Default Copy constructor:

let us go through the program in C++.

C++ Program of the copy constructor

Output:


Shallow Copy Constructor

A shallow copy is a process where the copy of an object is created by copying all the member’s data exactly as it is. Default copy constructor produces the shallow copy constructor.

Both the objects that are the original and copy point at the same memory. Since, both the object points to the same memory location, changes made by one will be reflected on other. Now since we only need to make a replica of the original object, shallow does not fully fill this purpose.

Let us understand through an example in C++.

Output:

Shallow Copy constructor

Since the pointer breadth of both the objects (original and copied) points to the same memory location, freeing one of the memory will free the other one too. So this is solved by the Deep Constructor.


Deep Copy Constructor

A deep copy constructor is used to overcome the problem caused by the Shallow constructor. In the deep constructor, it is like having a copy of a project but both the original and copied project are kept in separate files, that is the changes made in one of the files will not affect the other.

A deep copy will allocate separate memory space for copied file. A user-defined copy constructor is used for the Deep copy constructor and assign dynamic memory as well if required.

Let us understand through an example in C++.

Output:

Deep copy constructor

As you can see in the program above, we use destructor to destroy the dynamically allocated memory.


MORE

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More