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

The two classes Account and Interest_Account can be used <strong>in</strong> a program to perform some simple bank<br />

transactions. A program to illustrate the use of the new class Interest_Account and the orig<strong>in</strong>al class<br />

Account is shown below:<br />

with <strong>Ada</strong>.Text_io,<br />

Class_Interest_Account, Class_Account, Statement;<br />

use <strong>Ada</strong>.Text_io,<br />

Class_Interest_Account, Class_Account;<br />

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

Mike :Account; --Normal Account<br />

Cor<strong>in</strong>na :Interest_Account; --Interest ber<strong>in</strong>g account<br />

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

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

Set_Rate( 0.00026116 );<br />

Statement( Mike );<br />

--For all <strong>in</strong>stances of<br />

--<strong>in</strong>terest ber<strong>in</strong>g accounts<br />

Put("Deposit £50.00 <strong>in</strong>to Mike's account"); New_L<strong>in</strong>e;<br />

Deposit( Mike, 50.00 );<br />

Statement( Mike );<br />

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

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

Statement( Mike );<br />

Put("Deposit £500.00 <strong>in</strong>to Cor<strong>in</strong>na's account"); New_L<strong>in</strong>e;<br />

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

Statement( Account(Cor<strong>in</strong>na) );<br />

Put("Add <strong>in</strong>terest to Cor<strong>in</strong>na's account"); New_L<strong>in</strong>e;<br />

Calc_Interest( Cor<strong>in</strong>na );<br />

Add_Interest( Cor<strong>in</strong>na );<br />

Statement( Account(Cor<strong>in</strong>na) );<br />

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

In this program, the procedure Statement takes as its parameter an <strong>in</strong>stance of an Account. However, as<br />

the account for Cor<strong>in</strong>na is an <strong>in</strong>stance of a Interest_Account it must first be converted to an <strong>in</strong>stance of an<br />

Account before it can be passed as an actual parameter to Statement. This is accomplished with a view<br />

conversion Account(Cor<strong>in</strong>na). The actual parameter passed to Statement is now viewed as if it were an<br />

Account.<br />

which, when compiled and run, would produce the follow<strong>in</strong>g output:<br />

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

Deposit £50.00 <strong>in</strong>to Mike's account<br />

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

Withdraw £80.00 from Mike's account<br />

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

Deposit £500.00 <strong>in</strong>to Cor<strong>in</strong>na's account<br />

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

Add <strong>in</strong>terest to Cor<strong>in</strong>na's account<br />

M<strong>in</strong>i statement: The amount on deposit is £500.13<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!