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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Tasks 303<br />

the implementation of which is:<br />

package body Pack_Factorial is<br />

task body Task_Factorial is --Implementation<br />

Factorial : Positive;<br />

Answer : Positive := 1;<br />

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

Put("Pack_factorial"); New_L<strong>in</strong>e;<br />

accept Start( F:<strong>in</strong> Positive) do --Factorial<br />

Factorial := F;<br />

end Start;<br />

for I <strong>in</strong> 2 .. Factorial loop --Calculate<br />

Answer := Answer * I;<br />

end loop;<br />

accept F<strong>in</strong>ish( Result:out Positive ) do --Return answer<br />

Result := Answer;<br />

end F<strong>in</strong>ish;<br />

end Task_Factorial;<br />

end Pack_Factorial;<br />

The code to <strong>in</strong>teract with this task <strong>in</strong> a package would be as follows:<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io, Pack_Factorial;<br />

use <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io, Pack_Factorial;<br />

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

Factorial: Positive;<br />

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

Task_Factorial.Start(5);<br />

--Start factorial calculation<br />

--Task runn<strong>in</strong>g<br />

Put("Factorial 5 is ");<br />

Task_Factorial.F<strong>in</strong>ish( Factorial ); --Obta<strong>in</strong> result<br />

Put( Factorial ); New_L<strong>in</strong>e;<br />

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

Note:<br />

If this form is used, then the task will come <strong>in</strong>to immediate existence as soon as the program is<br />

executed.<br />

Only one <strong>in</strong>stance of the factorial task can be created.<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!