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.

68 Procedures and functions<br />

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

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

procedure Answer_Is( N:<strong>in</strong> Integer;<br />

Message:<strong>in</strong> Boolean := True) is<br />

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

if Message then Put("The answer = "); end if;<br />

Put( N, Width=>1 );<br />

if Message then New_L<strong>in</strong>e; end if;<br />

end Answer_Is;<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 Is_A_Int( An_Int:<strong>in</strong> Integer ) is<br />

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

Put("The parameter is an Integer: value = ");<br />

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

end Is_A_Int;<br />

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

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

procedure Is_A_Float( A_Float:<strong>in</strong> Float ) is<br />

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

Put("The parameter is a Float: value = ");<br />

Put( A_Float, Aft=>2, Exp=>0 ); New_L<strong>in</strong>e;<br />

end Is_A_Float;<br />

The <strong>in</strong>dividual procedures have unique names so that they can be identified and re-used <strong>in</strong> a program. This is a<br />

consequence of each procedure be<strong>in</strong>g a separate compilation unit. However, <strong>Ada</strong> allows the renam<strong>in</strong>g of a<br />

procedure or function. By choos<strong>in</strong>g the same name a user can overload a particular name with several different<br />

def<strong>in</strong>itions. For example, a program unit can be written which renames the three different procedure names<br />

(Is_A_Int, Is_A_Float, Is_A_Char) with the same overloaded name Is_A.<br />

with Is_A_Int, Is_A_Float, Is_A_Char;<br />

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

procedure Is_A( The:<strong>in</strong> Integer ) renames Is_A_Int;<br />

procedure Is_A( The:<strong>in</strong> Float ) renames Is_A_Float;<br />

procedure Is_A( The:<strong>in</strong> Character ) renames Is_A_Char;<br />

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

Is_A( 'A' );<br />

Is_A( 123 );<br />

Is_A( 123.45 );<br />

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

Note: It is possible to write several functions or procedures with the same name directly by us<strong>in</strong>g the package<br />

construct.<br />

When run this program would pr<strong>in</strong>t the type and value of the argument passed to Is_A.<br />

The parameter is a Character: value = A<br />

The parameter is an Integer: value = 123<br />

The parameter is a Float: value = 123.450<br />

Of course, for this to happen, the actual function called must be different <strong>in</strong> each case. The name Is_A is<br />

overloaded by three different functions. The b<strong>in</strong>d<strong>in</strong>g between the called function and its body is worked out by the<br />

compiler at compile-time us<strong>in</strong>g the signature of the different functions that have been overloaded to resolve any<br />

conflicts.<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!