This series of Eiffel source files (obtained from the SmallEiffel distribution) is the definition for an abstract animal, an abstract quadrupede and three defined classes. This will give the reader an overview in the class hierarchy functionality of Eiffel.
Animal.e - Base definition. Notice the deferred keyword - it signals the programmers intent to implement the code later on. cri is a deferred member variable, while crier is a deferred function. See the Result and Void keywords used in reproduction. Notice the strange quirk in reproduction_avec_quadrupede() that the Animal class has explicit prior knowledge of a child class (in this case, Quadrupede). In the felicitations function, notice the Current keyword - analogous to the this keyword in C++ and Java. One other thing to be aware of when reading this is the Eiffel does not explictly require all deferred functions or variables to resolved. Only the ones that are required (used) are needed. There is no definition of cri in any of the following code.
deferred class ANIMAL feature {ANY} cri: STRING is deferred end; crier2 is do std_output.put_string(cri); end; crier is deferred end; reproduction(autre: ANIMAL): ANIMAL is require autre /= Void do Result := autre; end; reproduction_avec_quadrupede(autre: QUADRUPEDE): ANIMAL is require autre /= Void do Result := autre; end; felicitations(autre: ANIMAL) is do Current.crier2; std_output.put_string(" + "); autre.crier; std_output.put_string(" = "); Current.reproduction(autre).crier; std_output.put_new_line; end; end -- ANIMAL
Quadrupede.e - Quadrupede definition Quadrupede is a class that inherits from the Animal class described above. Notice that it redefines reproduction even though it is not a deferred function (this is just a function overload).
deferred class QUADRUPEDE inherit ANIMAL redefine reproduction end; feature {ANY} reproduction(autre: ANIMAL): ANIMAL is do Result := autre.reproduction_avec_quadrupede(Current); end; end -- QUADRUPEDE
Chat.e - Inherits from Quadrupede Chat inherits from Quadrupede and implements both the crier function (which was deferred in Animal) and overloads the reproduction_avec_quadrupede function.
class CHAT inherit QUADRUPEDE redefine reproduction_avec_quadrupede end; feature {ANY} reproduction_avec_quadrupede(quadrupede: CHAT): CHAT is do Result := Current; end; crier is do std_output.put_string("MIAOU"); end; end -- CHAT
Chien.e - Inherits from QuadrupedeChien inherits from Quadrupede and implements the crier function (which was deferred in Animal).
class CHIEN inherit QUADRUPEDE; feature {ANY} crier is do std_output.put_string("OUARF"); end; end -- CHIEN
Mille_Pattes.e - Inherits from Animal Mille_Pattes inherits from Animal and implements the crier function.
class MILLE_PATTES inherit ANIMAL; feature {ANY} crier is do std_output.put_string("SCOLO"); end; end -- MILLE_PATTES
Sample1.e - Test Driver Number 1 Notice that the !! construct is Eiffel's new keyword. It will force the initialization of the the variable at this time. make is the defined main function (as defined by the creation keyword).
class SAMPLE1 creation {ANY} make feature {ANY} chien: CHIEN; chat: CHAT; mille_pattes: MILLE_PATTES; make is do !!chat; !!chien; !!mille_pattes; chat.felicitations(chien); mille_pattes.felicitations(chat); end; end -- SAMPLE1
Sample2.e - Test Driver Number 2 Just another example.
class SAMPLE2 creation {ANY} make feature {ANY} chat: CHAT; mille_pattes: MILLE_PATTES; make is do !!chat; !!mille_pattes; chat.felicitations(chien); mille_pattes.felicitations(chat); end; end -- SAMPLE2
Sample3.e - Test Driver Number 3 This is probably the most interesting of the sample test drivers. It will store the animals into an array which it will then access the first element in the array and call its crier function. However, in the second array, both elements are the same.
class SAMPLE3 creation {ANY} make feature {ANY} make is local chat1, chat2: CHAT; t_chat: ARRAY[CHAT]; medor: CHIEN; t_chien: ARRAY[CHIEN]; do !!chat1; !!chat2; t_chat := <<chat1,chat2>>; t_chat.item(1).crier; !!medor; t_chien := <<medor,medor>>; t_chien.item(1).crier; end; end -- SAMPLE3
This notice does not appear in any of these original source files, but it does appear in most of the other sample files in SmallEiffel. So, I will just put it here in case it was omitted. It also allows me to say I had nothing to do with this code - did you think I knew French?
-- This file is part of SmallEiffel The GNU Eiffel Compiler. -- Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE -- Dominique COLNET and Suzanne COLLIN - colnet@loria.fr -- http://www.loria.fr/SmallEiffel -- SmallEiffel is free software; you can redistribute it and/or modify it -- under the terms of the GNU General Public License as published by the Free -- Software Foundation; either version 2, or (at your option) any later -- version. SmallEiffel is distributed in the hope that it will be useful,but -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- for more details. You should have received a copy of the GNU General -- Public License along with SmallEiffel; see the file COPYING. If not, -- write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, -- Boston, MA 02111-1307, USA. --
Last Modified Sunday, 05-Aug-2018 12:51:50 EDT These pages were made by Justin R. Erenkrantz unless otherwise stated. This work is licensed under a Creative Commons License. These pages will look best in an XHTML 1.0 compliant browser. |
|