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.

Polymorphism 231<br />

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

The above classes can be comb<strong>in</strong>ed <strong>in</strong>to a program which pr<strong>in</strong>ts details about various rooms or offices <strong>in</strong> a<br />

build<strong>in</strong>g. The program is as follows:<br />

with <strong>Ada</strong>.Text_Io, Class_room, Class_Office;<br />

use <strong>Ada</strong>.Text_Io, Class_room, Class_Office;<br />

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

W422 : Room;<br />

W414 : Office;<br />

The procedure about can take either an <strong>in</strong>stance of a Room or an Office as a parameter. This is achieved<br />

by describ<strong>in</strong>g the parameter as Room'Class. The parameter of type Room'Class will match an <strong>in</strong>stance of<br />

Room plus an <strong>in</strong>stance of any type which is derived directly or <strong>in</strong>directly from a Room.<br />

In the procedure about the call to the function Describe( Place ) is not resolvable at compile-time, as<br />

the object place may be either a Room or an Office. At run-time when the type of Place is known to the<br />

system, this call can be resolved. Thus, either Describe <strong>in</strong> the class Room or Describe <strong>in</strong> the class Office<br />

will be called.<br />

procedure About( Place:<strong>in</strong> Room'Class ) is<br />

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

Put( "The place is" ); New_L<strong>in</strong>e;<br />

Put( " " & Describe( Place ) ) ; --Run time dispatch<br />

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

end About;<br />

Note:<br />

One way to implement the dynamic b<strong>in</strong>d<strong>in</strong>g is for the object to conta<strong>in</strong> <strong>in</strong>formation about which<br />

function Describe is to be called. This <strong>in</strong>formation can then be <strong>in</strong>terrogated at run-time to allow the<br />

appropriate version of the function Describe to be executed. By careful optimization, the overhead<br />

of dynamic b<strong>in</strong>d<strong>in</strong>g can be limited to a few mach<strong>in</strong>e cycles.<br />

The body of the test program Ma<strong>in</strong> is:<br />

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

Initialize( W414, 414, "4th Floor west w<strong>in</strong>g", 2 );<br />

Initialize( W422, 422, "4th Floor east w<strong>in</strong>g" );<br />

About( W422 );<br />

About( W414 );<br />

--Call with a room<br />

--Call with an Office<br />

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

Note:<br />

The call to about with an <strong>in</strong>stance of a Room and an Office is possible because the type of the<br />

formal parameter is Room'Class.<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!