Copy Constructor in C++ with Examples

To understand the concept of a Copy Constructor, you need to understand what constructor is first. In programming, a constructor is said to be a member method called spontaneously as soon as an entity or object is generated. On the other hand, a copy constructor is a kind of a constructor or a method that initializes an entity or object via another object or entity of a similar class.

Open, and log in from the Ubuntu 20.04 Linux system. Make sure you have c++ compiler configured on your Ubuntu 20.04 system. If not, open the command-shell terminal via “Ctrl+Alt+T.” Now, install build-essential packages using apt first. It may require your sudo account password to install it. Add the password, and press Enter. Use the below query for this:

$ sudo apt install build-essential

After the installation of essential packages, it’s time to install the c++ language compiler. For that, use the apt package to install it. Utilize the below query in the shell:

$ sudo apt install g++

Now check the installed version of c++ compiler on your system. For that, use the below version command to do so.

$ g++ --version

Example 01:

You have to understand that just the shallow copy may be made with the standard copy constructor method. A shallow copy is described as creating a duplicate of an entity by replicating all or most of the component variables’ information in their current state. To see the illustration and working of the shallow copy using a copy constructor, let’s begin with an example. First of all, create a new c++ file using the “cc” extension and touch command. The command is as follows:

$ touch main.cc

Open the newly created file “main.cc” in a GNU editor to add c++ code in it using the below code.

$ nano main.c

Now the file has been opened, write out the below code in it. We have included the input-output standard stream package in the code first. Added a namespace and created a class “Test.” In this class, we have defined integer type variables x, y, and z. Then we have used a constructor method to give some memory space to pointer z. Data has been used to assign values to integers a, b, and pointer variable z. The method Show() has been used to print values assigned to variables. The main function is used to start the compilation of a code. We have created a single object, t1, for a class Test. Using this object, we have passed some values to the function “Data.” Then we have used the copy constructor way to copy one constructor to another. Then Show() method has been called using a second object to print the values of integers. Because no function Object() has been provided in the example below, the phrase Demo t2 = t1; invokes the compiler’s default function Object(). The default function Object() makes a deep or exact duplicate of an existing entity. As a result, both object’s pointer “z” refers to the very same memory address. As a result, when one field’s storage is released, the other field’s storage is also liberated since both fields link to the same address space. Save the file via Ctrl+S ad quit it using Ctrl+X to compile the code.

Compile your c++ code in a shell via g++ compiler as below.

$ g++ main.cc

Let’s execute the file to see the results of the copy constructor shallow copy method. For that, try out the following query:

$ ./a.out

The output shows the same values which are being passed to variables.

Example 02:

This time we will be using Deep copy illustration using the copy constructor. Deep copy reserves space for replica dynamically before copying the real value; the original and replica have separate memory addresses. Both the original and the replica will be different in this sense, and they’ll never occupy similar storage space. The user-defined function Object() must be written for a deep copy. Open the file main.cc once again by the below command.

$ nano main.cc

All the code is the same in this example with a little change. Because we built our constructor in the scenario below, named “Test,” and passed the other constructor in the parameter binding the object with it. The phrase Demo t2 = t1; uses the user-defined copy function Object(). It duplicates the content types data as well as the entity referred to by the z pointer. The reference type mutable is not copied while using deep copy. Save your c++ code and close the file.

Now compile the main.cc file via the following command:

$ g++ main.cc

Execute your code and see the result as below. The output is shown below.

$ ./a.out

Example 03:

Here we have another example for copy constructor in our guide. Open the same file to update our code using the below-stated instruction.

$ nano main.cc

Now the file has been opened in the GNU editor, update your code with the below-shown c++ language script. We have included input-output stream first in the code, then used a namespace as standard. We have created a class named “Class” and initialize two private integer type data members a, and b. Then we have 4 public methods. Two of them are constructors, and the other two are integer type get() methods. The first constructor is simple, while the second constructor creates a deep copy using the first constructor object “c1”. A method getA() is returning the value of variable “a” and the other, getB() method is returning the value of variable “b” to the main method. The main method has created an object of a first constructor and passed parameter values to the constructor. Then, we used the technique of copy constructor to copy one constructor to another. The values have been printed out in the “cout” statements using both objects separately.

Compile and execute the above code with the stated queries. The output shows the different values for both the objects in the shell.

$ g++ main.cc

$ ./a.out

Example 04:

To better understand the concept of copy constructor, we have another example. Open your file to update it.

$ nano main.cc

We have created a new class, “Room,” and added some private data members “l” for length and “h” for height. The first constructor is a simple constructor to initialize values by taking from the object. Another constructor is using the first constructor object by binding. The double type method Area() has been used to calculate the area of a room. The main function is passing values to the first constructor and printing the area of a room via the first object. Then a constructor has been copied, and then the values have been printed out via a second object.

Compile the code.

$ g++ main.cc

The execution of a code shows the presented below results.



We have covered the concept of a copy constructor with examples in our guide. We have also elaborated the idea of shallow copy and deep copy in this tutorial. We hope you find this guide helpful.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.