Introduction to Eiffel

Hugh Gibbons & Colm Ó Dunlaing

 

Bottles of Beer ditty

99 bottles of beer on the wall, 99 bottles of beer,
Take one down and pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall, 98 bottles of beer,
Take one down and pass it around, 97 bottles of beer on the wall.

97 bottles of beer on the wall, 97 bottles of beer,
Take one down and pass it around, 96 bottles of beer on the wall.

...

2 bottles of beer on the wall, 2 bottles of beer,
Take one down and pass it around, 1 bottle of beeer on the wall.

1 bottle of beer on the wall, 1 bottle of beer,
Take one down and pass it around, no bottles of beer on the wall
Go to the store and buy some more,

99 bottles of beer on the wall...

Rather than writing out all 100 verses we can write a program that will do this job.

 

Never Ending Story

It was a cold and frosty night and the soldiers were sitting by the fire and the captain said

"Private, tell us a story"

The private began,

" It was a cold and frosty night ...

 

Structure of Eiffel Programs

 

Class <class-name>

creation -- optional

<routine name> -- usually called 'make'

feature

features

i.e. <attributes> and/or <routines>

end -- class class-name

A feature is either an attribute or routine.

Attributes

Attributes are where the data to be manipulated is stored. An attiribute can refer to objects or items of basic type (integer, boolean etc) or it can refer to more complex objects from any class, e.g. a : ARRAY[INTEGER] or s:STRING.

Routines

A routine (or a method) is either a procedure or a function.

Classes and Types

For our purposes, classes and types are the same,

e.g. s:STRING

We can say s is of type STRING or we can say that s is a reference to an object from the STRING class.

Eiffel comes with large (clusters of) libraries of classes, e.g. ARRAY, STRING,

clusters:

KERNEL: any, exceptions, array, string, file, comparable, numeric, hashable, none.
DATA STRUCTURES: list, circular, set, tree, hash_table, stack, queue.
WINDOWS: window, popup_menu, scroll_menu, command, text, text_editor.
GRAPHICS: bitmap, v_scroll_bar, select_box, rgb_pixel, figure, point, triangle, ellipse
etc.

Eiffel Control Structures

if <boolean> then

<then-statements>

else

<else-statements>

end

from

<init-statements>

until

<boolean>

loop

<body of loop>

end 

 

'Pascal-like' program for Bottles of Beer 

'Pascal' like version is here

 

OO version of Bottles of Beer 

The OO version is here

The OO verson involves two classes, the root class, OO_BEERS.

The class, OO_BEERS, make use of another class, WALL.

We call the class, OO_BEERS, the client class and the WALL class, the supplier class as it suplies access to its features.