A Copy Constructor creates an object by initializing it with a previously created object of the same class.
The Copy Constructor is used for the following purposes:
- Initialize an object of another object of the same type.
- Copy object and pass to function as an argument.
- Copy object and return from the function.
If the copy constructor is not defined in the class, it is defined by the compiler itself. If your class has pointer variables and dynamic memory allocation, you need a copy constructor.
Syntax of Copy Constructor:
Types of Copy Constructor:
- Default copy constructor
- User-defined copy constructor
1. Default Copy Constructor:
The compiler defines a default copy constructor. If the user does not define a copy constructor, the compiler will provide that constructor.
2. User Defined Constructor:
The developer defines a user-defined copy constructor.
Syntax of User-defined Copy Constructor:
Consider the following situation:
When Copy Constructor is called
The copy constructor is called in the following scenario:
- To initialize an object with another existing object of the same class type. For example, Student s1 = s2, where Student is a class.
- When objects of the same class type are passed as arguments by value.
- If the function returns an object of the same class type by value.
The constructor produces two types of copies.
1. Shallow copy
2. Deep copy
1. Shallow Copy Constructor:
The shallow copy constructor is used when the class is not handling dynamically allocated memory.
A shallow copy copies a reference to the original object. The compiler provides a default copy constructor. The default copy constructor provides a shallow copy, as shown in the following example. This is a bitwise copy of the object.
2. Deep Copy Constructor:
Deep copy allocates memory dynamically for the copy and then copies the actual value. Both the source and the copy have different memory locations. In this way, both the source and the copy are separate and do not share the same memory location. For a deep copy, you need to create a user-defined constructor.
How does the copy constructor work in C ++?
A copy constructor can be defined as a special type of constructor used to declare an object and use other objects to initialize it. A regular constructor is called when an object of that class is initialized. The overloaded parameter is called if the constructor is overloaded with some parameters instead of initializing the object and passing the same number of arguments. The copy constructor is an overloaded constructor, so you can call it when an object is initialized with an argument and the argument needs to pass an object value instead of a normal value.
When the overloaded constructor parameter detects that the received value is an object, the copy constructor is called and execution of the set of declarations defined within the copy constructor begins. You must create an object of the same class before using the copy constructor. One clear approach is that programs intended to implement a copy constructor also need a default or parameterized constructor so that they can create objects that help the copy constructor get involved …
Copy constructors are considered a special type of constructor that works with references to objects of the same class. It is used to provide some functionality to the application. Compared to the default constructor, the copy constructor is used less frequently in simple programs, but for the development of complex applications that need to be deployed in a production environment, the copy constructor is used there and the development of the application.
Intern & Technical Content Writer for Programmer’s Academy.