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.

Wad<strong>in</strong>g In Deeper<br />

So how do the source files all get tied together? First, the compiler compiles each source file<br />

(.cpp) <strong>in</strong>to an object file (.obj). After each module has been compiled, the l<strong>in</strong>ker l<strong>in</strong>ks all the<br />

object files together to make a s<strong>in</strong>gle executable file (the .exe). The l<strong>in</strong>ker also may l<strong>in</strong>k <strong>in</strong><br />

other needed files such as resource files (.res) and library files (.lib).<br />

The declarations for classes and structures are often kept <strong>in</strong> a separate file called a<br />

header file. Headers have a filename extension of .h or .hpp. (I touched on headers<br />

briefly when I discussed the iostream class <strong>in</strong> Day 1, “Gett<strong>in</strong>g Your Feet Wet.”) A header file<br />

should conta<strong>in</strong> only class, structure, and function declarations. You should never put any<br />

code statements <strong>in</strong> a header.<br />

NEW TERM<br />

NOTE<br />

There is an exception to the rule that no code should be placed <strong>in</strong><br />

headers. You may put <strong>in</strong>l<strong>in</strong>e functions <strong>in</strong> headers. An <strong>in</strong>l<strong>in</strong>e function is a<br />

special function <strong>in</strong> terms of the way the compiler generates code for the<br />

function. You’ll learn more about <strong>in</strong>l<strong>in</strong>e functions on Day 4, “Totally<br />

Immersed: <strong>C++</strong> Classes and Object-Oriented Programm<strong>in</strong>g,” when I<br />

discuss classes.<br />

Once you have created a header file for a class or structure, you can <strong>in</strong>clude that header <strong>in</strong><br />

any source code module that needs to see the class or structure declaration. To do that you<br />

use the #<strong>in</strong>clude directive:<br />

#<strong>in</strong>clude “structur.h”<br />

When you use the #<strong>in</strong>clude directive, it is as if the contents of the file be<strong>in</strong>g <strong>in</strong>cluded<br />

were pasted <strong>in</strong>to the source file at that po<strong>in</strong>t. List<strong>in</strong>g 2.4, <strong>in</strong> the next section, conta<strong>in</strong>s a<br />

program that uses the #<strong>in</strong>clude directive. The header file used <strong>in</strong> List<strong>in</strong>g 2.4 is conta<strong>in</strong>ed <strong>in</strong><br />

List<strong>in</strong>g 2.5.<br />

TIP<br />

Header files typically implement a sentry to ensure that the header is<br />

<strong>in</strong>cluded only once for a program. A sentry essentially tells the compiler,<br />

“I’ve already been <strong>in</strong>cluded once, so don’t <strong>in</strong>clude me aga<strong>in</strong>.” A<br />

sentry looks like this:<br />

#ifndef _MYCLASS_H<br />

#def<strong>in</strong>e _MYCLASS_H<br />

class MyClass {<br />

// class declared here<br />

};<br />

#endif<br />

59<br />

2

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

Saved successfully!

Ooh no, something went wrong!