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.

Inheritance 159<br />

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

A program to illustrate the use of the class Named_Account is shown below:<br />

with Class_Named_Account, Statement;<br />

use Class_Named_Account;<br />

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

Mike : Named_Account;<br />

Get : Money;<br />

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

Set ( Mike, "A.N.Other/Brighton/UK" );<br />

Deposit ( Mike, 10.00 );<br />

Statement( Mike );<br />

Withdraw ( Mike, 5.00, Get );<br />

Statement( Mike );<br />

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

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

Statement for : A.N.Other<br />

M<strong>in</strong>i statement: The amount on deposit is £10.00<br />

Statement for : A.N.Other<br />

M<strong>in</strong>i statement: The amount on deposit is £ 5.00<br />

10.8 Initialization and f<strong>in</strong>alization<br />

When an <strong>in</strong>stance of a class is created, there can be static <strong>in</strong>itialization of the <strong>in</strong>stance or class attributes <strong>in</strong> the<br />

object. For example, <strong>in</strong> the class Account <strong>in</strong> Section 6.3.4 the <strong>in</strong>itial amount <strong>in</strong> the account was set to £0.00<br />

when an <strong>in</strong>stance of the class was elaborated. However, this <strong>in</strong>itialization is limited to a simple assignment. In<br />

some cases a more complex <strong>in</strong>itialization is required.<br />

Consider the case of a bank account that records an audit trail of all transactions made on the account. The<br />

audit trail consists of a record written to disk for each transaction made on the account. The audit trail file<br />

descriptor is shared between all <strong>in</strong>stances of the class and is <strong>in</strong>itialized by the first elaboration of an <strong>in</strong>stance of the<br />

class. The file descriptor is closed when the last <strong>in</strong>stance of the class is f<strong>in</strong>alized.<br />

By <strong>in</strong>herit<strong>in</strong>g from the package <strong>Ada</strong>.F<strong>in</strong>alization user def<strong>in</strong>ed <strong>in</strong>itialization and f<strong>in</strong>alization can be<br />

def<strong>in</strong>ed for a class. This takes the form of two user def<strong>in</strong>ed procedures Initialize and F<strong>in</strong>alize which are<br />

called respectively when an <strong>in</strong>stance of the class is elaborated and when an <strong>in</strong>stance of the class is destroyed. For<br />

example, the po<strong>in</strong>t when <strong>in</strong>itialization and f<strong>in</strong>alization take place is annotated on the follow<strong>in</strong>g fragment of code.<br />

procedure Ex5 is<br />

Mike : Account_At; --Initialization on mike<br />

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

Deposit( Mike, 100.00 );<br />

declare<br />

Cor<strong>in</strong>na : Account_At; --Initialization on cor<strong>in</strong>na<br />

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

Deposit( Cor<strong>in</strong>na, 100.00 );<br />

--F<strong>in</strong>alization on cor<strong>in</strong>na<br />

end;<br />

end Ex5;<br />

--F<strong>in</strong>alization on mike<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!