After receiving his Ph.D. in Applied Mathematics from Harvard University in 1980, Eric Roberts taught at Wellesley College from 1980-85, where he chaired the Computer Science Department. From 1985-90, he was a member of the research staff at Digital Equipment Corporation’s Systems Research Center in Palo Alto, California, where he conducted computer science research, focusing on programming tools for multiprocessor architectures. In September 1990, Roberts joined the Stanford faculty, where he is now Professor of Computer Science and the John A. and Cynthia Fry Gunn University Fellow in Undergraduate Education.
From 1990 to 2002, Professor Roberts was Associate Chair and Director of Undergraduate Studies for Computer Science. In that capacity, he was the principal architect of Stanford’s introductory programming sequence, which for many years held the distinction of being the largest course at Stanford. He has also written four computer science textbooks that are used at many colleges and universities throughout the world. His research focuses on computer science education, particularly for underserved communities. From 1998 to 2005, Roberts was Principal Investigator for the Bermuda Project, which developed the computer science curriculum for Bermuda’s public secondary schools.
While at Stanford, Professor Roberts has received several university- level teaching awards, including the Bing Fellowship, established “to recognize excellence in teaching and a committed interest to the teaching of undergraduates”; the Dinkelspiel Award, which recognizes “distinctive and exceptional contributions to undergraduate education”; and the Laurance and Naomi Carpenter Hoagland Prize, awarded for excellence in undergraduate teaching. In January 2002, Roberts was named one of the first eight University Fellows in Undergraduate Education, which are designed “to reward faculty who make truly outstanding contributions to Stanford’s undergraduate experience.”
Professor Roberts has been active in professional organizations dedicated to computer science education. From 2005 to 2007, he served as co-chair of the Education Board of the Association of Computing Machinery (ACM) and was for many years on the board of the ACM Special Interest Group on Computer Science Education (SIGCSE). From 1998 to 2001, Roberts served as co-chair and principal editor for the ACM/IEEE- CS Joint Task Force on Computing Curricula 2001, which published a detailed set of curriculum guidelines in December 2001. He also chaired the ACM Java Task Force from 2004 to 2006. In 2003, Roberts received the SIGCSE Award for Outstanding Contribution to Computer Science Education. Professor Roberts is a Fellow of the ACM and the American Association for the Advancement of Science (AAAS).
Professor Roberts has also been active in several organizations seeking to promote socially responsible use of science and technology. He is past president of both Computer Professionals for Social Responsibility, a public-interest organization of computer scientists and other professionals concerned about the impact of computer technology on society, and Student Pugwash USA, which encourages students to use their training in science and technology to create a better world. In 1999-2000, Roberts was the Eugene M. Lang Visiting Professor for Social Change at Swarthmore College.
Contents
1 Overview of C++ 1
1.1 Your first C++ program 2
1.2 The history of C++ 3
1.3 The structure of a C++ program 6
1.4 Variables 14
1.5 Data types 19
1.6 Expressions 26
1.7 Statements 36
Summary 47
Review questions 48
Exercises 50
2 Functions and Libraries 55
2.1 The idea of a function 56
2.2 Libraries 59
2.3 Defining functions in C++ 61
2.4 The mechanics of function calls 65
2.5 Reference parameters 73
2.6 Interfaces and implementations 78
2.7 Principles of interface design 85
2.8 Designing a random number library 90
2.9 Introduction to the Stanford libraries 107
Summary 112
Review questions 114
Exercises 115
3 Strings 125
3.1 Using strings as abstract values 126
3.2 String operations 129
3.3 The library 137
3.4 Modifying the contents of a string 138
3.5 The legacy of C-style strings 139
3.6 Writing string applications 140
3.7 The strlib.h library 146
Summary 147
Review questions 148
Exercises 149
4 Streams 159
4.1 Using strings as abstract values 160
4.2 Formatted input 165
4.3 Data files 167
4.4 Class hierarchies 181
4.5 The simpio.h and filelib.h libraries 186
Summary 188
Review questions 189
Exercises 190
5 Collections 195
5.1 The Vector class 197
5.2 The Stack class 211
5.3 The Queue class 217
5.4 The Map class 226
5.5 The Set class 232
5.6 Iterating over a collection 236
Summary 243
Review questions 245
Exercises 246
6 Designing Classes 261
6.1 Representing points 262
6.2 Operator overloading 268
6.3 Rational numbers 281
6.4 Designing a token scanner class 292
6.5 Encapsulating programs as classes 301
Summary 303
Review questions 305
Exercises 306
7 Introduction to Recursion 315
7.1 A simple example of recursion 316
7.2 The factorial function 318
7.3 The Fibonacci function 325
7.4 Checking palindromes 332
7.5 The binary search algorithm 335
7.6 Mutual recursion 336
7.7 Thinking recursively 338
Summary 340
Review questions 342
Exercises 344
8 Recursive Strategies 349
8.1 The Towers of Hanoi 350
8.2 The subset-sum problem 361
8.3 Generating permutations 364
8.4 Graphical recursion 368
Summary 375
Review questions 375
Exercises 376
9 Backtracking Algorithms 389
9.1 Recursive backtracking in a maze 390
9.2 Backtracking and games 400
9.3 The minimax algorithm 409
Summary 415
Review questions 416
Exercises 417
10 Algorithmic Analysis 429
10.1 The sorting problem 430
10.2 Computational complexity 435
10.3 Recursion to the rescue 443
10.4 Standard complexity classes 449
10.5 The Quicksort algorithm 452
10.6 Mathematical induction 458
Summary 462
Review questions 463
Exercises 466
11 Pointers and Arrays 473
11.1 The structure of memory 474
11.2 Pointers 484
11.3 Arrays 494
11.4 Pointer arithmetic 500
Summary 506
Review questions 508
Exercises 510
12 Dynamic Memory Management 515
12.1 Dynamic allocation and the heap 516
12.2 Linked lists 519
12.3 Freeing memory 523
12.4 Defining a CharStack class 527
12.5 Heap-stack diagrams 536
12.6 Unit testing 543
12.7 Copying objects 546
12.8 The uses of const 550
12.9 Efficiency of the CharStack class 558
Summary 560
Review questions 562
Exercises 564
13 Efficiency and Representation 569
13.1 Software patterns for editing text 570
13.2 Designing a simple text editor 572
13.3 An array-based implementation 579
13.4 A stack-based implementation 586
13.5 A list-based implementation 591
Summary 607
Review questions 608
Exercises 610
14 Linear Structures 615
14.1 Templates 616
14.2 Implementing stacks 619
14.3 Implementing queues 634
14.4 Implementing vectors 649
14.5 Integrating prototypes and code 656
Summary 657
Review questions 658
Exercises 659
15 Maps 663
15.1 Implementing maps using vectors 664
15.2 Lookup tables 668
15.3 Hashing 671
15.4 Implementing the HashMap class 682
Summary 683
Review questions 684
Exercises 685
16 Trees 689
16.1 Family trees 691
16.2 Binary search trees 693
16.3 Balanced trees 706
16.4 Implementing maps using BSTs 717
16.5 Partially ordered trees 719
Summary 722
Review questions 724
Exercises 727
17 Sets 737
17.1 Sets as a mathematical abstraction 738
17.2 Expanding the set interface 742
17.3 Implementation strategies for sets 747
17.4 Optimizing sets of small integers 753
Summary 761
Review questions 762
Exercises 764
18 Graphs 767
18.1 The structure of a graph 768
18.2 Representation strategies 772
18.3 A low-level graph abstraction 776
18.4 Graph traversals 783
18.5 Defining a Graph class 789
18.6 Finding shortest paths 804
18.7 Algorithms for searching the web 808
Summary 812
Review questions 813
Exercises 815
19 Inheritance 823
19.1 Simple inheritance 824
19.2 A hierarchy of graphical shapes 832
19.3 A class hierarchy for expressions 841
19.4 Parsing an expression 861
19.5 Multiple inheritance 870
Summary 873
Review questions 875
Exercises 877
20 Strategies for iteration 885
20.1 Using iterators 886
20.2 Using functions as data values 890
20.3 Encapsulating data with functions 899
20.4 The STL algorithms library 904
20.5 Functional programming in C++ 907
20.6 Implementing iterators 911
Summary 918
Review questions 920
Exercises 921
A Stanford library interfaces 927
Index 1025