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.

154 Inheritance<br />

The abstract class can then be used to derived specific types of bank account. In the follow<strong>in</strong>g case it has been<br />

used to derive a simple bank account.<br />

with Class_Abstract_Account;<br />

use Class_Abstract_Account;<br />

package Class_Account is<br />

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

subtype Money is Class_Abstract_Account.Money;<br />

subtype Pmoney is Class_Abstract_Account.Pmoney;<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 Account is new Abstract_Account with record<br />

Balance_Of : Money := 0.00;<br />

end record;<br />

end Class_Account;<br />

--Amount <strong>in</strong> account<br />

Note:<br />

Subtype has been used to make the subtypes Money and PMoney visible to clients of the class. If this<br />

had not been done, users of the class would <strong>in</strong> most cases have to with and use the package<br />

Class_abstract_account.<br />

The implementation of which would be the same as the class Account shown <strong>in</strong> Section 6.3.5.<br />

Once a class has been derived from an exist<strong>in</strong>g class it too may be used as a base class <strong>in</strong> deriv<strong>in</strong>g a new class.<br />

For example, an account that allows a customer to only make three withdrawals <strong>in</strong> a week can be derived from<br />

Class_Account as follows:<br />

with Class_Account;<br />

use Class_Account;<br />

package Class_Account_Ltd is<br />

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

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

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

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

private<br />

Withdrawals_In_A_Week : Natural := 3;<br />

type Account_Ltd is new Account with record<br />

Withdrawals : Natural := Withdrawals_In_A_Week;<br />

end record;<br />

end Class_Account_Ltd;<br />

Note:<br />

The derived class overloads the method Withdraw with a new specialized mean<strong>in</strong>g. The method<br />

Reset is used to set the number of withdrawals that may be made <strong>in</strong> the current week to three.<br />

The implementation for the class is as follows:<br />

package body Class_Account_Ltd is<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!