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.

Data structures <strong>95</strong><br />

This <strong>in</strong>itialization can be more elegantly expressed us<strong>in</strong>g a record aggregate which is then assigned to the<br />

object mike.<br />

Mike := (Name=> "Mike ", Height=> 183, Sex=> Male);<br />

Note:<br />

The construct:<br />

(name => "Mike ", height => 183, sex => Male )<br />

is a record aggregate.<br />

The record aggregate can also be def<strong>in</strong>ed us<strong>in</strong>g the absolute position of the arguments or a mixture of absolute<br />

and named arguments. For example, the follow<strong>in</strong>g three assignments are all equal <strong>in</strong> effect.<br />

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

Cor<strong>in</strong>na := (Name=> "Cor<strong>in</strong>na ", Sex=> Female, Height=> 171);<br />

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

Note:<br />

A record aggregate must have all its components specified even if some components have default<br />

values. Once a named parameter <strong>in</strong> an aggregate has been used, all parameters to the right must also<br />

be named.<br />

If there is only one member of the record aggregate then it must still be enclosed <strong>in</strong> brackets.<br />

A data structure may be compared for equality or assigned. For example, us<strong>in</strong>g the declarations:<br />

Cor<strong>in</strong>na, Mike, Miranda : Person;<br />

Taller : Person;<br />

the follow<strong>in</strong>g code can be written:<br />

Mike := (Name=>"Mike ", Height=>183, Sex=>Male);<br />

Cor<strong>in</strong>na:= (Name=>"Cor<strong>in</strong>na ", Height=>171, Sex=>Female);<br />

Miranda:= (Name=>"Miranda ", Height=>74, Sex=>Female);<br />

Taller := Mike;<br />

if mike = Taller then<br />

Put("Mike taller"); New_L<strong>in</strong>e;<br />

end if;<br />

if Mike /= Taller and Cor<strong>in</strong>na /= Taller then<br />

Put("Miranda taller"); New_L<strong>in</strong>e;<br />

end if;<br />

7.2.1 Other operations allowed on data structures<br />

Chapter 12 describes how new mean<strong>in</strong>gs for the <strong>in</strong>built operators <strong>in</strong> <strong>Ada</strong> can be def<strong>in</strong>ed. Us<strong>in</strong>g these techniques to<br />

def<strong>in</strong>e an additional mean<strong>in</strong>g for > between <strong>in</strong>stances of a Person would allow the follow<strong>in</strong>g to be written:<br />

if Mike > Cor<strong>in</strong>na then<br />

Put("Mike taller"); New_L<strong>in</strong>e;<br />

else<br />

Put("Cor<strong>in</strong>na taller"); New_L<strong>in</strong>e;<br />

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