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.

200 Child libraries<br />

14.4.1 Example of the use of the generic procedure G_3Order<br />

A program to order the height of three people is shown below. In this program, a record of type Person is<br />

created for each of the <strong>in</strong>dividuals. A specific <strong>in</strong>stance of the generic procedure G_3Order is created to order<br />

these records <strong>in</strong>to ascend<strong>in</strong>g height order.<br />

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

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

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

Max_Chs : constant := 7;<br />

type Gender is ( Female, Male );<br />

type Height_Cm is range 0 .. 300;<br />

type Person is record<br />

Name : Str<strong>in</strong>g( 1 .. Max_Chs ); --Name as a Str<strong>in</strong>g<br />

Height : Height_Cm := 0; --Height <strong>in</strong> cm.<br />

Sex : Gender; --Gender of person<br />

end record;<br />

function Gt( First,Second:<strong>in</strong> Person ) return Boolean is<br />

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

return First.Height > Second.Height;<br />

end Gt;<br />

procedure Order is new G_3Order( Person, Gt ); --Instantiate<br />

Person1 : Person := ("Cor<strong>in</strong>na", 171, Female );<br />

Person2 : Person := ("Miranda", 74, Female );<br />

Person3 : Person := ("Mike ", 183, Male );<br />

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

Order( Person1, Person2, Person3 );<br />

Put("List of people <strong>in</strong> ascend<strong>in</strong>g height order are"); New_L<strong>in</strong>e;<br />

Put( Person1.Name ); New_L<strong>in</strong>e;<br />

Put( Person2.Name ); New_L<strong>in</strong>e;<br />

Put( Person3.Name ); New_L<strong>in</strong>e;<br />

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

which when run would pr<strong>in</strong>t:<br />

List of people <strong>in</strong> ascend<strong>in</strong>g height order are<br />

Miranda<br />

Cor<strong>in</strong>na<br />

Mike<br />

14.4.2 Summary<br />

The follow<strong>in</strong>g table summarizes the effect of different subprogram specifications for a formal parameter.<br />

Generic formal subprogram<br />

with function ">" ( a, b:<strong>in</strong> T )<br />

return Boolean is ;<br />

with function ">" ( a, b:<strong>in</strong> T )<br />

return Boolean;<br />

with procedure exec is exec;<br />

Explanation<br />

Has a default value of the current<br />

def<strong>in</strong>ition of ">" at the po<strong>in</strong>t of<br />

<strong>in</strong>stantiation of the generic subprogram.<br />

Takes the value of the formal parameter<br />

">" at the po<strong>in</strong>t of <strong>in</strong>stantiation of the<br />

generic subprogram.<br />

Takes the value of the formal parameter<br />

at the po<strong>in</strong>t of def<strong>in</strong>ition of the generic<br />

subprogram.<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!