Constructor (object-oriented programming): Difference between revisions

Content deleted Content added
m Reverted 1 edit by 47.31.109.240 (talk) to last revision by 43.228.94.170. (Swan)
No edit summary
Tag: extraneous markup
Line 1:
<ref></ref>{{refimprove|date=August 2010}}
{{ProgLangCompare}}
In [[class-based programming THIS IS THE FAKE REPORT PLEASE CONTACT TO OTHER SITES|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.