Content deleted Content added
No edit summary |
m Reverted edits by 223.230.231.214 (talk) to last version by Timtempleton |
||
Line 1:
In [[class-based programming|class-based]] [[object-oriented programming]], a '''constructor''' in a [[Class (computer programming)|class]] is a special type of [[subroutine]] called to [[object creation|create an object]]. It prepares the new object for use, often accepting [[argument]]s that the constructor uses to set required [[member variable]]s.
A constructor resembles an [[method (computer science)|instance method]], but it differs from a method in that it has no explicit [[return type]], it is not implicitly [[inheritance (object-oriented programming)|inherited]] and it usually has different rules for scope modifiers. Constructors often have the same name as the declaring [[class (computer science)|class]]. They have the task of [[initialization (computing)|initializing]] the object's [[data member]]s and of establishing the [[invariant (computer science)|invariant]] of the class, failing if the invariant is invalid. A properly written constructor leaves the resulting [[object (computer science)|object]] in a ''valid'' state. [[Immutable object]]s must be initialized in a constructor.
Line 14:
{{unreferenced section|date=June 2013}}
=== Parameterized constructors ===
Constructors that can take arguments are termed as parameterized constructors. The number of arguments can be greater or equal to one(1).
For example:
Line 368 ⟶ 370:
=== Object Pascal ===
In [[Object Pascal]], the constructor is similar to a [[factory method]]. The only syntactic difference to regular methods is the keyword <code>constructor</code> in front of the name (instead of <code>procedure</code> or <code>function</code>). It can have any name, though the convention is to have <
<source lang="delphi">
|