• No results found

The programs in this chapter use only features of C++ that are completely standard. For that reason, these programs should work with any C++ compiler on any type of hardware. The standard libraries, however, do not include all the features that you would want as a programmer. Even though almost all modern applications use graphical displays, the standard libraries offer no graphical capabilities. Every modern operating system will provide libraries that support drawing pictures on the screen, but those libraries are different for each machine. In order to create applications that are as interesting as those you are used to using, you will certainly need to use nonstandard libraries somewhere along the way.

As part of the supporting material for this textbook, Stanford University provides a set of libraries that make learning to program in C++ both easier and more enjoyable. Those libraries include a sophisticated graphics package that works in exactly the same way on the various platforms that most people use. The Stanford libraries, however, provide several tools that it makes sense to use even at this point in your exploration of C++.

One library package that you might want to use for the exercises in this chapter is the console library, which creates a console window on the screen as soon as your program starts up. Having this window appear automatically solves the problem that Dennis Ritchie identified in The C Programming Language when he wrote that one of the harder challenges in running a program is to “find out where the output went.” To use this library, all you have to do is add the line

#include "console.h"

at the beginning of your program. If this line appears, the program creates the console. If it’s missing, the program works the way it always did.

The other Stanford library that you might want to begin using right away is the simpio.h library, which simplifies getting input from the user. Instead of the lines

int limit;

cout << "Enter exponent limit: "; cin >> limit;

using simpio.h allows you to collapse this idea into the following single line: int limit = getInteger("Enter exponent limit: ");

This library also includes a getReal function for reading a floating-point value and a getLine function for reading an entire line as a string. You will learn how to write these functions in Chapter 4, but you can use them now to get in the habit.

Summary

This chapter is itself a summary, which makes it hard to condense it to a few central points. Its purpose was to introduce you to the C++ programming language and give you a crash course in how to write simple programs in that language. This chapter focused on the low-level structure of the language, focusing on the concepts of expressions and statements, which together make it possible to define functions.

Important points in this chapter include:

• In the 30+ years of its existence, the C++ programming language has become one of the most widely used languages in the world.

• A typical C++ program consists of comments, library inclusions, program-level definitions, function prototypes, a function named main that is called when the program is started, and a set of auxiliary functions that work together with the main program to accomplish the required task.

• Variables in a C++ program must be declared before they are used. Most variables in C++ are local variables, which are declared within a function and can only be used inside the body of that function.

• A data type is defined by a domain of values and a set of operations. C++ includes several primitive types that allow programs to store common data values including integers, floating-point numbers, Booleans, and characters. As you will learn in later chapters, C++ also allows programmers to define new types from existing ones.

• The easiest way to perform input and output operations in C++ is to use the iostream library. This library defines three standard streams that refer to the console: cin for reading input data, cout for writing normal program output, and cerr for reporting error messages. Console input is traditionally performed using the >> operator, as in the statement

cin >> limit;

which reads a value from the console into the variable limit. Console output uses the << operator, as illustrated by the line

cout << "The answer is " << answer << endl;

which would display the value of answer on the console along with an identifying label. The endl value ensures that the next console output appears on a new line.

• Expressions in C++ are written in a form similar to that in most programming languages, with individual terms connected by operators. A list of the C++ operators appears in Table 1-4 along with their precedence and associativity.

• Statements in C++ fall into two general categories: simple statements and control statements. A simple statement consists of an expression—typically an assignment or a function call—followed by a semicolon. The control statements described in this chapter are the if, switch, while, and for statements. The first two are used to express conditional execution, while the last two are used to specify repetition.

• C++ programs are typically subdivided into several functions. You can use the examples in this chapter as models for writing your own functions, or you can wait until functions are covered in more detail in Chapter 2.

• The materials associated with this book include the Stanford C++ libraries which provide tools to make learning programming more fun and exciting. You will learn more about the features of the Stanford C++ libraries as you read this text. Although it is by no means necessary to use them, two libraries that might make your life easier are console.h and simpio.h.