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.

98 Data structures<br />

7.5.1 Constra<strong>in</strong>ed vs. unconstra<strong>in</strong>ed discrim<strong>in</strong>ants<br />

Us<strong>in</strong>g the last def<strong>in</strong>ition of type Person:<br />

Declaration The object Mike is Comment<br />

Mike: Person; Unconstra<strong>in</strong>ed The variable Mike may be<br />

compared with or assigned any<br />

other <strong>in</strong>stance of Person.<br />

Mike: Person(4); Constra<strong>in</strong>ed May only be assigned or<br />

compared with another<br />

Person(4).<br />

7.5.2 Restrictions on a discrim<strong>in</strong>ant<br />

A discrim<strong>in</strong>ant must be a discrete type or access type. If it is an access type then the record must be limited. This<br />

unfortunately means that a Float cannot be used as a discrim<strong>in</strong>ant to a record.<br />

7.6 Variant records<br />

There will be occasions when a data structure conta<strong>in</strong>s data items that are mutually exclusive. For example, <strong>in</strong> a<br />

description of a person who may be a Lecturer or a Student the data members are:<br />

Data member Belongs to Description<br />

Name<br />

Both a Lecturer The name of the person.<br />

and a Student<br />

Class_Size Lecturer<br />

The size of the group that the lecturer<br />

teaches.<br />

Full_Time<br />

Grade<br />

Student<br />

Whether or not the student is full-time or<br />

part-time.<br />

The mark out of 100 that the student<br />

ga<strong>in</strong>s at the end of the course.<br />

In this example the storage for Class_Size can overlay all or part of the storage for Full_Time and<br />

Grade as the components:<br />

• Class_Size<br />

• Full_Time and Grade<br />

of the data structure will not be used simultaneously.<br />

This can be visualized as:<br />

role -> Student Name Full_Time grade<br />

role -> Lecturer Name Class_Size<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!