50 likes | 145 Views
Eiffel examples. Example 1. class C creation default_create feature {ANY} -- the public class data count : INTEGER element : DOUBLE end ;. features - the data an object may store and operations that may be performed on an object. feature {ANY} = feature
E N D
Example 1 class C creation default_create feature {ANY}-- the public class data count: INTEGER element: DOUBLE end; • features - the data an object may store and operations that may be performed on an object. • feature{ANY} = feature • feature{NONE} = feature{} • default_create – default creation function
Example 2 class C creation feature {NONE}-- the private class data add:INTEGERis -- routine with returned value do Result :=count +1 -- returned value end feature -- the public class data count :INTEGER increase(c :INTEGER ) :INTEGERis -- argument and returned value do count := c Result :=add -- returned value end end; • The {NONE} appearing after the feature reserved word states that all of the following features are available to objects who are of type NONE.
Example 2 (cont) class MAIN creation make feature {NONE}-- the private class data make is local c : C do !!c -- c creation (= create c) print(c.increase(2).out + “,”) print(c.count.out) end end; • Output – 3,2
Example 3 class C creation -- Constructors definitions make, make_init feature count: INTEGER make is -- Constructor without argument do count :=0 end make_init (c :INTEGER ) is -- Constructor with argument do count :=c end end; Constructor don't need to have the same name as the class. Class can have multiple constructors with different names.