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.

Arrays 115<br />

8.5.2 Attributes of multidimensional arrays<br />

Like s<strong>in</strong>gle dimensional arrays, various attributes can also be extracted from multidimensional arrays. For<br />

multidimensional arrays it is, however, necessary to specify which dimension is to be <strong>in</strong>terrogated. This is<br />

achieved by append<strong>in</strong>g the appropriate dimension to the attribute. For example, to f<strong>in</strong>d the number of elements <strong>in</strong><br />

the second dimension of the object Sqrs use The.Sqrs'Length(2). Section B.2, Appendix B lists attributes<br />

that can be extracted from an object or type.<br />

8.6 Initializ<strong>in</strong>g an array<br />

A pixel on a true colour computer screen is represented by the three primary colours: red, blue, and green. Each<br />

colour has an <strong>in</strong>tensity rang<strong>in</strong>g from 0 (dark) to 255 (bright). This is the RGB additive colour model used by<br />

computer term<strong>in</strong>als and TVs, which is different from the CYMB subtractive colour model used <strong>in</strong> pr<strong>in</strong>t<strong>in</strong>g. In<br />

<strong>Ada</strong>, a pixel could be represented by an array of three elements represent<strong>in</strong>g the <strong>in</strong>tensities of the primary colours.<br />

To represent the colour white, the <strong>in</strong>tensity of each of the primary colours would be set to 255. A pixel can be<br />

represented by the type Pixel_Array as follows:<br />

type Colour is ( Red, Green, Blue );<br />

type Intensity is range 0 .. 255;<br />

type Pixel_Array is array( Colour ) of Intensity;<br />

A s<strong>in</strong>gle po<strong>in</strong>t on the screen could be represented by the object dot as follows:<br />

Dot<br />

: Pixel_Array;<br />

which could be <strong>in</strong>itialized to black or white with the follow<strong>in</strong>g assignments:<br />

Dot := ( 0, 0, 0 );<br />

Dot := ( 255, 255, 255 );<br />

--Black<br />

--White<br />

The values can be named by us<strong>in</strong>g the subscript to the array as follows:<br />

Dot := ( Red=> 255, Green=>255, Blue=>255);<br />

--White<br />

An others clause can be used to let the rema<strong>in</strong><strong>in</strong>g elements of the array take a particular value as <strong>in</strong>:<br />

Dot := Pixel_Array'( Red=>255, others=>0 );<br />

--Red<br />

Note:<br />

When the others clause is used, and at least one other element is given a value by a different means,<br />

then the type of the constant must be specified. This is achieved by prefix<strong>in</strong>g the constant with its type<br />

name followed by a '.<br />

Us<strong>in</strong>g a similar notation to that used <strong>in</strong> the case statement <strong>in</strong>troduced <strong>in</strong> Section 3.8.1, a range of values may<br />

also be specified:<br />

Dot := ( Red=>255, Green=>255, Blue=>0 );<br />

Dot := ( Red | Blue => 255, Green=>0 );<br />

Dot := ( Red .. Blue => 127 );<br />

--Yellow<br />

--Purple<br />

--Grey<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!