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.

12 Def<strong>in</strong><strong>in</strong>g new operators<br />

This chapter shows how the predef<strong>in</strong>ed operators <strong>in</strong> <strong>Ada</strong> can be overloaded with a new mean<strong>in</strong>g.<br />

12.1 Def<strong>in</strong><strong>in</strong>g operators <strong>in</strong> <strong>Ada</strong><br />

The exist<strong>in</strong>g operators <strong>in</strong> <strong>Ada</strong> can be overloaded with a new mean<strong>in</strong>g. These new operators have the same<br />

precedence as their exist<strong>in</strong>g counterparts. For example, to trace every executed <strong>in</strong>teger + operation <strong>in</strong> a program,<br />

the operator + can be overloaded by a function that writes trace <strong>in</strong>formation to the term<strong>in</strong>al before deliver<strong>in</strong>g the<br />

normal <strong>in</strong>teger addition. This is implemented by the follow<strong>in</strong>g function:<br />

function "+" ( F:<strong>in</strong> Integer; S:<strong>in</strong> Integer ) return Integer is<br />

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

Put("[Perform<strong>in</strong>g "); Put(F, Width=>1 );<br />

Put(" + "); Put(S, Width=>1 ); Put("]");<br />

return Standard."+"( F, S );<br />

end "+";<br />

Note: To perform the <strong>in</strong>built + the functional notation for the plus operation must be used. This is written as<br />

Standard."+"( f, s ). In <strong>Ada</strong>, the <strong>in</strong>built operators are considered to belong to the package<br />

Standard which is automatically made visible to all program units.<br />

Section C.4, Appendix C gives the specification for the package standard.<br />

The above function can be used to trace the use of + <strong>in</strong> the follow<strong>in</strong>g program:<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 />

function "+" ( F:<strong>in</strong> Integer; S:<strong>in</strong> Integer ) return Integer is<br />

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

Put("[Perform<strong>in</strong>g "); Put(F, Width=>1 );<br />

Put(" + "); Put(S, Width=>1 ); Put("]");<br />

return Standard."+"( F, S );<br />

end "+";<br />

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

Put("The sum of 1 + 2 is: "); Put ( 1+2 ); New_L<strong>in</strong>e;<br />

Put("The sum of 1 + 2 is: ");<br />

Put( Standard."+"(1,2), Width=>1 ); New_L<strong>in</strong>e;<br />

Put("The sum of 1 + 2 is: ");<br />

Put( "+"(1,2), Width=>1 ); New_L<strong>in</strong>e;<br />

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

Note:<br />

As the package Standard is considered to be <strong>in</strong>cluded with all program units. To achieve the effect<br />

of trac<strong>in</strong>g each use of +, the overloaded function "+" has to be a nested function of the program unit.<br />

The sum of 1 + 2 is: [Perform<strong>in</strong>g 1 + 2]3<br />

The sum of 1 + 2 is: 3<br />

The sum of 1 + 2 is: [Perform<strong>in</strong>g 1 + 2]3<br />

Note: The way of directly us<strong>in</strong>g the operator + def<strong>in</strong>ed <strong>in</strong> the package Standard<br />

'Standard."+"(1,2)’.<br />

The function notation for the use of the operator + "+"(1,2).<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!