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.

224 Polymorphism<br />

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

Us<strong>in</strong>g the above declarations the follow<strong>in</strong>g program can be written:<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Float_Text_Io;<br />

use <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Float_Text_Io;<br />

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

type P_Fun is access function(Item:<strong>in</strong> Float) return Float;<br />

type Vector is array ( Integer range ) of Float;<br />

-- Body of the procedures apply, square and float<br />

procedure Put( Items:<strong>in</strong> Vector ) is<br />

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

for I <strong>in</strong> Items'Range loop<br />

Put( Items(I), Fore=>4, Exp=>0, Aft=>2 ); Put(" ");<br />

end loop;<br />

end Put;<br />

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

Numbers := (1.0, 2.0, 3.0, 4.0, 5.0);<br />

Put("Square list :");<br />

Apply( Square'access, Numbers );<br />

Put( Numbers ); New_L<strong>in</strong>e;<br />

Numbers := (1.0, 2.0, 3.0, 4.0, 5.0);<br />

Put("cube list :");<br />

Apply( Cube'access, Numbers );<br />

Put( Numbers ); New_L<strong>in</strong>e;<br />

end Ex2;<br />

which when run, will produce the follow<strong>in</strong>g results:<br />

Square list : 1.00 4.00 9.00 16.00 25.00<br />

cube list : 1.00 8.00 27.00 64.00 125.00<br />

Note:<br />

A program is not allowed to take the address of a predef<strong>in</strong>ed operator such as Standard."+". This<br />

is to ease compiler implementation.<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!