What is a copy constructor?

Asked 07-Dec-2019
Viewed 581 times

1 Answer


0

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);