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.

92 Day 3<br />

Inl<strong>in</strong>e functions are usually class member functions. Often the <strong>in</strong>l<strong>in</strong>e function def<strong>in</strong>ition (the<br />

function itself) is placed <strong>in</strong> the header file follow<strong>in</strong>g the class declaration. (This is the one time<br />

that you can place code <strong>in</strong> your header files.) Because the GetSpeed() function mentioned<br />

previously is so small, it can be <strong>in</strong>l<strong>in</strong>ed easily. Here’s how it would look:<br />

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

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

}<br />

An <strong>in</strong>l<strong>in</strong>e function can also be def<strong>in</strong>ed with<strong>in</strong> a class declaration. Because I haven’t talked<br />

about classes yet, though, I’ll hold that discussion for tomorrow.<br />

Summary<br />

Wow, that’s some pretty heavy stuff! Because you are read<strong>in</strong>g this, you must still be left<br />

stand<strong>in</strong>g. That’s good news. Today we got out the big guns and took on po<strong>in</strong>ters and<br />

references. Once you get a handle on po<strong>in</strong>ters, you are well on your way to understand<strong>in</strong>g<br />

<strong>C++</strong>. As part of the discussion on po<strong>in</strong>ters you learned about local versus dynamic memory<br />

allocation, which led to a discussion about the new and delete operators. Today ends with<br />

an explanation of how <strong>C++</strong> extends the use of functions over what the C language provides.<br />

Workshop<br />

The Workshop conta<strong>in</strong>s quiz questions to help you solidify your understand<strong>in</strong>g of the<br />

material covered and exercises to provide you with experience <strong>in</strong> us<strong>in</strong>g what you have learned.<br />

You can f<strong>in</strong>d answers to the quiz questions <strong>in</strong> Appendix A, “Answers to Quiz Questions.”<br />

Q&A<br />

Q Po<strong>in</strong>ters and references confuse me. Am I alone?<br />

A Absolutely not! Po<strong>in</strong>ters and references are complicated and take some time to fully<br />

understand. You will probably have to work with <strong>C++</strong> a while before you get a<br />

handle on po<strong>in</strong>ters and references.<br />

Q Do I always have to delete an object that I created dynamically with the new<br />

operator?<br />

A Yes and no. All objects created with new must have a correspond<strong>in</strong>g delete, or the<br />

program will leak memory. Some objects, however, have parent objects that will<br />

take the responsibility for delet<strong>in</strong>g them. So the question is not whether an object<br />

created with new should be deleted, but rather who should delete it. You will always<br />

want to call delete for classes you write. Later, when you learn about VCL (on

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

Saved successfully!

Ooh no, something went wrong!