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.

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

K : constant := 1024;<br />

type Word16 is mod 64 * K;<br />

Pattern : Word16;<br />

the follow<strong>in</strong>g code:<br />

• sets the top nibble of the two byte word Pattern to zero.<br />

Pattern := Pattern and 16#FFF#;<br />

• sets bit 9 <strong>in</strong> the two byte word Pattern to 1.<br />

Pattern := Pattern or 2#0000001000000000#;<br />

Note:<br />

The constant to base 16 is 16#FFF# and to base 2 is 2#0000001000000000#. Section B.4,<br />

Appendix B describes how to declare constants to different bases.<br />

• Flips bit 9 <strong>in</strong> the two byte word Pattern. If bit 9 were a 1 it would now be a 0 and if it were a 0 it<br />

would now be a 1.<br />

Pattern := Pattern xor 2#0000001000000000#;<br />

• Inverts the bits <strong>in</strong> the two byte word Pattern.<br />

4.20 Self-assessment<br />

Pattern := not Pattern;<br />

• Why is it not always appropriate to hold a value us<strong>in</strong>g an <strong>in</strong>stance of a Float?<br />

• How <strong>in</strong> a program can you f<strong>in</strong>d out the smallest value that can be stored <strong>in</strong> an <strong>in</strong>stance of<br />

Long_Integer?<br />

• What are the benefits of user def<strong>in</strong>ed types and subtypes <strong>in</strong> a program?<br />

• Why does the follow<strong>in</strong>g program fail?<br />

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

type Miles is new Integer range 0 .. 100;<br />

type Kilometres is new Integer range 0 .. 100;<br />

London_Brighton : Miles := 50;<br />

To_Brighton : constant Kilometres := 2;<br />

Distance_To_London : Miles;<br />

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

Distance_To_London := London_Brighton + To_Brighton;<br />

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

• What is the difference between a type and a subtype?<br />

• Why are the concepts of universal <strong>in</strong>teger and universal float important?<br />

• How can you convert a value of one type to that of another?<br />

• Us<strong>in</strong>g the type declarations:<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!