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.

210 Polymorphism<br />

The storage for P_People can be visualized as illustrated <strong>in</strong> Figure 15.2.<br />

P_People<br />

24<br />

People<br />

Figure 15.2 Storage for P_People after it has been assigned the access value of people.<br />

15.1.1 Access to an object via its access value<br />

To access an object via its access value requires the use of .all which de-references the access value. This may<br />

be thought of as an <strong>in</strong>direction operator. For example, the follow<strong>in</strong>g program accesses the object people by<br />

us<strong>in</strong>g the access value for People stored <strong>in</strong> the object P_People.<br />

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

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

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

type P_Integer is access all Integer;<br />

People : aliased Integer;<br />

P_People : P_Integer;<br />

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

People := 24;<br />

P_People := People'Access; --Access value for people<br />

Put("The number of people is : "); Put( P_People.all );<br />

New_L<strong>in</strong>e;<br />

End Ma<strong>in</strong>;<br />

Note:<br />

In the declaration of P_<strong>in</strong>teger, access all signifies that read and write access may be made to the<br />

object via its access value.<br />

which when run, would produce:<br />

The number of people is : 24<br />

The ideas described above have their orig<strong>in</strong>s <strong>in</strong> low-level assembly language programm<strong>in</strong>g where the address<br />

of an item may be freely taken and manipulated. <strong>Ada</strong> provides a more discipl<strong>in</strong>ed way of implement<strong>in</strong>g these<br />

concepts.<br />

15.1.2 Lvalues and rvalues<br />

In work<strong>in</strong>g with access values it is convenient to th<strong>in</strong>k about the lvalue and rvalue of an object. The lvalue is the<br />

access value of the object, whilst the rvalue is the contents of the object. For example, <strong>in</strong> the statement:<br />

value := amount;<br />

the object amount will deliver its contents, whilst the object value will deliver its access value so that the<br />

rvalue of amount may be assigned to it. The names lvalue and rvalue are an <strong>in</strong>dication of whether the object is<br />

on the left or the right-hand side of an assignment statement.<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!