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

Create successful ePaper yourself

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

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

Now the compiler knows to treat the literal values as <strong>in</strong>ts. A cast is also used to temporarily<br />

force the compiler to treat one data type as if it were someth<strong>in</strong>g else. Let’s go back to the first<br />

example <strong>in</strong> this section and this time cast one of the variables to remove the ambiguity:<br />

<strong>in</strong>t x = 5;<br />

float y = 10.5;<br />

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

In this case x is an <strong>in</strong>t, but you are cast<strong>in</strong>g it to a float, thereby tell<strong>in</strong>g the compiler to treat<br />

it as a float. The compiler happily calls the float version of multiply() and goes on its way.<br />

Ultimately, you want to write overloaded functions so that ambiguities do not exist and<br />

cast<strong>in</strong>g is not necessary. In some cases that is not possible, and <strong>in</strong> those cases cast<strong>in</strong>g will be<br />

required.<br />

Default Parameters for Functions<br />

NEW TERM<br />

A function <strong>in</strong> <strong>C++</strong> can have default parameters which, as the name implies, supply<br />

a default value for a function if no value is specified when the function is called.<br />

A function implement<strong>in</strong>g a default parameter might look like this:<br />

// declaration, parameter ‘eraseFirst’ will be false by default<br />

void Redraw(bool eraseFirst = false);<br />

// def<strong>in</strong>ition<br />

void Redraw(bool eraseFirst)<br />

{<br />

if (eraseFirst) {<br />

// erase code<br />

}<br />

// draw<strong>in</strong>g code<br />

}<br />

When this function is called, it can be called with or without a parameter. If the parameter<br />

is supplied at the time the function is called, the function behaves as a regular function would.<br />

If the parameter is not supplied when the function is called, the default parameter is used<br />

automatically. Given this example, the follow<strong>in</strong>g two l<strong>in</strong>es of code are identical:<br />

Redraw();<br />

Redraw(false);<br />

Note that when a parameter has a default value, it can be omitted from the function call<br />

altogether. You can mix default and non-default parameters <strong>in</strong> the same function:<br />

<strong>in</strong>t PlaySound(char* name, bool loop = false, <strong>in</strong>t loops = 10);<br />

// call function<br />

<strong>in</strong>t res;<br />

res = PlaySound(“chime.wav”); // does not loop sound<br />

res = PlaySound(“d<strong>in</strong>g.wav”, true); // plays sound 10 times<br />

res = PlaySound(“bell.wave”, true, 5); // plays sound 5 times<br />

89<br />

3

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

Saved successfully!

Ooh no, something went wrong!