In
object-oriented programming, a
constructor is code that is run when an
object is created.
Default constructors of objects are usually nullary.
Java example public class MyInteger { private int data; // Nullary constructor public MyInteger() { this(0); } // Non-nullary constructor public MyInteger(int value) { this.data = value; } int getData() { return data; } void setData(int value) { data = value; } }
C++ example class Integer { private: int data; public: // Default constructor with parameters // Leaving parameters unspecified defaults to the default value Integer(int value = 0): data{value} {}
nodiscard int getData() const noexcept { return data; } void setData(int value) noexcept { data = value; } } ==Algebraic data types==