12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

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.

NOTE<br />

NOTE<br />

Up to Your Neck <strong>in</strong> <strong>C++</strong><br />

can create such a scenario thanks to function overload<strong>in</strong>g. Here’s how the declarations for an<br />

overloaded function look:<br />

// declarations <strong>in</strong> <strong>C++</strong><br />

<strong>in</strong>t multiply(<strong>in</strong>t num1, <strong>in</strong>t num2);<br />

float multiply(float num1, float num2);<br />

short multiply(short num1, short num2);<br />

You still have to write separate functions for each of these declarations, but at least you can<br />

use the same function name. The compiler takes care of call<strong>in</strong>g the correct function based on<br />

the parameters you pass the function. For example:<br />

float x = 1.5;<br />

float y = 10.5;<br />

float result = multiply(x, y);<br />

The compiler sees that two floats are passed to the function and calls the version of the<br />

multiply() function that takes two float<strong>in</strong>g-po<strong>in</strong>t values for parameters. Likewise, if two <strong>in</strong>ts<br />

are passed, the compiler calls the version of multiply() that takes two <strong>in</strong>tegers.<br />

It is the parameter list that makes overloaded functions work. You can<br />

vary either the type or the number of parameters a function takes (or<br />

both), but you cannot create an overloaded function by chang<strong>in</strong>g just<br />

the return value. For example, the follow<strong>in</strong>g does not constitute an<br />

overloaded function:<br />

<strong>in</strong>t DoSometh<strong>in</strong>g();<br />

void DoSometh<strong>in</strong>g();<br />

If you try to compile a program conta<strong>in</strong><strong>in</strong>g these l<strong>in</strong>es, you will get a<br />

compiler error that says, Type mismatch <strong>in</strong> redeclaration of<br />

‘DoSometh<strong>in</strong>g()’. The two functions need to vary by more than just the<br />

return value <strong>in</strong> order to have overloaded functions.<br />

Compilers keep track of overloaded functions <strong>in</strong>ternally through a<br />

process called name mangl<strong>in</strong>g. Name mangl<strong>in</strong>g means that the compiler<br />

creates a function name that takes <strong>in</strong>to account the parameter list of the<br />

function. Internally, the compiler refers to the mangled name rather<br />

than the pla<strong>in</strong> text name you would recognize. For example, for the<br />

multiply function tak<strong>in</strong>g two float values, the mangled name might be<br />

multiply$qff.<br />

87<br />

3

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

Saved successfully!

Ooh no, something went wrong!