Content deleted Content added
No edit summary |
ClueBot NG (talk | contribs) m Reverting possible vandalism by 185.192.230.85 to version by Chmielo13. Report False Positive? Thanks, ClueBot NG. (3361592) (Bot) |
||
Line 1:
{{refimprove|date=August 2010}}
{{ProgLangCompare}} ▼
In [[class-based programming|class-based]] [[object-oriented programming]], a '''constructor''' (abbreviation: '''ctor''') is a special type of [[subroutine]] called to [[object creation|create an object]]. It prepares the new object for use, often accepting [[Parameter_(computer_programming)|arguments]] 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 [[Class invariant|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.▼
Most languages allow [[method overloading|overloading]] the constructor in that there can be more than one constructor for a class, with differing parameters. Some languages take consideration of some special types of constructors. Constructors, which concretely use a single class to create objects and return a new instance of the class, are abstracted by [[Factory (object-oriented programming)|factories]], which also create objects but can do so in various ways, using multiple classes or different allocation schemes such as an [[object pool]].
== Types ==▼
{{unreferenced section|date=June 2013}}▼
=== Defined constructors ===▼
Constructors that can take at least one argument are termed as parameterized constructors.▼
For example:▼
<source lang="cpp">
class Example
Line 17 ⟶ 31:
}
</source>
▲{{ProgLangCompare}}
▲In [[class-based programming|class-based]] [[object-oriented programming]], a '''constructor''' (abbreviation: '''ctor''') is a special type of [[subroutine]] called to [[object creation|create an object]]. It prepares the new object for use, often accepting [[Parameter_(computer_programming)|arguments]] 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 [[Class invariant|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.
▲== Types ==
▲{{unreferenced section|date=June 2013}}
▲=== Defined constructors ===
▲Constructors that can take at least one argument are termed as parameterized constructors.
▲For example:
When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly. The method of calling the constructor implicitly is also called the ''shorthand'' method.
<source lang="cpp">
|