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.

Data structures 97<br />

Then an assignment to an <strong>in</strong>stance of Person is:<br />

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

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

Note: The value of the discrim<strong>in</strong>ant must be specified <strong>in</strong> the record aggregate.<br />

However, Mike, Cor<strong>in</strong>na and Younger are not of the same type so the assignment:<br />

Younger := Cor<strong>in</strong>na; -- Fail at run-time<br />

will fail at run-time as the discrim<strong>in</strong>ants of the record are not identical. The object Younger conta<strong>in</strong>s a Str<strong>in</strong>g<br />

of length of 10 whilst the object Cor<strong>in</strong>na conta<strong>in</strong>s a Str<strong>in</strong>g of length 7.<br />

7.5 Default values to a discrim<strong>in</strong>ant<br />

A discrim<strong>in</strong>ant to a type may have a default value. If a value is not specified with the declaration of a<br />

discrim<strong>in</strong>ated type then it is an unconstra<strong>in</strong>ed discrim<strong>in</strong>ated type. An <strong>in</strong>stance of an unconstra<strong>in</strong>ed discrim<strong>in</strong>ated<br />

type may be assigned or compared with other unconstra<strong>in</strong>ed discrim<strong>in</strong>ants of the same type name.<br />

For example, if the data structure Person is now def<strong>in</strong>ed as:<br />

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

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

subtype Str_Range is Natural range 0 .. 20;<br />

type Person( Chs:Str_Range := 0 ) is record --Length of name<br />

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

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

Sex : Gender; --Gender<br />

end record;<br />

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

declare<br />

Mike : Person; --Unconstra<strong>in</strong>ed<br />

Cor<strong>in</strong>na: Person; --Unconstra<strong>in</strong>ed<br />

Younger: Person; --Unconstra<strong>in</strong>ed<br />

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

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

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

Younger := Cor<strong>in</strong>na;<br />

if Cor<strong>in</strong>na = Younger then<br />

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

end if;<br />

end;<br />

Note:<br />

It would still be an error to write:<br />

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

as the length of "Cor<strong>in</strong>na" is not 10 characters.<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!