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.

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

For example, the speed of various forms of transport can be def<strong>in</strong>ed us<strong>in</strong>g the type and subtype mechanism as<br />

follows:<br />

type Speed_Mph is range 0 .. 25_000;<br />

subtype Tra<strong>in</strong>_Speed is Speed_Mph range 0 .. 130;<br />

subtype Bus_Speed is Speed_Mph range 0 .. 75;<br />

subtype Cycl<strong>in</strong>g_Speed is Speed_Mph range 0 .. 30;<br />

subtype Person_Speed is Speed_Mph range 0 .. 15;<br />

A subtype is derived from an exist<strong>in</strong>g type and constra<strong>in</strong>s the values that can be assigned to an <strong>in</strong>stance of the<br />

subtype. The compiler will enforce this constra<strong>in</strong>t either by perform<strong>in</strong>g a compile-time check or by generat<strong>in</strong>g<br />

code to check the constra<strong>in</strong>t at run-time. Of course, the subtype <strong>in</strong>herits all the operations that can be performed<br />

on an <strong>in</strong>stance of the type.<br />

Instances of a type and its subtypes may be freely mixed <strong>in</strong> arithmetic, comparison and assignment operations.<br />

For example, us<strong>in</strong>g the above type and subtypes declarations for the speed of various forms of transport, the<br />

follow<strong>in</strong>g code can be written:<br />

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

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

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

-- Type and subtype declarations for speeds<br />

T0715 : Tra<strong>in</strong>_Speed; --07:15 Brighton - London<br />

B0720 : Bus_Speed; --07:20 Brighton - London<br />

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

T0715 := 55; --Average speed Brighton - London (Tra<strong>in</strong>)<br />

B0720 := 35; --Average speed Brighton - London (Bus)<br />

if T0715 > B0720 then<br />

Put("The tra<strong>in</strong> is faster then the bus");<br />

else<br />

Put("The bus is faster then the tra<strong>in</strong>");<br />

end if;<br />

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

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

Note:<br />

It is of course an error to mix <strong>in</strong>stances of subtypes which are derived from different types.<br />

4.10.1 Types vs. subtypes<br />

Criteria Types Subtype<br />

Instances may be mixed with only <strong>in</strong>stances of the<br />

same type<br />

only <strong>in</strong>stances of a type<br />

and subtypes derived<br />

from the type<br />

May have a constra<strong>in</strong>t Yes Yes<br />

4.11 More on types and subtypes<br />

In <strong>Ada</strong> only subtypes have names. The consequence of this is that the declaration:<br />

type Speed_Mph is range 0 .. 25_000;<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!