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.

Inheritance 155<br />

The specialization of the procedure Withdraw calls the method withdraw <strong>in</strong> class Account to process the<br />

withdrawal. To avoid <strong>in</strong>f<strong>in</strong>ite recursion, the parameter The is converted to type Account before it is passed as a<br />

parameter to Withdraw. This is termed a view conversion. Overload resolution is then used to determ<strong>in</strong>e which<br />

version of Withdraw to call.<br />

procedure Withdraw ( The:<strong>in</strong> out Account_Ltd;<br />

Amount:<strong>in</strong> Pmoney; Get:out Pmoney ) is<br />

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

if The.Withdrawals > 0 then --Not limit<br />

The.Withdrawals := The.Withdrawals - 1;<br />

Withdraw( Account(The), Amount, Get ); --In Account<br />

else<br />

Get := 0.00;<br />

end if;<br />

end Withdraw;<br />

--Sorry<br />

The function reset resets the number of withdrawals that may be made <strong>in</strong> the current week.<br />

procedure Reset( The:<strong>in</strong> out Account_Ltd ) is<br />

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

The.Withdrawals := Withdrawals_In_A_Week;<br />

end Reset;<br />

end Class_Account_Ltd;<br />

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

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

with Class_Account, Class_Account_ltd, Statement;<br />

use Class_Account, Class_Account_ltd;<br />

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

Mike : Account_Ltd;<br />

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

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

Deposit( Mike, 300.00 ); --In credit<br />

Statement( Account(Mike) );<br />

Withdraw( Mike, 100.00, Obta<strong>in</strong> ); --Withdraw some money<br />

Withdraw( Mike, 10.00, Obta<strong>in</strong> ); --Withdraw some money<br />

Withdraw( Mike, 10.00, Obta<strong>in</strong> ); --Withdraw some money<br />

Withdraw( Mike, 20.00, Obta<strong>in</strong> ); --Withdraw some money<br />

Statement( Account(Mike) );<br />

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

Note:<br />

The with and use of the package Class_Account so that the subtype Money is directly visible.<br />

The procedure Statement seen earlier is used to pr<strong>in</strong>t a m<strong>in</strong>i statement.<br />

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

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

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

Note:<br />

The f<strong>in</strong>al withdrawal of £20 is not processed as three withdrawals have already been made this week.<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!