04.06.2015 Views

Manual básico de Programación en C++

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Ejemplo:<br />

// Definimos dos funciones distintas max( ), una que regrese el mayor <strong>de</strong><br />

// dos <strong>en</strong>teros y otra que regrese el mayor <strong>de</strong> dos strings.<br />

#inclu<strong>de</strong> <br />

int max( int a, int b )<br />

{<br />

if ( a > b ) return a;<br />

return b;<br />

}<br />

char *max( char *a, char *b )<br />

{<br />

if ( strcmp (a,b) > 0 ) return a;<br />

return b;<br />

}<br />

int main( )<br />

{<br />

printf ( ''max( 19, 69 ) = %d\n'', max( 19, 69 ) );<br />

printf ( ''max( abc,<strong>de</strong>f ) = %s\n'', max( ''abc'',''<strong>de</strong>f'' ) );<br />

return 0;<br />

}<br />

El programa <strong>de</strong>l ejemplo <strong>de</strong>fine dos funciones que difier<strong>en</strong> <strong>en</strong> su lista <strong>de</strong> parámetros,<br />

<strong>de</strong> ahí que <strong>de</strong>fina dos funciones distintas.<br />

Las refer<strong>en</strong>cias se pue<strong>de</strong>n usar para proveer una función con un alias <strong>de</strong> un argum<strong>en</strong>to<br />

real <strong>de</strong> llamada <strong>de</strong> función. Esto permite cambiar el valor <strong>de</strong>l argum<strong>en</strong>to <strong>de</strong> llamada <strong>de</strong><br />

función tal como se conoce <strong>de</strong> otros l<strong>en</strong>guajes <strong>de</strong> programación <strong>de</strong> llamada-porrefer<strong>en</strong>cia:<br />

void mira( int porValor, int &porRefer<strong>en</strong>cia )<br />

{<br />

porValor = 42;<br />

porRefer<strong>en</strong>cia = 42;<br />

}<br />

void mar( )<br />

{<br />

int ix, jx;<br />

ix = jx = 1;<br />

mira( ix, jx );<br />

// ix == 1, jx == 42<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!