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.

NOTE<br />

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

<strong>in</strong>t Airplane::GetSpeed()<br />

{<br />

return speed; // speed is a class member variable<br />

}<br />

In this case, the scope-resolution operator tells the compiler that the GetSpeed() function is<br />

a member of the Airplane class. I’ll talk more about class member functions when I discuss<br />

classes tomorrow.<br />

Inl<strong>in</strong>e Functions<br />

Tradition has it that class member function names beg<strong>in</strong> with uppercase<br />

letters. There is no hard and fast rule about this, but you will f<strong>in</strong>d<br />

that most <strong>C++</strong> programs follow this tradition. As a further note, I am<br />

not a fan of the underscore character <strong>in</strong> function names. For example, I<br />

much prefer the function name GetVideoRect() over the name<br />

get_video_rect(). Regardless of what nam<strong>in</strong>g convention you use for<br />

your functions, be consistent and use the same nam<strong>in</strong>g convention<br />

throughout your programs.<br />

Normally a function only appears <strong>in</strong> the executable file once. Each section of code that uses<br />

the function calls the function. This means that program execution jumps from the po<strong>in</strong>t<br />

of the function call to the po<strong>in</strong>t <strong>in</strong> the program where the function resides. The statements<br />

<strong>in</strong> the function are executed, and then the function returns. When the function returns,<br />

program execution jumps back to the statement follow<strong>in</strong>g the function call.<br />

An <strong>in</strong>l<strong>in</strong>e function, as its name implies, is placed <strong>in</strong>l<strong>in</strong>e <strong>in</strong> the compiled code wherever<br />

a call to that function occurs.<br />

NEW TERM<br />

NOTE<br />

Inl<strong>in</strong>e functions are declared like regular functions but are def<strong>in</strong>ed with the <strong>in</strong>l<strong>in</strong>e keyword.<br />

Each time the compiler encounters a call to an <strong>in</strong>l<strong>in</strong>e function <strong>in</strong> the source code, it places<br />

a separate copy of the function’s code <strong>in</strong> the executable program at that po<strong>in</strong>t. Inl<strong>in</strong>e<br />

functions execute quickly because no actual function call takes place (the code is already<br />

<strong>in</strong>l<strong>in</strong>ed <strong>in</strong> the program).<br />

Inl<strong>in</strong>e functions should be reserved for functions that are very small or<br />

for those that need to be executed very quickly. Large functions or<br />

those that are called from many places <strong>in</strong> your program should not be<br />

<strong>in</strong>l<strong>in</strong>ed because your executable file will be larger as a result.<br />

91<br />

3

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

Saved successfully!

Ooh no, something went wrong!