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 />

Gett<strong>in</strong>g Your Feet Wet<br />

numbers <strong>in</strong> the formatted str<strong>in</strong>g. You can use either function <strong>in</strong> your <strong>C++</strong>Builder programs,<br />

but spr<strong>in</strong>tf() is preferred because it has full float<strong>in</strong>g-po<strong>in</strong>t support (and it’s one less character<br />

to type!). To get a real appreciation of what spr<strong>in</strong>tf() can do for you, consult the <strong>C++</strong>Builder<br />

onl<strong>in</strong>e help.<br />

Arrays of Str<strong>in</strong>gs<br />

Not only can you have character arrays, but you can have an array of character arrays<br />

(effectively an array of str<strong>in</strong>gs). That might sound complicated, but you have already seen this<br />

type of array <strong>in</strong> the ARGSTEST program we looked at earlier. You can allocate this k<strong>in</strong>d of array<br />

as follows:<br />

char str<strong>in</strong>gs[][20] = {<br />

“This is str<strong>in</strong>g 1”,<br />

“This is str<strong>in</strong>g 2”,<br />

“This is str<strong>in</strong>g 3”,<br />

“This is str<strong>in</strong>g 4”<br />

};<br />

Although you can use this type of str<strong>in</strong>g array, there are easier ways to handle arrays of str<strong>in</strong>gs<br />

<strong>in</strong> <strong>C++</strong>Builder. (I’ll save that discussion for after you’ve had a chance to learn more about<br />

<strong>C++</strong>Builder.)<br />

Summary<br />

If you are go<strong>in</strong>g to use arrays of str<strong>in</strong>gs extensively, you should look<br />

<strong>in</strong>to the Standard Template Library (STL). STL provides <strong>C++</strong> classes<br />

that allow you to store and manipulate arrays of str<strong>in</strong>gs much more<br />

easily than is possible us<strong>in</strong>g C-style character arrays. STL also <strong>in</strong>cludes a<br />

str<strong>in</strong>g class.<br />

You’ve covered a lot of ground today. First you t<strong>in</strong>kered with the <strong>C++</strong>Builder IDE by<br />

creat<strong>in</strong>g a GUI Hello World program. Follow<strong>in</strong>g that you were <strong>in</strong>troduced to console mode<br />

applications where you created Hello World, Part II. After the <strong>in</strong>itial play<strong>in</strong>g around, you<br />

were put to work learn<strong>in</strong>g the basics of C as a foundation to learn<strong>in</strong>g <strong>C++</strong>. You have learned<br />

about the follow<strong>in</strong>g C and <strong>C++</strong> features:<br />

■ Variables<br />

■ Operators<br />

■ Data types<br />

35<br />

1

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

Saved successfully!

Ooh no, something went wrong!