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.

Exceptions 181<br />

12.3.5 use type<br />

A modified form of the use clause allows operators from a package to be used without hav<strong>in</strong>g to prefix the<br />

operator with the package name. Other components however, from the package need to be prefixed with the<br />

package name when used. This modified form of the use clause is use type which is followed by the type name<br />

whose operators can be used without prefix<strong>in</strong>g them by the package name. For example, the follow<strong>in</strong>g program<br />

requires all components <strong>in</strong> the package Bounded_Str<strong>in</strong>g except for operators to be prefixed with the package<br />

name.<br />

with <strong>Ada</strong>.Text_Io, Class_Bounded_Str<strong>in</strong>g;<br />

use type Class_Bounded_Str<strong>in</strong>g.Bounded_Str<strong>in</strong>g;<br />

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

Town : Class_Bounded_Str<strong>in</strong>g.Bounded_Str<strong>in</strong>g :=<br />

Class_Bounded_Str<strong>in</strong>g.To_Bounded_Str<strong>in</strong>g("Brighton");<br />

County: Class_Bounded_Str<strong>in</strong>g.Bounded_Str<strong>in</strong>g :=<br />

Class_Bounded_Str<strong>in</strong>g.To_Bounded_Str<strong>in</strong>g("E Sussex");<br />

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

<strong>Ada</strong>.Text_Io.Put(<br />

Class_Bounded_Str<strong>in</strong>g.To_Str<strong>in</strong>g( Town & " " & County )<br />

);<br />

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

12.4 Self-assessment<br />

• What operators can be overloaded with a new mean<strong>in</strong>g <strong>in</strong> <strong>Ada</strong>?<br />

• Can a user <strong>in</strong>vent new operators? For example, could a user def<strong>in</strong>e the monadic operator ++ to add<br />

one to an <strong>in</strong>teger?<br />

• Why might excessive use of overload<strong>in</strong>g the standard operators lead to a program that is difficult to<br />

follow?<br />

• Why is the function rat_const needed <strong>in</strong> the class Rational?<br />

• How can a user guarantee to use the def<strong>in</strong>ition for the operator + <strong>in</strong> the package standard?<br />

12.5 Exercises<br />

Construct the follow<strong>in</strong>g class:<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!