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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

25 <strong>Ada</strong> <strong>in</strong>troduction: Part 1<br />

3 <strong>Ada</strong> <strong>in</strong>troduction: Part 1<br />

This chapter looks at some simple <strong>Ada</strong> programs, and presents the basic control structures of the<br />

language. The data types Integer and Character are used to <strong>in</strong>troduce these structures.<br />

3.1 A first <strong>Ada</strong> program<br />

The first program presented is a simple one that writes the message ‘Hello World’ onto the user’s term<strong>in</strong>al.<br />

with Text_Io;<br />

use Text_Io;<br />

procedure Hello is<br />

beg<strong>in</strong><br />

Put("Hello World"); New_L<strong>in</strong>e;<br />

end Hello;<br />

Note:<br />

The example programs <strong>in</strong> this book are shown with reserved words <strong>in</strong> bold to aid readability. As the<br />

name suggests, reserved words can only be used for their <strong>in</strong>tended purpose. Strange error messages<br />

can occur when a reserved word is <strong>in</strong>advertently used by the programmer as the name of an object <strong>in</strong> a<br />

program. Reserved words are entered as normal text when writ<strong>in</strong>g a program. Section B.1, Appendix B<br />

lists all the reserved words <strong>in</strong> the <strong>Ada</strong> programm<strong>in</strong>g language.<br />

When compiled and run, this program will display on a user’s term<strong>in</strong>al the message:<br />

Hello World<br />

In the above program, the reserved words beg<strong>in</strong> and end are used to bracket the body of the procedure<br />

Hello. In <strong>Ada</strong>, a procedure can be a self-conta<strong>in</strong>ed program unit that may be <strong>in</strong>dependently compiled. In the<br />

above example, the s<strong>in</strong>gle procedure Hello forms a complete program that may be compiled and run by an<br />

appropriate <strong>Ada</strong> compil<strong>in</strong>g system.<br />

The statement Put("Hello World"); is responsible for outputt<strong>in</strong>g the greet<strong>in</strong>g to the term<strong>in</strong>al. Used <strong>in</strong><br />

conjunction with New_L<strong>in</strong>e, which outputs a new l<strong>in</strong>e character to the term<strong>in</strong>al, these procedures are def<strong>in</strong>ed<br />

and implemented by the library package <strong>Ada</strong>.Text_Io.<br />

Note:<br />

The end keyword is followed by the name of the procedure, <strong>in</strong> this example helloworld. The<br />

compiler checks for this to ensure that the procedure's extent agrees with the programmer’s view.<br />

One of the important concepts <strong>in</strong> <strong>Ada</strong> is the idea of encapsulat<strong>in</strong>g items together to form a package which may<br />

be re-used <strong>in</strong> other programs. The library package <strong>Ada</strong>.Text_Io is provided on <strong>Ada</strong> systems to allow the <strong>in</strong>put<br />

and output of textual <strong>in</strong>formation to and from the user’s program. This library package is <strong>in</strong>troduced to a<br />

procedure by means of the statements with <strong>Ada</strong>.Text_Io; use <strong>Ada</strong>.Text_Io; the details of which will<br />

be expla<strong>in</strong>ed later.<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!