







































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
useful book foe C++ programming language
Typology: Study notes
1 / 47
This page cannot be seen from the preview
Don't miss anything!








































800 East 96th Street, Indianapolis, Indiana, 46240 USA
Sams Teach Yourself
in
iv
vi
About the Authors
Jesse Liberty is the author of numerous books on software development, including best-sell- ing titles on C++ and .NET. He is the president of Liberty Associates, Inc. (http://www.lib- ertyassociates.com), where he provides custom programming, consulting, and training.
Rogers Cadenhead is a writer, computer programmer, and web developer who has written 23 books on Internet-related topics, including Sams Teach Yourself Java in 21 Days and Sams Teach Yourself Java in 24 Hours. He publishes the Drudge Retort and other websites that receive more than 22 million visits a year. This book’s official website is at http://cplusplus.cadenhead.org.
Dedications
Acknowledgments
With each book, there is a chance to acknowledge and to thank those folks without whose support and help this book literally would have been impossible. First among them are Stacey, Robin, and Rachel Liberty.
—Jesse Liberty
A book like this requires the hard work and dedication of numerous people. Most of them are at Sams Publishing in Indianapolis, and to them I owe considerable thanks—in particu- lar, to Keith Cline, Mandie Frank, Songlin Qiu, Mark Taber, and Jon Upchurch. Most of all, I thank my incredible wife, Mary, and sons, Max, Eli, and Sam.
—Rogers Cadenhead
This page intentionally left blank
Introduction
Congratulations! By reading this sentence, you are already 20 seconds closer to learning C++, one of the most important programming languages in the world.
If you continue for another 23 hours, 59 minutes, and 40 seconds, you will master the fundamentals of the C++ programming language. Twenty-four 1-hour lessons cover the fundamentals, such as managing I/O, creating loops and arrays, using object-oriented programming with templates, and creating C++ programs.
All of this has been organized into well-structured, easy-to-follow lessons. There are working projects that you create—complete with output and an analysis of the code—to illustrate the topics of the hour. Syntax examples are clearly marked for handy reference.
To help you become more proficient, each hour ends with a set of common questions and answers.
You don’t need any previous experience in programming to learn C++ with this book.
This book starts with the basics and teaches you both the language and the concepts involved with programming C++. Whether you are just beginning or already have some experience programming, you will find that this book makes learning C++ fast and easy.
No, you don’t need to learn C first. C++ is a much more powerful and versatile lan- guage that was created by Bjarne Stroustrup as a successor to C. Learning C first can lead you into some programming habits that are more error-prone than what you’ll do in C++. This book does not assume that readers are familiar with C.
When you see this symbol, you know that what you see next will show the output from a code listing/example.
This book uses various typefaces:
set in a special monospace font.
or characters you would type in code—are typeset in italic monospace.
unnumbered line in a listing, you’ll know that the unnumbered line is really a continuation of the preceding numbered code line (some code lines are too long for the width of the book). In this case, you should type the two lines as one; do not divide them.
This page intentionally left blank
102 103 104
17
zombies
A visual repre- sentation of memory.
Storing Variables in Memory When you create a variable in C++, you must tell the compiler the variable’s name and what kind of information it will hold, such as an integer, character, or floating- point number. This is the variable’s type (sometimes called data type ). The type tells the compiler how much room to set aside in memory to hold the variable’s value. Each cubbyhole in memory can hold 1 byte. If a variable’s type is 2 bytes in size, it needs 2 bytes of memory. Because computers use bytes to represent values, it is important that you familiarize yourself with this concept. A short integer, represented by short in C++, is usually 2 bytes. A long integer (long) is 4 bytes, an integer (int) can be 2 or 4 bytes, and a long long integer is 8 bytes. Characters of text are represented by the char type in C++, which usually is 1 byte in size. In Figure 3.1 shown earlier, each cubbyhole holds 1 byte. A single short integer could be stored in addresses 106 and 107. True-false values are stored as the bool type. The values true and false are the only values it can hold. The size of a short always is smaller than or the same as an int. The size of an int is always the same or smaller than a long. Floating-point numeric types are different and are discussed later this hour. The usual type sizes thus far described do not hold true on all systems. You can check the size a type holds in C++ using sizeof(), an element of the language called a function. The parentheses that follow sizeof should be filled with the name of a type, as in this statement: std:cout << sizeof(int) << “\n”;
This statement displays the number of bytes required to store an integer variable. The sizeof() function is provided by the compiler and does not require an include directive. The Sizer program in Listing 3.1 relies on the sizeof() function to report the sizes of common C++ types on your computer.
1: #include
This program makes use of a new feature of C++0x, the next version of the language. The long long int data type holds extremely large integers. If your compiler fails with an error, it may not support this feature yet. Delete lines 19–20 and try again to see if that’s the problem.
After being compiled, this program produces the following output when run on a Linux Ubuntu 9.10 system: The size of an integer: 4 bytes The size of a short integer: 2 bytes The size of a long integer: 4 bytes The size of a character: 1 bytes The size of a boolean: 1 bytes The size of a float: 4 bytes The size of a double float: 8 bytes The size of a long long int: 8 bytes
Compare this output to how it runs on your computer. The sizeof() function reveals the size of an object specified as its argument. For example, on line 16 the keyword float is passed to sizeof(). As you can see from the output, on the Ubuntu computer an int is equivalent in size to a long.