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

When perform<strong>in</strong>g arithmetic with an <strong>in</strong>stance of a type's base type, no range checks take place. This allows an<br />

implementor to implement the base type <strong>in</strong> the most efficient or effective way for a specific mach<strong>in</strong>e. However,<br />

the exception Constra<strong>in</strong>t_Error will be generated if the resultant arithmetic evaluation leads to a wrong<br />

result. For example, the exception Constra<strong>in</strong>t_Error is generated if an overflow is detected when<br />

perform<strong>in</strong>g calculations with the base type.<br />

4.11.3 Arithmetic with types and subtypes<br />

In a program deal<strong>in</strong>g with a student's exam marks, the follow<strong>in</strong>g program is written to average the marks for a<br />

student tak<strong>in</strong>g English, Maths and Comput<strong>in</strong>g:<br />

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

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

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

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

English : Exam_Mark; --English exam mark<br />

Maths : Exam_Mark; --Maths " "<br />

Comput<strong>in</strong>g : Exam_Mark; --Comput<strong>in</strong>g " "<br />

Average : Exam_Mark; --<br />

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

English := 72;<br />

Maths := 68;<br />

Comput<strong>in</strong>g := 76;<br />

Put("Average exam mark is ");<br />

Average := (English + Maths + Comput<strong>in</strong>g) / 3;<br />

Put( Exam_Mark'Image(Average) ); New_L<strong>in</strong>e;<br />

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

In execut<strong>in</strong>g the statement:<br />

Average := (English+Maths+Comput<strong>in</strong>g) / 3;<br />

the expression:<br />

(English+Maths+Comput<strong>in</strong>g) / 3<br />

will generate a result which is <strong>in</strong> the range 0 .. 100. However, the component of the statement<br />

English+Maths+Comput<strong>in</strong>g will generate a temporary result which is outside the range of Exam_Mark.<br />

In <strong>Ada</strong> the arithmetic operations are def<strong>in</strong>ed to process <strong>in</strong>stances of the root types. In evaluat<strong>in</strong>g<br />

English+Maths+Comput<strong>in</strong>g, English+Maths will deliver a temporary object of type Root_Enteger<br />

(Exam_Mark'Base) which is then added to Comput<strong>in</strong>g. The result of the addition is divided by 3 at which<br />

po<strong>in</strong>t a range check is performed on the temporary result before it is assigned to the object average.<br />

Of course, for this to work the Root_Integer type must be sufficiently large to hold the sum of<br />

English+Maths+Comput<strong>in</strong>g. Remember, this will be of type Root_Integer which has a range of -2 15 ..<br />

2 15 -1.<br />

4.11.4 Warn<strong>in</strong>g<br />

If the declaration for Exam_mark where replaced by:<br />

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

then the above program would fail with a Constra<strong>in</strong>t_Error if the base type of Exam_Mark were to be<br />

implemented <strong>in</strong> a s<strong>in</strong>gle byte.<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!