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.

64 Day 2<br />

NOTE<br />

seen, I have been us<strong>in</strong>g getch() at the end of my programs to keep the program from clos<strong>in</strong>g<br />

prematurely, but have been ignor<strong>in</strong>g the return value. The getch() function returns the<br />

ASCII value of the key pressed. Because the ASCII value of the 1 key is 49, I want to subtract<br />

49 from the value of the key pressed to obta<strong>in</strong> the equivalent <strong>in</strong>dex number for that record<br />

<strong>in</strong> the records array. If the user presses 1, an ASCII 49 is returned, and 49–49 is 0, which<br />

is the first <strong>in</strong>dex of the array. If the user presses 2, the calculation yields 1 (50–49), and so on.<br />

The do-while loop ensures that the user presses a key between 1 and 3. If a key other than<br />

1, 2, or 3 is pressed, the loop cont<strong>in</strong>ues to fetch keystrokes until a valid key is pressed.<br />

F<strong>in</strong>ally, I want to po<strong>in</strong>t out l<strong>in</strong>e 66 <strong>in</strong> List<strong>in</strong>g 2.4:<br />

mail<strong>in</strong>gListRecord temp = listArray[rec];<br />

This code is not necessary <strong>in</strong> this program, but I <strong>in</strong>cluded it to illustrate a po<strong>in</strong>t. This code<br />

creates an <strong>in</strong>stance of the mail<strong>in</strong>gListRecord structure and assigns to it the contents of one<br />

of the structures <strong>in</strong> the array. A simple assignment is possible here because the compiler knows<br />

how to copy one structure to another. It does a simple member-to-member copy and copies<br />

all structure members to the newly created <strong>in</strong>stance of the structure.<br />

Our discussion of structures up to this po<strong>in</strong>t describes how a structure<br />

works <strong>in</strong> C. In <strong>C++</strong> a structure operates like it does <strong>in</strong> C, but <strong>C++</strong><br />

extends structures to allow them to conta<strong>in</strong> functions as well as data<br />

members. In fact, a structure <strong>in</strong> <strong>C++</strong> is essentially a class where all data<br />

members and functions have public access. That won’t make sense<br />

until later on when I discuss classes on Day 4, but you can file this<br />

tidbit away for future reference.<br />

Now you know about structures. Chances are you won’t use a lot of structures <strong>in</strong> your<br />

programs. This section is important, though, because it serves as sort of a primer for<br />

discuss<strong>in</strong>g classes <strong>in</strong> Day 3, “Up to Your Neck <strong>in</strong> <strong>C++</strong>.”<br />

Summary<br />

This chapter conta<strong>in</strong>s essential <strong>in</strong>formation on some of <strong>C++</strong>’s basic operations. You need<br />

what is presented here <strong>in</strong> order to program <strong>in</strong> <strong>C++</strong>Builder. First you learned about the<br />

different types of loops <strong>in</strong> <strong>C++</strong>; then you learned about the switch statement and how to use<br />

it. I talked a little about scope and what that means to your variables. Then you found out<br />

what structures are and how they can be used <strong>in</strong> your programs. Tomorrow we’ll tackle some<br />

of the big stuff.

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

Saved successfully!

Ooh no, something went wrong!