When do we call a copy constructor?
In the C++ programming language, we may call a copy function in the following situations. It is called when we return an object of the class by the value. A copy constructor is also recommended to be used in the condition when we pass an object of the class by a value as a parameter when we create an object that is created by depending on the other object belonging to the same class.
Types of a copy constructor
There are two types of copy constructors.
copy constructor (default)
If the user does not define any copy constructor, then, in this case, the compiler supplies its constructor.
The programmer always defines the user-defined constructor.
When do we need a copy constructor?
If our copy constructor is not defined, then the compiler C++ creates the copy constructor that is the default one. We need to define our constructor when an object uses pointers, dynamic memory allocation, or other resources.
// Constructor body
After using the copy constructor, we obtain two copies. One is known as a shallow copy, while the second is a deep copy. The shallow copy is produced by the default copy constructor. Whereas the deep copy constructor allocates the memory automatically, it copies the actual value. The copied and actual values are stored in different places inside the memory. It is said that a deep copy is required while creating a user-defined constructor.
Implementation of a copy constructor
We have implemented the code in the text editor of Ubuntu, and the resultant value is obtained by execution on the Linux terminal.
This program will use the object-oriented programming concept, as constructors are used in making classes. A class is created with the private portion having integer type values. In the public portion, a constructor is created with the name of the class. This constructor will use two values in the parameter to receive the value passed from the main program. Inside the constructor, the sent values are assigned to the new variables.
After the constructor, again, a new constructor is created. It is a copied constructor. As we have described above, this constructor takes the class name, and the object created.
Then we have used two functions, getx() and get(), which will return the variables’ value. In the main program, constructors are called by using the class name, each (original), and the copy constructor contains arguments in the parameter while calling. The normal constructor contains two values that are passed to the variable. And the copy constructor is assigned with the object of the first class. To display the resultant value, we will call the get function by using objects of both constructors.
We will save the code and execute it by using the compiler G++.
On execution of the source code, you can see that both values are the same that the constructors passed either from the original one or by the copy constructor.
Linked List Concept in C++
A linked list is a data structure that contains several nodes connected through the addresses stored in each node.
struct node *next_part;
We create a structure having a data part that stores the values in it and the next part that stores the address of the adjacent node. The next step we do is to initialize the nodes in the main program. All the nodes are declared as NULL by using the pointers.
Each data part of the node is assigned by the values. This is done by accessing that part of the node.
Similarly all the nodes get their relevant data.
The main thing in the linked list is the connection between the nodes created. This is done when the next part of one node is assigned with the address of the second node; similarly, the second node contains the address of the third one. And so on, this process continues till the last node. The last node’s next part is declared null as there is no further node here.
The copy constructor and the linked list in C++
For using a copy constructor in the linked list, we have used a structure to create the node. After closing the structure, a class with the name linked_list is used here. The private part will contain the variables of a pointer-type head and a tail. A normal constructor assigns the values to the head and tail parts in the public part.
Two integer-type variables will accept the values that will be sent from the main program by creating the object. These variables are assigned to the head and tail variables to store values in them. After the normal constructor, a copy constructor is declared. This will be of the same name, and its parameter will contain a constant class name with the object. This constructor is also assigned with the same values. But these are accessed with the objects. We will get these values by using the get() function. Both the values in the variable will be returned.
After the constructor, a simple function of adding a node to the existing node is used. A new node of temp name is created. The data part is assigned with the value. And the next part is declared as NULL. Here we check if the node added is the first one or the linked list already contains a node in it. So an if-else statement is used here. It will check if the head is equal to null, then the head and the tail are allotted the value of “tmp”. But in another case, if they are not null, then the tail’s next part is assigned with the address of a new node. It means “tmp” will contain the address of the tail. And the tail will get the value updated.
Now in the main program, we will create the object of the class; by creating the object, constructors are called. The original and the copy constructor will contain the parameter values. The copy constructor creates the object and is assigned with the object of the original constructor.
These values will be displayed. To see the resultant value, execute the program on the terminal. You can see that both the constructor have the same values.
This article explains the working and creation of a copy constructor in the linked list using C++. This is done by explaining the copy constructor, its types, and usage in a simple program demonstrated in the example. Further, the concept of linked list creation is also explained. A combined example of a linked list with the copy constructor is used to clear every ambiguity of the end-user.