426 91 5MB
English Pages [843] Year 2016
Programming Abstractions in C++ (Chapters 1–13) Eric S. Roberts This text represents a major revision of the course reader that we’ve been using at Stanford for the last several years. The primary goal of the revision was to bring the approach more closely in line with the way C++ is used in industry, which will in turn make it easier to export Stanford’s approach to teaching data structures to a larger fraction of schools. As of yet, the draft text is extremely rough and has not been copy-edited as the book will be. This installment contains only 13 out of the 19 chapters in the outline. The remaining chapters will be distributed as handouts during the quarter. This textbook has had an interesting evolutionary history that in some ways mirrors the genesis of the C++ language itself. Just as Bjarne Stroustup’s first version of C++ was implemented on top of a C language base, this reader began its life as Eric Roberts’s textbook Programming Abstractions in C (Addison-Wesley, 1998). In 2002-03, Julie Zelenski updated it for use with the C++ programming language, which we began using in CS106B and CS106X during that year. Although the revised text worked fairly well at the outset, CS106B and CS106X have evolved in recent years so that their structure no longer tracks the organization of the book. In 2008, I embarked on a comprehensive in the process of rewriting the book so that students in these courses can use it as both a tutorial and a reference. As always, that process takes a considerable amount of time, and there are certain to be some problems in the revision. At the same time, I’m convinced that the material in CS106B and CS106X is tremendously exciting and will be able to carry us through a quarter of instability, as we turn the reader into a book that can be used at universities and colleges around the world. I want to thank my colleagues at Stanford over the last several years, starting with Julie Zelenski for her extensive work on the initial C++ revision. My colleagues Keith Schwarz, Jerry Cain, Stephen Cooper, and Mehran Sahami have all made important contributions to the revision. And I also need to express my thanks to several generations of section leaders and so many students over the years, all of whom have helped make it so exciting to teach this wonderful material.
Chapter 1 An Overview of C++
Out of these various experiments come programs. This is our experience: programs do not come out of the minds of one person or two people such as ourselves, but out of day-to-day work. — Stokely Carmichael and Charles V. Hamilton, Black Power, 1967
2
Overview of C++
In Lewis Carroll’s Alice’s Adventures in Wonderland, the King asks the White Rabbit to “begin at the beginning and go on till you come to the end: then stop.” Good advice, but only if you’re starting from the beginning. This book is designed for a second course in computer science and therefore assumes that you have already begun your study of programming. At the same time, because first courses vary considerably in what they cover, it is difficult to rely on any specific material. Some of you, for example, will already understand C++ control structures from prior experience with closely related languages such as C or Java. For others, however, the structure of C++ will seem unfamiliar. Because of this disparity in background, the best approach is to adopt the King’s advice. This chapter therefore “begins at the beginning” and introduces you to those parts of the C++ language you will need to write simple programs
1.1 Your first C++ program As you will learn in more detail in the following section, C++ is an extension of an extremely successful programming language called C, which appeared in the early 1970s. In the book that serves as C’s defining document, The C Programming Language, Brian Kernighan and Dennis Ritchie offer the following advice on the first page of Chapter 1. The only way to learn a new programming language is by writing programs in it. The first program to write is the same for all languages: Print the words hello, world
This is the big hurdle; to leap over it you have to be able to create the program text somewhere, compile it successfully, load it, run it, and find out where the output went. With these mechanical details mastered, everything else is comparatively easy.
If you were to rewrite it in C++, the “Hello World” program would end up looking something like the code in Figure 1-1. At this point, the important thing is not to understand exactly what all of the lines in this program mean. There is plenty of time to master those details later. Your mission—and you should decide to accept it—is to get the HelloWorld program running. Type in the program exactly as it appears in Figure 1-1 and then figure out what you need to do to make it work. The exact steps you need to use depend on the programming environment you’re using to create and run C++ programs. If you are using this textbook for a class, your instructor will presumably provide some reference material on the programming environments you are
1.1 Your first C++ program
F I G U R E 1 - 1 The “Hello World” program /* * File: HelloWorld.cpp * -------------------* This file is adapted from the example * on page 1 of Kernighan and Ritchie's * book The C Programming Language. */ #include using namespace std; int main() { cout operators and have already had occasion to use the three standard streams exported by the library: cin, cout, and cerr. You have, however, only scratched the surface of what you can do even with the standard streams. To write C++ programs that move beyond the simple examples you have seen up to now, you will have to learn more about streams and how to use them to create more sophisticated applications. This chapter begins by giving you more insight into the features provided by the > operators. It then moves on to introduce the notion of data files and shows you how to implement file-processing applications. The chapter then concludes by exploring the structure of the C++ stream classes as a representative example of inheritance hierarchies in an object-oriented language.
4.1 Formatted output The easiest way to generate formatted output in C++ is to use the