C++ programming language, Study notes of C programming

useful book foe C++ programming language

Typology: Study notes

2017/2018

Uploaded on 01/28/2018

eman-al-shiekh
eman-al-shiekh 🇸🇦

1 document

1 / 47

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2b
pf2c
pf2d
pf2e
pf2f

Partial preview of the text

Download C++ programming language and more Study notes C programming in PDF only on Docsity!

800 East 96th Street, Indianapolis, Indiana, 46240 USA

Jesse Liberty

Rogers Cadenhead

Sams Teach Yourself

in

Hours

C++

Table of Contents

iv

vi

Table of Contents

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

This book is dedicated to Edythe, who provided life; Stacey, who shares it; and Robin

and Rachel, who give it purpose.

—Jesse Liberty

This book is dedicated to my dad, who’s currently teaching himself something a lot

harder than computer programming: how to walk again after spinal surgery. Through

the many months of rehab, you’ve been an inspiration. I’ve never known someone

with as much indefatigable determination to fix the hitch in his giddy-up.

—Rogers Cadenhead

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.

Who Should Read This Book?

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.

Should I Learn C First?

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:

. To help you distinguish C++ code from regular English, actual C++ code is type-

set in a special monospace font.

. Placeholders—words or characters temporarily used to represent the real words

or characters you would type in code—are typeset in italic monospace.

. New or important terms are typeset in italic.

. In the listings in this book, each real code line is numbered. If you see an

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.

Introduction

This page intentionally left blank

HOUR 3: Creating Variables and Constants

102 103 104

17

zombies

FIGURE 3.1 101 105 106 107

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”;

What Is a Variable? 31

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.

LISTING 3.1 The Full Text of Sizer.cpp

1: #include 2: 3: int main() 4: { 5: std::cout << “The size of an integer:\t\t”; 6: std::cout << sizeof(int) << “ bytes\n”; 7: std::cout << “The size of a short integer:\t”; 8: std::cout << sizeof(short) << “ bytes\n”; 9: std::cout << “The size of a long integer:\t”; 10: std::cout << sizeof(long) << “ bytes\n”; 11: std::cout << “The size of a character:\t”; 12: std::cout << sizeof(char) << “ bytes\n”; 13: std::cout << “The size of a boolean:\t\t”; 14: std::cout << sizeof(bool) << “ bytes\n”; 15: std::cout << “The size of a float:\t\t”; 16: std::cout << sizeof(float) << “ bytes\n”; 17: std::cout << “The size of a double float:\t”; 18: std::cout << sizeof(double) << “ bytes\n”; 19: std::cout << “The size of a long long int:\t”; 20: std::cout << sizeof(long long int) << “ bytes\n”; 21: 22: return 0; 23: }

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.