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.

56 Day 2<br />

The extern keyword tells the compiler, “I’m go<strong>in</strong>g to be us<strong>in</strong>g a variable <strong>in</strong> this source file<br />

that you will f<strong>in</strong>d declared <strong>in</strong> another source file.” The compiler sorts it all out at compile time<br />

and makes sure you get access to the correct variable.<br />

While global variables are convenient, they aren’t particularly OOP friendly. Usually there<br />

are better solutions (which you will learn about when I discuss classes). In addition, global<br />

variables consume memory for the life of the program. Local variables use up memory only<br />

while they are <strong>in</strong> scope. Use local variables whenever possible, and keep the use of global<br />

variables to a m<strong>in</strong>imum.<br />

Structures<br />

A structure is a collection of related data rolled up <strong>in</strong>to a s<strong>in</strong>gle storage unit. For <strong>in</strong>stance, let’s<br />

say you wanted to keep a mail<strong>in</strong>g list. It would be convenient to be able to have a s<strong>in</strong>gle data<br />

variable that could be used to hold all the fields needed <strong>in</strong> a typical mail<strong>in</strong>g list. A structure<br />

will allow you to do that. You first declare a structure and then later create an <strong>in</strong>stance of that<br />

structure when you want to put the structure to use. A structure is declared with the struct<br />

keyword:<br />

struct mail<strong>in</strong>gListRecord {<br />

char firstName[20];<br />

char lastName[20];<br />

char address[50];<br />

char city[20];<br />

char state[4];<br />

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

bool aFriend;<br />

bool aFoe;<br />

};<br />

Each of the elements of a structure is called a data member. You will notice that each of the<br />

data members must be declared just as it would be if it were a variable <strong>in</strong> a code block. In this<br />

example I have five char arrays, one <strong>in</strong>t, and two bool data members. (My apologies to my<br />

friends around the world if this looks like a U.S.-slanted mail<strong>in</strong>g-list record.) F<strong>in</strong>ally, make<br />

note of the semicolon follow<strong>in</strong>g the clos<strong>in</strong>g brace of the structure declaration. This is a<br />

requirement for structure and class declarations.<br />

A structure is a collection of related data identified as a s<strong>in</strong>gle storage unit. After a<br />

structure has been declared, an <strong>in</strong>stance of that structure can be created for use. Each<br />

of the elements of a structure is called a data member.<br />

NEW TERM

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

Saved successfully!

Ooh no, something went wrong!