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.

Packages as classes 75<br />

6.3.1 An object for a bank account<br />

Before look<strong>in</strong>g <strong>in</strong> detail at the implementation of an object that represents a bank account, it is appropriate to<br />

consider the messages that might be sent to such an object. For a very simple type of bank account these messages<br />

would be:<br />

• Deposit money <strong>in</strong>to the account.<br />

• Withdraw money from the account.<br />

• Deliver the account balance.<br />

The follow<strong>in</strong>g program demonstrates the send<strong>in</strong>g of these messages to an <strong>in</strong>stance of an Account.<br />

with <strong>Ada</strong>.Text_Io, Class_Account, Statement;<br />

use <strong>Ada</strong>.Text_Io, Class_Account;<br />

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

My_Account: Account;<br />

Obta<strong>in</strong> : Money;<br />

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

Statement( My_Account );<br />

Put("Deposit £100.00 <strong>in</strong>to account"); New_L<strong>in</strong>e; --Deposit<br />

Deposit( My_Account, 100.00 );<br />

Statement( My_Account );<br />

Put("Withdraw £80.00 from account"); New_L<strong>in</strong>e; --Withdraw<br />

Withdraw( My_Account, 80.00, Obta<strong>in</strong> );<br />

Statement( My_Account );<br />

Put("Deposit £200.00 <strong>in</strong>to account"); New_L<strong>in</strong>e; --Deposit<br />

Deposit( My_Account, 200.00 );<br />

Statement( My_Account );<br />

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

Note: The package Class_Account conta<strong>in</strong>s:<br />

• The def<strong>in</strong>ition of the type Account plus the def<strong>in</strong>ition of the operations allowed on an <strong>in</strong>stance of an<br />

Account;<br />

• The subtype Money used to def<strong>in</strong>e some of the parameters to messages sent to an <strong>in</strong>stance of<br />

Account.<br />

• The procedure Statement is used to simplify the pr<strong>in</strong>t<strong>in</strong>g of a m<strong>in</strong>i statement of the balance held <strong>in</strong> the<br />

account.<br />

The messages sent to an <strong>in</strong>stance of an Account are: Deposit, Withdraw, Balance. For example, to<br />

deposit £100 <strong>in</strong>to My_Account the follow<strong>in</strong>g procedural notation is used:<br />

Deposit( My_Account, 100.00 );<br />

This should be read as: send the message deposit to the object My_Account with an actual parameter of<br />

100.00.<br />

To withdraw money from the account a programmer would send the message Withdraw to the object<br />

My_Account with two parameters, the amount to withdraw and a variable that is to be filled with the amount<br />

actually withdrawn. The implementation of the method will check that the person has sufficient funds <strong>in</strong> their<br />

account to allow the transaction to take place. This is written as:<br />

Withdraw( My_Account, 80.00, obta<strong>in</strong> );<br />

Note:<br />

In reality the method is a normal <strong>Ada</strong> procedure that is passed as parameters, the object on which the<br />

action is to take place, plus any additional <strong>in</strong>formation as successive parameters.<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!