Schweitzer Fachinformationen
Wenn es um professionelles Wissen geht, ist Schweitzer Fachinformationen wegweisend. Kunden aus Recht und Beratung sowie Unternehmen, öffentliche Verwaltungen und Bibliotheken erhalten komplette Lösungen zum Beschaffen, Verwalten und Nutzen von digitalen und gedruckten Medien.
The quick and crystal-clear guide to C++ programming
C++ Essentials For Dummies is your useful reference to the key concepts of C++, the popular general-purpose language utilized everywhere from building games to writing parts of operating systems. With minimal review and background material-and absolutely no fluff-this book gets straight to the essential topics you need to know to ramp up, brush up, or level up.
Great for supplementing classroom learning, reviewing for a certification, or staying knowledgeable on the job, C++ Essentials For Dummies is a fantastic refresher guide that you can always turn to for answers.
John Paul Mueller produced more than 100 books and more than 600 articles on a range of topics, including functional programming techniques, application development using C++, and machine learning methodologies.
Ronald Mak teaches computer science and data science at San Jose State University. He was formerly a senior scientist at NASA and JPL, and has written books on software design, compiler construction, and numerical computing.
Introduction 1
Chapter 1: Compiling and Running Your First C++ Application 3
Chapter 2: Storing Data in C++ 9
Chapter 3: Directing the Application Flow 37
Chapter 4: Dividing Your Work with Functions 61
Chapter 5: Splitting Up Source Code Files 79
Chapter 6: Referring to Your Data through Pointers 95
Chapter 7: Working with Classes 117
Chapter 8: Working with Arrays and Pointers 151
Chapter 9: Ten Features for More Advanced C++ Programming 159
Index 171
Chapter 2
IN THIS CHAPTER
Using storage bins called variables
Working with integer and character variables
Manipulating strings
Using Boolean variables and conditional operators
Declaring and initializing a variable to hold a floating-point value
Telling cout how many digits to print after the decimal point
cout
Using enumerations to make your code easier to read
Reading from the console
The best way to think of memory is as a set of storage bins. When you write a computer application, you reserve some storage bins, and you give each storage bin a name. You also say what type of thing can be stored in the storage bin. The technical term for such a storage bin is a variable.
In this chapter, you discover how you can use these storage bins in your applications.
When you write an application, you specify that you want to make use of one or more storage bins called variables. Each computer storage bin can hold only one value at a time. A variable is simply a storage bin with an associated name.
You can put many different types of values into your variables. For example, you can put numbers in a storage bin, or you can put a string in a storage bin. (However, each storage bin contains a unique kind of data - you can't put a number into a storage bin designed for a string.) As for numbers, they can be either integers (which are positive whole numbers, negative whole numbers, and 0) or numbers with a decimal point, such as 3.11 or 10.0, which (for various reasons) are called floating-point numbers.
The term floating-point number refers to a number that has a decimal point and something to the right of the decimal point (even if it's just a 0). When you see the term floating point, you can remember what it means by focusing on the word point in its name - when you see point, think of decimal point.
In your C++ application, you can easily write a line of code that creates a variable. Although the variable doesn't actually get created until you run the application, people often refer to this process as creating a variable. A variable has three aspects:
count
LastName
10
a
You declare a variable when you specify its type and name. That is known as a variable declaration.
The code for the SimpleVariable example, shown here, demonstrates how to create a variable:
SimpleVariable
#include <iostream> using namespace std; int main() { int mynumber; mynumber = 10; cout << mynumber << endl; return 0; }
This is a full application that you can run.
Take a careful look at this code. Remember that the computer starts with the code inside the braces that follow main(), and it performs the code line by line.
main()
The first line inside main looks like this:
main
int mynumber;
When you declare a variable, the first thing you specify is the type of thing the variable can hold. Here, you use the word int. This word is the C++ word for integer. Thus, the variable that you're declaring can hold an integer. Next is the name of the variable. This variable is named mynumber. Then a semicolon ends the variable declaration.
int
mynumber
The next line looks like this:
mynumber = 10;
This line puts the value 10 in the variable. Because you already know that the variable can hold an integer, you're allowed to put in a 10 because it's an integer. If you'd tried to put a value other than an integer in the variable, the compiler would've given you an error message. The compiler makes sure that you put into a variable only the same type of value. And, of course, you noticed that the statement ends with a semicolon. In C++, every statement ends with a semicolon.
To put a value in a variable, you type the variable's name, an equal sign (surrounded by optional spaces), and the value. You end the line with a semicolon. This line of code is an assignment statement. Or you can say that you're setting the variable to the value.
The next line is this:
cout << mynumber << endl;
This is a cout statement, which means that it writes something on the console. This code tells the computer to write the value of mynumber on the console. The previous line of code put a 10 in the storage bin, so this line prints a 10 on the console. The endl means "end line", so the 10 is printed on a line by itself. When you run the application, you see this:
endl
Think of it like this: When you type the variable's name, you're accessing the variable. The exception to this is when the variable's name appears to the left of an equal sign. In that case, you're setting the variable. You can do two things with a variable:
When you retrieve the value that's in a variable, you aren't removing it from the variable. The value is still inside the variable.
If you want to declare three integer variables in a row, you can do it in a single declaration statement, like this:
int monica, rachel, phoebe;
This statement declares three separate variables. The first is called monica; the second is called rachel; and the third is called phoebe. Each of these three variables can have an integer value. You haven't put anything in any of them, so you may follow that with some code to assign each of them a value. For example, this code puts the value 10 in monica, 20 in rachel, and 3254 in phoebe.
monica
rachel
phoebe
20
3254
monica = 10; rachel = 20; phoebe = 3254;
When you run your applications, the computer executes the statements in the order in which they appear in your code. Therefore, in the preceding code, the computer first creates the three storage bins. Then it puts the value 10 inside monica. Next, rachel gets the value 20. And finally, phoebe gets the value 3254.
Although a variable can hold only one value at a time, you can still change what the variable holds. After you put another value in a variable, it forgets what it originally had.
You put another value in the variable in the same way you originally put a value in it. Look closely at the code for the ChangeVariable example:
ChangeVariable
#include <iostream> using namespace std; int main() { int mynumber; mynumber = 10; cout << mynumber << endl; mynumber = 20; cout << mynumber << endl; return 0; }
When you see a single equal sign by itself, the item on the left side is the variable item that receives the value that is on the right side.
Because you can do only two direct things with variables - put something in and retrieve the value - setting one variable equal to another is a simple process of retrieving the value of one variable and putting it in the other. This process is often referred to as copying the...
Dateiformat: ePUBKopierschutz: Adobe-DRM (Digital Rights Management)
Systemvoraussetzungen:
Das Dateiformat ePUB ist sehr gut für Romane und Sachbücher geeignet – also für „fließenden” Text ohne komplexes Layout. Bei E-Readern oder Smartphones passt sich der Zeilen- und Seitenumbruch automatisch den kleinen Displays an. Mit Adobe-DRM wird hier ein „harter” Kopierschutz verwendet. Wenn die notwendigen Voraussetzungen nicht vorliegen, können Sie das E-Book leider nicht öffnen. Daher müssen Sie bereits vor dem Download Ihre Lese-Hardware vorbereiten.Bitte beachten Sie: Wir empfehlen Ihnen unbedingt nach Installation der Lese-Software diese mit Ihrer persönlichen Adobe-ID zu autorisieren!
Weitere Informationen finden Sie in unserer E-Book Hilfe.