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.

196 Child libraries<br />

The procedure pop returns the top item on the stack.<br />

procedure Pop( The:<strong>in</strong> out Stack; Item: out T ) is<br />

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

if The.Tos > 0 then<br />

Item := The.Elements( The.Tos ); --Top element<br />

The.Tos := The.Tos - 1; --Move down<br />

else<br />

raise Stack_Error;<br />

end if;<br />

end Pop;<br />

--Failed<br />

The procedure reset resets the stack to empty.<br />

procedure Reset( The:<strong>in</strong> out Stack ) is<br />

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

The.Tos := 0; --Set TOS to 0 (Non exist<strong>in</strong>g element)<br />

end Reset;<br />

end Class_Stack;<br />

A generic package cannot be used directly. First an <strong>in</strong>stantiation of the package must be made for a specific<br />

type. For example, to <strong>in</strong>stantiate an <strong>in</strong>stance of the above package Class_Stack to provide an Integer stack,<br />

the follow<strong>in</strong>g declaration is made:<br />

with Class_Stack;<br />

pragma Elaborate_All( Class_Stack );<br />

package Class_Stack_Int is new Class_Stack(Integer);<br />

Note:<br />

As the size of the stack is not specified the default value of 3 is used.<br />

The pragma to cause an eloboration of the generic package. This causes the compiler to generate a<br />

specific <strong>in</strong>stance of the package.<br />

The newly created package Class_Stack_Int can then be used <strong>in</strong> a program.<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!