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

An efficient implementation is to access the private <strong>in</strong>stance attributes of the class Stack directly. This can be<br />

done by creat<strong>in</strong>g a child package of the generic package Class_Stack. However, as the parent class is generic, its<br />

child package must also be generic. The specification of this generic child package is as follows:<br />

generic<br />

package Class_Stack.Additions is<br />

function Top( The:<strong>in</strong> Stack ) return T;<br />

function Items( The:<strong>in</strong> Stack ) return Natural;<br />

private<br />

end Class_Stack.Additions;<br />

Note: As the child can see the components of the parent, it can also see any generic types.<br />

The implementation of the class is then:<br />

package body Class_Stack.Additions is<br />

function Top( The:<strong>in</strong> Stack ) return T is<br />

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

return The.Elements( The.Tos );<br />

end Top;<br />

function Items( The:<strong>in</strong> Stack ) return Natural is<br />

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

return Natural(The.Tos);<br />

end Items;<br />

end Class_Stack.Additions;<br />

A generic child of a package is considered to be declared with<strong>in</strong> the generic parent. Thus, to <strong>in</strong>stantiate an<br />

<strong>in</strong>stance of the parent and child the follow<strong>in</strong>g code is used:<br />

with Class_Stack;<br />

pragma Elaborate_All( Class_Stack );<br />

package Class_Stack_Pos is new Class_Stack(Positive,10);<br />

with Class_Stack_Pos, Class_Stack.Additions;<br />

pragma Elaborate_All( Class_Stack_Pos, Class_Stack.Additions );<br />

package Class_Stack_Pos_Additions is<br />

new Class_Stack_Pos.Additions;<br />

Note:<br />

The name of the <strong>in</strong>stantiated child package is an <strong>Ada</strong> identifier.<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!