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 49<br />

4.12 Compile-time and run-time checks<br />

The follow<strong>in</strong>g program declares data types to represent: (a) the number of power po<strong>in</strong>ts <strong>in</strong> a room, (b) the<br />

capacity of a lecture room <strong>in</strong> seats, and (c) the capacity of a tutorial room, aga<strong>in</strong> <strong>in</strong> seats. In this program, various<br />

assignments are made, some of which will fail to compile, some of which will fail <strong>in</strong> execution.<br />

procedure Dec is<br />

type Power_Po<strong>in</strong>ts is range 0 .. 6;<br />

type Room_Size is range 0 .. 120;<br />

subtype Lecture_Room is Room_Size range 0 .. 75;<br />

subtype Tutorial_Room is Room_Size range 0 .. 20;<br />

Po<strong>in</strong>ts_In_504 : Power_Po<strong>in</strong>ts;<br />

People_In_504 : Lecture_Room;<br />

People_In_616 : Tutorial_Room;<br />

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

Po<strong>in</strong>ts_In_504 := 3;<br />

Po<strong>in</strong>ts_In_504 := 80;<br />

People_In_504 := 15;<br />

People_In_616 := People_In_504;<br />

People_In_504 := Po<strong>in</strong>ts_In_504;<br />

--Power outlets<br />

--Size lecture room<br />

--Size tutorial room<br />

--OK<br />

--Error / Warn<strong>in</strong>g<br />

--OK<br />

--OK<br />

-- Type Mismatch<br />

People_In_504 := Lecture_Room( Po<strong>in</strong>ts_In_504 ); --Force<br />

People_In_504 := 50;<br />

People_In_616 := People_In_504;<br />

end Dec;<br />

--OK<br />

--Constra<strong>in</strong>t error<br />

The compilation or execution of the follow<strong>in</strong>g l<strong>in</strong>es will fail for the follow<strong>in</strong>g reasons:<br />

L<strong>in</strong>e<br />

Po<strong>in</strong>ts_In_504:= 80;<br />

People_In_504 := Po<strong>in</strong>ts_In_504;<br />

People_In_616 := People_In_504;<br />

Reason for failure<br />

The range of values allowed for the object<br />

Po<strong>in</strong>ts_In_504 does not <strong>in</strong>clude 80.<br />

This error will usually be detected at<br />

compile-time.<br />

The objects on the LHS and RHS of the<br />

assignment statement are of different types<br />

and will thus produce a compile-time error.<br />

Will cause a constra<strong>in</strong>t error when<br />

executed, as the object People_In_504<br />

conta<strong>in</strong>s 50.<br />

In this example, the error could <strong>in</strong> theory be<br />

detected at compile-time.<br />

Note:<br />

Depend<strong>in</strong>g on the quality of the compiler, some errors which <strong>in</strong> theory could be detected at compiletime,<br />

will only be detected at run-time. Conversely possible run-time errors may be flagged through<br />

warn<strong>in</strong>gs at compile time.<br />

This shows the strength of <strong>Ada</strong>’s strong type check<strong>in</strong>g: problems <strong>in</strong> a program can be identified at an early<br />

stage of development. However, careful plann<strong>in</strong>g needs to be made when writ<strong>in</strong>g a program. Decisions about<br />

which dist<strong>in</strong>ct data types to use and which data types should be a subtype of others are particularly important.<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!