What is a copy constructor?

0 votes
183 views
Shikhar Arora asked 07-Dec-2019 in Technology by Shikhar Arora

What is a copy constructor

1 Answer

0 votes
Sangsaptak Pal answered 25-Jan-2020 by Sangsaptak Pal

A constructor is the member function of a class which initializes objects of a class. It is automatically invoked during the time of object creation of the class. It is the special member function of the class.

A constructor although looks a lot like a normal function differs from that in the following ways:

  • A constructor has the same name as the class
  • A constructor does not have a return type
  • A constructor is automatically or implicitly invoked at the time of object creation
  • If the programmer does not explicitly specify a constructor, the compiler automatically defines a constructor implicitly

There are majorly three types of constructors namely:

  1. Default or unparameterized constructors : Those constructors which do not take any arguments
  2. Parameterized constructors : Those constructors which takes arguments
  3. Copy constructors : Those constructors which initialize an object using another object of the same class. The syntax is as follows:

ClassName (const ClassName &old_obj);