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.

6 Introduction to programm<strong>in</strong>g<br />

An animation of the above <strong>Ada</strong> <strong>95</strong> program is shown below. In the animation the contents of the memory<br />

locations are shown after each <strong>in</strong>dividual <strong>Ada</strong> <strong>95</strong> statement is executed. When a memory location is declared <strong>in</strong><br />

<strong>Ada</strong> <strong>95</strong> <strong>in</strong>side a function its <strong>in</strong>itial contents are undef<strong>in</strong>ed.<br />

<strong>Ada</strong> <strong>95</strong> statements price kilos Cost<br />

Price_per_kilo : Float;<br />

U U U<br />

Kilos_of_apples : Float;<br />

Cost : Float;<br />

Price_per_kilo := 1.20;<br />

Kilos_of_apples := 5.2;<br />

Cost := Price_per_kilo *<br />

Kilos_of_apples;<br />

Put( Cost );<br />

Note:<br />

1.20 U U<br />

1.20 5.2 U<br />

1.20 5.2 6.24<br />

1.20 5.2 6.24<br />

U <strong>in</strong>dicates that the contents of the memory location are undef<strong>in</strong>ed.<br />

Due to lack of room <strong>in</strong> the title column the variable Price_per_kilo is represented by price<br />

and Kilos_of_apples by kilos.<br />

1.6.1 Runn<strong>in</strong>g the program<br />

The above l<strong>in</strong>es of code, though not a complete <strong>Ada</strong> <strong>95</strong> program, form the core code for such a program. When<br />

this code is augmented with additional peripheral code, compiled and then run, the output produced will be of the<br />

form:<br />

6.24<br />

A person who knows what the program does, will <strong>in</strong>stantly know that this represents the price of 5.2 kilograms<br />

of apples at £1.20 a kilogram. However, this will not be obvious to a casual user of the program.<br />

1.7 The declare block<br />

In <strong>Ada</strong> a declaration is separated from an executable statement. One way of express<strong>in</strong>g this split is the declare<br />

block that is specified as follows:<br />

declare<br />

Cost : Float;<br />

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

Cost := 5.2 * 1.20;<br />

Put( Cost );<br />

end;<br />

Note:<br />

By us<strong>in</strong>g this construct our <strong>Ada</strong> <strong>95</strong> programm<strong>in</strong>g statements are almost a complete program.<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!