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.

236 Polymorphism<br />

The function about uses a l<strong>in</strong>ear search to f<strong>in</strong>d the selected room number. If the room number does not exist,<br />

the returned str<strong>in</strong>g conta<strong>in</strong>s the text "Sorry room not known".<br />

function About(The:<strong>in</strong> Build<strong>in</strong>g; No:<strong>in</strong> Positive) return Str<strong>in</strong>g is<br />

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

for I <strong>in</strong> 1 .. The.Last loop<br />

if Where(The.Description(I).all) = No then<br />

return Describe(The.Description(I).all);<br />

end if;<br />

end loop;<br />

return "Sorry room not known";<br />

end About;<br />

end Class_Build<strong>in</strong>g;<br />

Note:<br />

Chapter 17 explores more sophisticated conta<strong>in</strong>er implementations.<br />

The append<strong>in</strong>g of .all to an access value causes the object described by the access value to be<br />

delivered.<br />

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

The classes Room, Office and Build<strong>in</strong>g can be used to build a program to allow visitors to a build<strong>in</strong>g to f<strong>in</strong>d<br />

out details about <strong>in</strong>dividual rooms. The program is split <strong>in</strong>to two procedures. The first procedure declares and sets<br />

up details about <strong>in</strong>dividual rooms <strong>in</strong> the build<strong>in</strong>g.<br />

with <strong>Ada</strong>.Text_Io,<strong>Ada</strong>.Integer_Text_Io,Class_Room,Class_Room.Build,<br />

Class_Office, Class_Office.Build, Class_Build<strong>in</strong>g;<br />

use <strong>Ada</strong>.Text_Io,<strong>Ada</strong>.Integer_Text_Io,Class_Room,Class_Room.Build,<br />

Class_Office, Class_Office.Build, Class_Build<strong>in</strong>g;<br />

procedure Set_Up( Watts:<strong>in</strong> out Build<strong>in</strong>g ) is<br />

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

Add( Watts, Build_Office( 414, "4th Floor west w<strong>in</strong>g", 2 ) );<br />

Add( Watts, Build_Room ( 422, "4th Floor east w<strong>in</strong>g" ) );<br />

end Set_Up;<br />

The second procedure <strong>in</strong>terrogates the object watts to f<strong>in</strong>d details about <strong>in</strong>dividual rooms.<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io, Class_Build<strong>in</strong>g, Set_Up;<br />

use <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io, Class_Build<strong>in</strong>g;<br />

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

Watts : Build<strong>in</strong>g;<br />

--Watts Build<strong>in</strong>g<br />

Room_No : Positive;<br />

--Queried room<br />

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

Set_Up( Watts );<br />

--Populate build<strong>in</strong>g<br />

loop<br />

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

Put( "Inquiry about room: " ); --Ask<br />

exit when End_Of_File;<br />

Get( Room_No ); Skip_L<strong>in</strong>e; --User response<br />

Put( About( Watts, Room_No ) );<br />

New_L<strong>in</strong>e;<br />

--Display answer<br />

exception<br />

when Data_Error =><br />

Put("Please retype the number"); --Ask aga<strong>in</strong><br />

New_L<strong>in</strong>e; Skip_L<strong>in</strong>e;<br />

end;<br />

end loop;<br />

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

Note:<br />

The program does not release the storage for the descriptions of the <strong>in</strong>dividual rooms and offices.<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!