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.

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

Distance is a new type, <strong>in</strong>stances of which may not be mixed with <strong>in</strong>stances of other types. The follow<strong>in</strong>g<br />

table shows some examples of type declarations <strong>in</strong> <strong>Ada</strong>.<br />

Type declaration<br />

type T is range 0 .. 250_000;<br />

type T is digits 8;<br />

type T is digits 8 range 0.0 .. 10.0;<br />

An <strong>in</strong>stance of T will Declare<br />

An object which can hold whole numbers <strong>in</strong><br />

the range 0 .. 250_000.<br />

An object which can hold a float<strong>in</strong>g po<strong>in</strong>t<br />

number which has a precision of 8 digits.<br />

An object which can hold a float<strong>in</strong>g po<strong>in</strong>t<br />

number which has a precision of 8 digits and<br />

can store numbers <strong>in</strong> the range 0.0 .. 10.0.<br />

4.3.1 Type conversions<br />

To convert between compatible scalar types the type name of the required type is used to convert an object to the<br />

required type. For example, the follow<strong>in</strong>g program converts an object of type Apples <strong>in</strong>to an object of type<br />

French_Apples.<br />

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

type Apples is range 0 .. 100;<br />

type French_Apples is range 0 .. 100;<br />

Number : Apples;<br />

Number_From_France : French_Apples;<br />

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

Number := 10;<br />

Number_From_France := French_Apples( Number );<br />

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

It is, however, up to the programmer to determ<strong>in</strong>e whether the conversion is mean<strong>in</strong>gful. Conversion,<br />

however, can only take place between types that are compatible.<br />

4.3.2 Universal <strong>in</strong>teger<br />

To avoid tedious type conversion when deal<strong>in</strong>g with constants, <strong>Ada</strong> has the concept of a universal <strong>in</strong>teger. The<br />

compiler will automatically convert a universal <strong>in</strong>teger to an appropriate type when used <strong>in</strong> an arithmetic<br />

expression. In <strong>Ada</strong> all <strong>in</strong>teger numeric constants are regarded as be<strong>in</strong>g of type universal <strong>in</strong>teger. Likewise all<br />

float<strong>in</strong>g po<strong>in</strong>t constants are regarded as a universal float.<br />

4.3.3 Constant declarations<br />

To make a program more readable, all values other than 0 or 1 should normally be given a symbolic name. This<br />

helps to improve the readability of a program and allows the programmer to change the value by means of a s<strong>in</strong>gle<br />

textual change. For example, the capacity of a car park could be described as:<br />

Max_Park<strong>in</strong>g_Spaces: constant := 100;<br />

This describes Max_Park<strong>in</strong>g_Spaces as a universal <strong>in</strong>teger. However, if the declaration had been:<br />

Max_Park<strong>in</strong>g_Spaces: constant Park<strong>in</strong>g_Spaces := 100;<br />

then Max_Park<strong>in</strong>g_Spaces would be a constant of type Park<strong>in</strong>g_spaces.<br />

Note:<br />

The latter declaration will restrict the places where Max_Park<strong>in</strong>g_Spaces can be used to only<br />

those places where a value of type Park<strong>in</strong>g_Spaces can occur.<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!