Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Constructor (object-oriented programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Eiffel === In [[Eiffel (programming language)|Eiffel]], the routines which initialize new objects are called ''creation procedures''. Creation procedures have the following traits: * Creation procedures have no explicit return type (by definition of ''procedure'').{{Efn|Eiffel ''routines'' are either ''procedures'' or ''functions''. Procedures never have a return type. Functions always have a return type.}} * Creation procedures are named. * Creation procedures are designated by name as creation procedures in the text of the class. * Creation procedures can be explicitly invoked to re-initialize existing objects. * Every effective (i.e., concrete or non-abstract) class must designate at least one creation procedure. * Creation procedures must leave the newly initialized object in a state that satisfies the class invariant.{{Efn|Because the inherited class invariant must be satisfied, there is no mandatory call to the parents' constructors.}} Although object creation involves some subtleties,<ref name="eiffel standard">{{Cite web|url=http://www.ecma-international.org/publications/standards/Ecma-367.htm|title=Eiffel ISO/ECMA specification document}}</ref> the creation of an attribute with a typical declaration <code lang="eiffel">x: T</code> as expressed in a creation instruction <code lang="eiffel">create x.make</code> consists of the following sequence of steps: * Create a new direct instance of type <code lang="eiffel">T</code>.{{Efn|The Eiffel standard requires fields to be initialized on first access, so it is not necessary to perform default field initialization during object creation.}} * Execute the creation procedure <code lang="eiffel">make</code> to the newly created instance. * Attach the newly initialized object to the entity <code lang="eiffel">x</code>. In the first snippet below, class <code lang="eiffel">POINT</code> is defined. The procedure <code lang="eiffel">make</code> is coded after the keyword <code lang="eiffel">feature</code>. The keyword <code lang="eiffel">create</code> introduces a list of procedures which can be used to initialize instances. In this case the list includes <code lang="eiffel">default_create</code>, a procedure with an empty implementation inherited from class <code lang="eiffel">ANY</code>, and the <code lang="eiffel">make</code> procedure coded within the class. <syntaxhighlight lang="eiffel"> class POINT create default_create, make feature make (a_x_value: REAL; a_y_value: REAL) do x := a_x_value y := a_y_value end x: REAL -- X coordinate y: REAL -- Y coordinate ... </syntaxhighlight> In the second snippet, a class which is a client to <code lang="eiffel">POINT</code> has a declarations <code lang="eiffel">my_point_1</code> and <code lang="eiffel">my_point_2</code> of type <code lang="eiffel">POINT</code>. In procedural code, <code lang="eiffel">my_point_1</code> is created as the origin (0.0, 0.0). Because no creation procedure is specified, the procedure <code lang="eiffel">default_create</code> inherited from class <code lang="eiffel">ANY</code> is used. This line could have been coded <code lang="eiffel">create my_point_1.default_create</code> . Only procedures named as creation procedures can be used in an instruction with the <code lang="eiffel">create</code> keyword. Next is a creation instruction for <code lang="eiffel">my_point_2</code>, providing initial values for the <code lang="eiffel">my_point_2</code>'s coordinates. The third instruction makes an ordinary instance call to the <code lang="eiffel">make</code> procedure to reinitialize the instance attached to <code lang="eiffel">my_point_2</code> with different values. <syntaxhighlight lang="eiffel"> my_point_1: POINT my_point_2: POINT ... create my_point_1 create my_point_2.make (3.0, 4.0) my_point_2.make (5.0, 8.0) ... </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)