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.

120 Arrays<br />

When run, this would deliver the follow<strong>in</strong>g results:<br />

mada m'i madam<br />

Note:<br />

Even though dynamic arrays can be created, they can only be used to store an object that is type<br />

compatible. In particular, the number and type of the elements <strong>in</strong> the receiv<strong>in</strong>g object must be the same<br />

as <strong>in</strong> the delivered object.<br />

8.10 A name and address class<br />

A class for manag<strong>in</strong>g a person’s name and address has the follow<strong>in</strong>g responsibilities.<br />

Method<br />

Set<br />

Deliver_L<strong>in</strong>e<br />

L<strong>in</strong>es<br />

Responsibility<br />

Set the name and address of a person. The name and address<br />

is specified with a / character separat<strong>in</strong>g each l<strong>in</strong>e.<br />

Deliver the n'th l<strong>in</strong>e of the address as a str<strong>in</strong>g.<br />

Deliver the number of l<strong>in</strong>es <strong>in</strong> the address.<br />

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

package Class_Name_Address is<br />

type Name_Address is tagged private;<br />

procedure Set( The:out Name_Address; Str:<strong>in</strong> Str<strong>in</strong>g );<br />

function Deliver_L<strong>in</strong>e( The:<strong>in</strong> Name_Address;<br />

L<strong>in</strong>e:<strong>in</strong> Positive ) return Str<strong>in</strong>g;<br />

function L<strong>in</strong>es( The:<strong>in</strong> Name_Address ) return Positive;<br />

private<br />

Max_Chs : constant := 200;<br />

subtype L<strong>in</strong>e_Index is Natural range 0 .. Max_Chs;<br />

subtype L<strong>in</strong>e_Range is L<strong>in</strong>e_Index range 1 .. Max_Chs;<br />

type Name_Address is tagged record<br />

Text : Str<strong>in</strong>g( L<strong>in</strong>e_Range ); --Details<br />

Length : L<strong>in</strong>e_Index := 0; --Length of address<br />

end record;<br />

end Class_Name_Address;<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!