Default constructor
Encyclopedia
In computer programming languages the term “default constructor” refers to a constructor
that is automatically generated in the absence of explicit constructors (and perhaps under other circumstances); this automatically provided constructor is usually a nullary constructor
. In specification or discussion of some languages, “default constructor” may additionally refer to any constructor that may be called without arguments, either because it is a nullary constructor or because all of its parameter
s have default values.
, the standard describes the default constructor for a class as a constructor
that can be called with no arguments (this includes a constructor whose parameters all have default arguments). For example:
In C++, default constructors are significant because they are automatically invoked in certain circumstances:
In the above circumstances, it is an error if the class does not have a default constructor.
The compiler will implicitly define a default constructor if no constructors are explicitly defined for a class. This implicitly-declared default constructor is equivalent to a default constructor defined with a blank body. For example:
If some constructors are defined, but they are all non-default, the compiler will not implicitly define a default constructor. Hence, a default constructor may not exist for a class.This is the reason for a typical error which can be demonstrated by the following example.
As a constructor of type other than default is defined the compiler does not define a default constructor and hence the creation of object_2 leads to an error.
and C#, a "default constructor" refers to a nullary constructor
that is automatically generated by the compiler if no constructors have been defined for the class.The default constructor is also empty, meaning that it does nothing. A user defined constructor that takes no parameters is called a default constructor too.
Constructor (computer science)
In object-oriented programming, a constructor in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created...
that is automatically generated in the absence of explicit constructors (and perhaps under other circumstances); this automatically provided constructor is usually a nullary constructor
Nullary constructor
In computer programming, a nullary constructor is a constructor that takes no arguments.-Object-oriented constructors:In object-oriented programming, a constructor is code that is run when an object is created...
. In specification or discussion of some languages, “default constructor” may additionally refer to any constructor that may be called without arguments, either because it is a nullary constructor or because all of its parameter
Parameter (computer science)
In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments...
s have default values.
C++
In C++C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
, the standard describes the default constructor for a class as a constructor
Constructor (computer science)
In object-oriented programming, a constructor in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created...
that can be called with no arguments (this includes a constructor whose parameters all have default arguments). For example:
In C++, default constructors are significant because they are automatically invoked in certain circumstances:
- When an object value is declared with no argument list, e.g.
MyClass x;
; or allocated dynamically with no argument list, e.g.new MyClass
; the default constructor is used to initialize the object - When an array of objects is declared, e.g.
MyClass x[10];
; or allocated dynamically, e.g.new MyClass [10]
; the default constructor is used to initialize all the elements - When a derived class constructor does not explicitly call the base class constructor in its initializer list, the default constructor for the base class is called
- When a class constructor does not explicitly call the constructor of one of its object-valued fields in its initializer list, the default constructor for the field's class is called
- In the standard library, certain containers "fill in" values using the default constructor when the value is not given explicitly, e.g.
vector
initializes the vector with 10 elements, which are filled with the default-constructed value of our type.(10);
In the above circumstances, it is an error if the class does not have a default constructor.
The compiler will implicitly define a default constructor if no constructors are explicitly defined for a class. This implicitly-declared default constructor is equivalent to a default constructor defined with a blank body. For example:
If some constructors are defined, but they are all non-default, the compiler will not implicitly define a default constructor. Hence, a default constructor may not exist for a class.This is the reason for a typical error which can be demonstrated by the following example.
As a constructor of type other than default is defined the compiler does not define a default constructor and hence the creation of object_2 leads to an error.
Java and C#
In both JavaJava (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
and C#, a "default constructor" refers to a nullary constructor
Nullary constructor
In computer programming, a nullary constructor is a constructor that takes no arguments.-Object-oriented constructors:In object-oriented programming, a constructor is code that is run when an object is created...
that is automatically generated by the compiler if no constructors have been defined for the class.The default constructor is also empty, meaning that it does nothing. A user defined constructor that takes no parameters is called a default constructor too.