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.

44 <strong>Ada</strong> <strong>in</strong>troduction: Part 2<br />

4.9 Type safety <strong>in</strong> a program<br />

By us<strong>in</strong>g the type mechanism, errors <strong>in</strong> a program can be detected at compile-time. For example, a program which<br />

processes distances <strong>in</strong> miles and kilometres can be made safer by def<strong>in</strong><strong>in</strong>g separate types for miles and kilometres<br />

as follows:<br />

type Miles is digits 8 range 0.0 .. 25_000.0;<br />

type Kilometres is digits 8 range 0.0 .. 50_000.0;<br />

Note:<br />

The range of values is adequate to accommodate any distance between two po<strong>in</strong>ts on the earth.<br />

A program which processes distances between cities could be def<strong>in</strong>ed as follows:<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Float_Text_Io;<br />

use <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Float_Text_Io;<br />

procedure Ma<strong>in</strong> is<br />

type Miles is digits 8 range 0.0 .. 25_000.0;<br />

type Kilometres is digits 8 range 0.0 .. 50_000.0;<br />

London_Paris : Miles;<br />

Paris_Geneva : Kilometres;<br />

London_Paris_Geneva: Kilometres;<br />

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

London_Paris := 210.0; --Miles<br />

Paris_Geneva := 420.0; --Kilometres<br />

London_Paris_Geneva :=<br />

Kilometres( London_Paris * 1.609_344 ) + Paris_Geneva;<br />

Put("Distance london - paris - geneva (Kms) is " );<br />

Put( Float( London_Paris_Geneva ), Aft=>2, Exp=>0 );<br />

New_L<strong>in</strong>e;<br />

end Ma<strong>in</strong>;<br />

Note: There is an explicit conversion of a distance <strong>in</strong> miles to kilometres us<strong>in</strong>g the type conversion<br />

kilometres( london_paris * 1.609_344 ).<br />

The contents of London_Paris_Geneva has been converted to a Float so that it can be pr<strong>in</strong>ted<br />

us<strong>in</strong>g the package <strong>Ada</strong>.Float_Text_Io. Chapter 17 describes how user def<strong>in</strong>ed types may be<br />

output.<br />

The parameters to put when outputt<strong>in</strong>g a float<strong>in</strong>g po<strong>in</strong>t number control the number of decimal places<br />

output and the format of the number. Section C.5, Appendix C lists the parameters used <strong>in</strong> outputt<strong>in</strong>g<br />

numbers .<br />

If by accident a programmer wrote:<br />

London_Paris_Geneva := London_Paris + Paris_Geneva;<br />

then the <strong>Ada</strong> compiler would detect a type mismatch at compile-time. London to Paris is <strong>in</strong> miles and Paris to<br />

Geneva is <strong>in</strong> kilometres.<br />

4.10 Subtypes<br />

The type mechanism can on occasion, be restrict<strong>in</strong>g as a programmer wants the range check<strong>in</strong>g provided by the<br />

type mechanism but does not want to have to keep explicitly perform<strong>in</strong>g type conversions. A subtype of a type<br />

provides the range check<strong>in</strong>g associated with a type, but <strong>in</strong>stances of a type and its subtypes may be freely mixed<br />

<strong>in</strong> expressions.<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!