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.

220 Polymorphism<br />

this program will produce the follow<strong>in</strong>g results:<br />

push number = 1<br />

push number = 2<br />

push number = 3<br />

push number = 4<br />

Pop number = 4<br />

Pop number = 3<br />

Pop number = 2<br />

Pop number = 1<br />

Pop: Exception Stack_error<br />

This is essentially the same driver code as used on the previous implementation of a stack. This time,<br />

however, the stack is us<strong>in</strong>g dynamically allocated storage.<br />

15.5 Hid<strong>in</strong>g the structure of an object (opaque type)<br />

So far, even though a client of a class cannot access the <strong>in</strong>stance attributes of an <strong>in</strong>stance of the class the client can<br />

still see the type and names of the <strong>in</strong>stance attributes used <strong>in</strong> the object <strong>in</strong> the specification of the class. The<br />

<strong>in</strong>stance attributes <strong>in</strong> an object can be hidden by mov<strong>in</strong>g the data declarations to the implementation part of the<br />

class. The specification part of the class will now conta<strong>in</strong> an access value to a data structure whose contents are<br />

def<strong>in</strong>ed <strong>in</strong> the implementation part of the class.<br />

If the specification part of the class no longer def<strong>in</strong>es how much storage is to be used then the storage for an<br />

object must be allocated dynamically. The reason for this is that the compiler will not know how much storage to<br />

allocate for an object. Remember that the implementation part may have been separately compiled. The<br />

specification part of the class def<strong>in</strong>es an access value which po<strong>in</strong>ts to the storage for the object’s <strong>in</strong>stance<br />

attributes. For example, the class for a bank account can now be def<strong>in</strong>ed as:<br />

with <strong>Ada</strong>.F<strong>in</strong>alization;<br />

use <strong>Ada</strong>.F<strong>in</strong>alization;<br />

package Class_Account is<br />

type Account is new Limited_Controlled with private;<br />

subtype Money is Float;<br />

subtype Pmoney is Float range 0.0 .. Float'Last;<br />

procedure Initialize( The:<strong>in</strong> out Account );<br />

procedure F<strong>in</strong>alize ( The:<strong>in</strong> out Account );<br />

procedure Deposit ( The:<strong>in</strong> out Account; Amount:<strong>in</strong> Pmoney );<br />

procedure Withdraw ( The:<strong>in</strong> out Account; Amount:<strong>in</strong> Pmoney;<br />

Get:out Pmoney );<br />

function Balance ( The:<strong>in</strong> Account ) return Money;<br />

private<br />

type Actual_Account;<br />

--Details In body<br />

type P_Actual_Account is access all Actual_Account;<br />

type Account is new Limited_Controlled with record<br />

Acc : P_Actual_Account; --Hidden <strong>in</strong> body<br />

end record;<br />

end Class_Account;<br />

Note:<br />

Apart from the user-def<strong>in</strong>ed <strong>in</strong>itialization and f<strong>in</strong>alization, the procedure and function specification is<br />

the same as seen <strong>in</strong> Section 6.3.4.<br />

The declaration for the type Actual_Account is tentative.<br />

The base type of the class is Limited_Controlled. Thus assignments of <strong>in</strong>stances of Account<br />

are prevented..<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!