C++

Copy Constructor Array in C++

Copy constructors are constructors that are being used to duplicate the current object of a specific class. It’s generally in the format Z (Z&), where Z is the class name. All classes have a standard copy constructor specified by the compiler. The copy constructor is identical to the standard constructor, with the exception that the argument would be another array element, and the recently created array has been adjusted to the dimensions of the parameter’s data type and declared to the parameter’s array contents. By using different examples, we are going to discuss the copy constructor and its categories in this article.

Using the Copy Constructor

In C++, the copy constructor is used to duplicate the content from one object to the other. Now, let us look at an example to demonstrate how to use the deep copy constructor.

First of all, we integrated the header file for input and output functionalities. After this, we used the standard namespace. Now, we are going to declare a class ‘Room’. We initialized the length and width of the room and set it in private. The data type of the length and width is ‘double’ here.

Now, we declared the variable’s ‘length’ and ‘width’ by using the parameterized constructor. Here, we passed these parameters to the ‘Room’ constructor and this is set to be public. In addition to this, we utilized a copy constructor. We copy the content of the ‘obj’ argument. The function calculateArea() is being called to find the area of the room by multiplying the value of the length by the value of the height.

In the subsequent step, we declared the main() function. Here, we constructed an object of the ‘Room’ class and also specified the values of its length and width. Now, you just have to copy the data from ‘Room1’ to ‘Room2’. After this, we utilized ‘cout’ to display the areas of both rooms. In the end, the return0 is used to terminate the program.

Use the Shallow Copy Constructor

When a class does not interact dynamically with memory allocation, the shallow copy constructor is being used. Two objects in the shallow copy constructor would refer to a similar memory address. References to actual items are replicated in a shallow copy. A standard copy constructor is specified by the compiler. It’s a bit-by-bit replica of a thing. Below, an instance is being used to illustrate the notion of a shallow copy constructor.

At the start of the program, we introduced two required libraries, <iostream> and <cstring>. Along with this, we also utilized a standard namespace. Now, we created a class named ‘computer’. We constructed a pointer of the class and set its data type ‘character’.

We publicly declared the function of the class ‘computer’ and here we passed the defined constructor as a parameter to this function. We created a dynamic memory allocation here and set it equal to the variable of the pointer. It permits the user to create a memory for an attribute or even an array in the program.

Moreover, we applied the void concatenate() function. To concatenate two strings, we utilized the strcat() method. Now, we declared a copy constructor of class ‘computer’. To show the output, we utilized the void display() function along with the ‘cout’ statement. Now it’s time to call the main() function. We utilized the copy constructor inside the body of the main() function. It is termed a constructor because it is being utilized to generate an object.

So, the copy constructor as the name implies, generates a new object that is an identical replica of the original copy. We created two objects and specified the values of these objects. Then, we applied the display() function individually on these objects to get the result.

In the next step, a1 is tempting to concatenate hence, we applied the ‘a1.concatenate()’ function. Here, we also set a value ‘Technology’ for concatenation. We again utilized the display() method to get the output. Now, we end the program by using ‘return 0’.

Use the Deep Copy Constructor

Deep copy requires a unique memory space for duplicated data. As a result, the original and copy are distinct. Modifications implemented in one memory region have no impact on the replica. We would utilize a user-defined copy constructor while constructing dynamic memory with pointers. Both entities would refer to separate locations in the memory.

In addition, we make use of the standard namespace. We’ll now make a class called ‘ProgrammingLanguage.’ We created a class pointer and defined its data type to ‘character.’ Then, we defined the function of the class ‘ProgrammingLanguage’ as public, and we provided the specified constructor to it as an argument.

Here, we constructed a dynamic memory allocation and allocated it equivalent to the pointer variable. It enables us to acquire storage for a data set or an array in the program. The function is called here, and the constructor of the ProgrammingLanguage class is given as a parameter. We have been utilizing the void concatenate() method.

Further, we will utilized the strcat() technique to concatenate two strings together. Now, we created a copy constructor of the class ‘ProgrammingLanguage’. The data is displayed by using the void display() method and the ‘cout’ command. The main() function will now be called. In the body of the main() function, we have used the copy constructor. This duplicates a predefined object. Hence, we wouldn’t normally want to modify the actual object. We make two objects and assign the object ‘a1’ the value ‘Python.’ The object ‘a2’ is equal to the object ‘a1’ in the next phase.

To obtain the output, we have been using the display() method on each of these objects individually. Now, we utilized the ‘return 0’ command to exit the program.

Conclusion

In this article, we have clarified the working of a copy constructor in C++ language. Whenever an object is made, a constructor is a specific form of a derived class that is declared instantly. Copy constructors are those constructors that will be utilized to replicate an element of a specified class that already exists. We also observed how to use shallow copy constructor and deep copy constructor with the help of different instances.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content