What is Copy Constructor in C++

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:

For example:

Practice Your Code Here

Types of Copy Constructor:

  1. Default copy constructor
  2. 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.

For example:

Practice Your Code Here

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.

For example:

Practice Your Code Here

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.

For example:

Practice Your Code Here

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 …

Conclusion:

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.

Recommended Articles:

  1. What is Constructor and Destructor in C++
  2. What is Encapsulation in C++
  3. What is Polymorphism in C++
  4. What is Inheritance in C++:
  5. What is Access Modifiers in C++

References:

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 114 times, 1 visits today)

5 1 vote
Article Rating
Subscribe
Notify of
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] What is Copy Constructor in C++ […]

trackback

[…] What is Copy Constructor in C++ […]

trackback

[…] What is Copy Constructor in C++ […]

trackback

[…] What is Copy Constructor in C++ […]

trackback

[…] What is Copy Constructor in C++ […]

You cannot copy content of this page
Scroll Up
5
0
Would love your thoughts, please comment.x
()
x