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.

Conta<strong>in</strong>ers 271<br />

17.7.1 Putt<strong>in</strong>g it all together<br />

The program below illustrates the use of a set to record the <strong>in</strong>gredients <strong>in</strong> a sandwich.<br />

package Pack_Types is<br />

type Fill<strong>in</strong>g is ( Cheese, Onion, Ham, Tomato );<br />

end Pack_Types;<br />

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

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

procedure Put_Fill<strong>in</strong>g( C:<strong>in</strong> Fill<strong>in</strong>g ) is<br />

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

Put( Fill<strong>in</strong>g'Image( C ) );<br />

end Put_Fill<strong>in</strong>g;<br />

with Pack_Types, Class_Set, Put_Fill<strong>in</strong>g;<br />

use Pack_Types;<br />

pragma Elaborate_All( Class_Set );<br />

package Class_Set_Sandwich is<br />

new Class_Set( T => Pack_Types.Fill<strong>in</strong>g, Put => Put_Fill<strong>in</strong>g );<br />

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

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

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

Sandwich : Class_Set_Sandwich.Set;<br />

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

Sandwich := Sandwich + Set_Const(Cheese);<br />

Sandwich := Sandwich + Set_Const(Onion) ;<br />

Put("Contents of sandwich are : ");<br />

Put( Sandwich ); New_L<strong>in</strong>e;<br />

Put("Number of <strong>in</strong>gredients is : ");<br />

Put( Members(Sandwich) ); New_L<strong>in</strong>e;<br />

null;<br />

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

Note:<br />

The <strong>in</strong>stantiation of the class Class_Set_Sandwich uses the default def<strong>in</strong>itions of > and < taken<br />

from the environment.<br />

An <strong>in</strong>stantiation of a class which is <strong>in</strong>herited from Controlled must be at the library level.<br />

which when run, will produce the follow<strong>in</strong>g results:<br />

Contents of sandwich are : (CHEESE,ONION)<br />

Number of <strong>in</strong>gredients is : 2<br />

17.8 Self-assessment<br />

• What is the purpose of an iterator?<br />

• When <strong>in</strong>herit<strong>in</strong>g from Controlled, the user can provide the follow<strong>in</strong>g procedures:<br />

F<strong>in</strong>alize, Initialize, and Adjust.<br />

What is the purpose of these procedures?<br />

• If A and B are controlled objects, what happens when the assignment:<br />

A := B;<br />

is made?<br />

• What is the difference between a deep and a shallow copy?<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!