Default constructor

From Wikipedia, the free encyclopedia

A default constructor is a constructor that can be called with no arguments. [1]

In C++ and Java, if (and only if) no constructors (default or non-default) are explicitly defined for a class, then the compiler will provide an implicit default constructor, which is equivalent to a default constructor which is blank. Therefore, a class is not guaranteed to have a default constructor. Some programmers explicitly create one as a matter of habit, so they won't forget later, but this is not required.

In C++, arrays only have a default constructor, which constructs each of its elements using the default constructor for their type. It is an error to declare an array of a class type that does not have a default constructor.

In C++ and Java, if a derived class does not explicitly invoke a constructor of the base class (in C++ in the initializer list, in Java using super() in the first line), then the default constructor is implicitly invoked for the base class. If the base class does not have a default constructor, it is an error.

In C++, if an instance field of a class is not explicitly initialized in the initializer list, then the default constructor is invoked to initialize that field. If that type does not have a default constructor, it is an error.