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.

Exceptions 207<br />

The implementation of this <strong>in</strong>herited class is:<br />

package body Class_Better_Stack is<br />

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

D : Natural;<br />

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

Class_Stack_T.Push( Class_Stack_T.Stack(The), Item );<br />

D := Class_Stack_T_Additions.Items(Class_Stack_T.Stack(The) );<br />

if D > The.Depth then<br />

The.Depth := The.Depth + 1;<br />

end if;<br />

end Push;<br />

function Max_Depth( The:<strong>in</strong> Better_Stack ) return Natural is<br />

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

return The.Depth;<br />

end Max_Depth;<br />

end Class_Better_Stack;<br />

14.8.1 Putt<strong>in</strong>g it all together<br />

An <strong>in</strong>stantiation of the class Better_stack for Positive numbers is created with the declaration:<br />

with Class_Better_Stack;<br />

pragma Elaborate_All( Class_Better_Stack );<br />

package Class_Better_Stack_Pos is<br />

new Class_Better_Stack(Positive,10);<br />

This is then used <strong>in</strong> a small test program of the new class as follows:<br />

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

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

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

Numbers : Better_Stack;<br />

Res : Positive;<br />

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

Put("Max depth "); Put( Max_Depth( Numbers ) ); New_L<strong>in</strong>e;<br />

Push( Numbers, 10 );<br />

Push( Numbers, 20 );<br />

Put("Max depth "); Put( Max_Depth( Numbers ) ); New_L<strong>in</strong>e;<br />

Push( Numbers, 20 );<br />

Put("Max depth "); Put( Max_Depth( Numbers ) ); New_L<strong>in</strong>e;<br />

Pop( Numbers, Res );<br />

Put("Max depth "); Put( Max_Depth( Numbers ) ); New_L<strong>in</strong>e;<br />

null;<br />

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

which when run produces the follow<strong>in</strong>g results:<br />

Max depth 0<br />

Max depth 2<br />

Max depth 3<br />

Max depth 3<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!