Table of Contents
Home / Definitions / Constructor
Development 2 min read

A constructor initializes an object in object-oriented programming. In many programming languages, a constructor has the same name as the class in which it creates the object. A programmer will typically declare a constructor within the class and then define the data types that make up the ensuing object.

Common types of constructors

Default constructors: what the program automatically inserts if no constructor is defined. This depends on the language, but typically a default constructor sets everything within the new object to its default value (such as 0) and defines no arguments. It can also set default parameters but is still considered a default constructor.

Parameterized constructor: has parameters which identify arguments once a new object is initialized. The program automatically calls a constructor, which a programmer designs with certain parameters.

Copy constructor: creates a new object by making a copy of a preexisting one. Typically these objects must be in the same class.

Conversion constructor: does not have a specific (explicit) declaration; instead, the program implies the declaration. If a constructor is only given one parameter, the program may convert an object to another class or type implicitly. This is also referred to as a converting constructor.

Move constructor: transfers a pointer to different data or assigns the data to another object, instead of using a copy constructor. The data type moves rather than being copied.






Was this Article helpful? Yes No
Thank you for your feedback. 0% 0%