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

which when run, will produce:<br />

M<strong>in</strong>i statement: Account £ 10001<br />

The amount on deposit is £200.00<br />

M<strong>in</strong>i statement: Account £ 10002<br />

The amount on deposit is £200.00<br />

6.10.2 Restrictions<br />

The follow<strong>in</strong>g restrictions apply, however:<br />

• Only discrete objects or access values may be used as the discrim<strong>in</strong>ant value. If an access value is used<br />

then the type must be limited.<br />

• To change the discrim<strong>in</strong>ant value the whole record structure must be changed.<br />

Thus the implementation of the procedure New_Number that allocates a new account number is:<br />

procedure New_Number( The: <strong>in</strong> out Account; N:<strong>in</strong> Natural ) is<br />

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

The := Account'( N, The.Balance_Of );<br />

end New_Number;<br />

Note:<br />

The whole record structure needs to be changed to change the discrim<strong>in</strong>ant. Chapter 7 discusses<br />

record <strong>in</strong>itialization <strong>in</strong> more detail.<br />

6.10.3 By assignment<br />

In this case the object is assigned an <strong>in</strong>itial value when it is declared. For example, the follow<strong>in</strong>g code sets<br />

My_Account with an account number and <strong>in</strong>itial balance:<br />

with Class_Account, Statement;<br />

use Class_Account;<br />

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

My_Account : Account := New_Account( 10001, 20.0 );<br />

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

Statement( My_Account );<br />

end Mai3;<br />

which when run, will produce:<br />

M<strong>in</strong>i statement: Account £ 10001<br />

The amount on deposit is £20.00<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!