18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Packages as classes 87<br />

The <strong>Ada</strong> specification of the class TUI is:<br />

package Class_TUI is<br />

type Menu_Item is ( M_1, M_2, M_3, M_4, M_Quit );<br />

type TUI is private;<br />

procedure Menu( The:<strong>in</strong> out TUI; M1,M2,M3,M4:<strong>in</strong> Str<strong>in</strong>g );<br />

function Event( The:<strong>in</strong> TUI ) return Menu_Item;<br />

procedure Message( The:<strong>in</strong> TUI; Mes:<strong>in</strong> Str<strong>in</strong>g );<br />

procedure Dialog(The:<strong>in</strong> TUI; Mes:<strong>in</strong> Str<strong>in</strong>g; Res:out Float);<br />

procedure Dialog(The:<strong>in</strong> TUI; Mes:<strong>in</strong> Str<strong>in</strong>g; Res:out Integer);<br />

private<br />

-- Not a concern of the client of the class<br />

end Class_TUI;<br />

For example, if an <strong>in</strong>stance of the TUI had been declared with:<br />

Screen : TUI;<br />

then, to setup the menu system:<br />

[a] Pr<strong>in</strong>t<br />

[b] Calculate<br />

Input selection:<br />

the follow<strong>in</strong>g code sequence would be used:<br />

Menu( Screen, "Pr<strong>in</strong>t", "Calculate", "", "" );<br />

Note:<br />

Null or empty menu items are not displayed.<br />

A str<strong>in</strong>g may be of any length. However, to store a str<strong>in</strong>g the receiv<strong>in</strong>g object must be of the correct<br />

size. <strong>Ada</strong> str<strong>in</strong>gs are fully discussed <strong>in</strong> Section 8.8.<br />

The user’s response to this menu is elicited with the function event. The function event returns an<br />

enumeration represent<strong>in</strong>g the menu item selected. For example, if the user selected option [b] then the code:<br />

case Event( Screen ) is<br />

when M_1 =><br />

when M_2 =><br />

--Pr<strong>in</strong>t<br />

--Calculate<br />

associated with label M_2 would be obeyed.<br />

Note:<br />

The selected menu item is <strong>in</strong>dicated by an enumeration M_1 for menu item 1, M_2 for menu item 2, etc.<br />

A programmer can display a message onto the TUI by us<strong>in</strong>g the procedure message which has the text to be<br />

output as its second parameter. Likewise, a programmer can <strong>in</strong>itiate a dialog with the user by us<strong>in</strong>g the procedure<br />

dialog that returns a float<strong>in</strong>g po<strong>in</strong>t number. The TUI currently only supports dialogs that solicit a float<strong>in</strong>g po<strong>in</strong>t<br />

number or <strong>in</strong>teger number.<br />

The fragment of code below illustrates the use of message and dialog <strong>in</strong>teractions <strong>in</strong> a program which converts<br />

miles to kilometres.<br />

Message( Screen, "Distance converter" );<br />

Dialog ( Screen, "Enter distance <strong>in</strong> miles", Miles );<br />

Message( Screen, "Distance <strong>in</strong> Kilometres is " &<br />

Float'Image( Miles * 1.6093 ) );<br />

Note:<br />

The operator & concatenates two str<strong>in</strong>gs <strong>in</strong>to a s<strong>in</strong>gle str<strong>in</strong>g. For example, "Hello" & " " &<br />

"world" delivers the s<strong>in</strong>gle str<strong>in</strong>g "Hello world".<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!