
双语教学的效果,除了师资的水平和学生的接受能力之外,另一个重要的因素是双语教学使用的英文版教材。它应该是国外非常受欢迎的权威作者的经典教材,并能反映该学科的最新发展动态。基于这样的理念,我们课题组在大量的检索和查询的基础上,详细阅读了几本“面向对象程序设计”方面的英文版教材,最后选择了非常著名的美国加州大学圣地亚哥分校计算机科学系的Walter Savitch 教授编写的“Problem Solving with C++”一书。
这是一本非常受师生欢迎的书,在世界各地有数十万的读者。从编排上看,书中除了有基本的讲解外,还有总结框、编程提示、编程陷阱,各章都有自测题、小结和编程项目。有的章还有案例学习(Case Study)。从内容上看,除了有面向对象的基本知识,如I/O流、类、继承、多态,同时还包括比较新的高级主题,如异常处理、模板、标准模板库等。
但这本教材同时也用了大量篇幅讲解了C语言部分的基础知识。对于我国很多院校来说,“面向对象程序设计”是在C语言之后的一门课,因此该书有很多冗余之处。作为“面向对象程序设计”的教材,原书约有1000多页,显得非常厚重。这不仅会增加学生的经济负担,同时也会增加学生的心理压力。为此,我们把C语言部分进行了大量的删减,只留了复习、总结性的叙述。而面向对象部分全部保留,并对这部分做了前后一致化的处理。同时对原书匹配的教学辅助资料(PPT、源代码、自测题和编程题答案)也做了相应的改动。
该教材的改编是“教育部双语教学课程建设项目”的任务之一。它是由在国外学习工作多年、外语好或有多年“面向对象程序设计”双语教学经验的张志华、刘雪飞、秦奕青、施运梅、路旭强和佟俐鹃等老师分工协作、相互配合完成的。马力妮和刘旭红老师在选教材的过程中也做了相当多的工作。
我校教务处对选教材和改编工作给予了高度的重视和大力支持。清华大学出版社的龙启铭老师在该书的改编过程中,自始至终都给予了极大的帮助和热心的指导。在此一并表示衷心的感谢!
虽然我们课题组的全体成员经过反复的讨论、修改,最后定稿。但不尽人意之处仍在所难免,恳请批评指正。
双语教学的效果,除了师资的水平和学生的接受能力之外,另一个重要的因素是双语教学使用的英文版教材。它应该是国外非常受欢迎的权威作者的经典教材,并能反映该学科的最新发展动态。基于这样的理念,我们课题组在大量的检索和查询的基础上,详细阅读了几本“面向对象程序设计”方面的英文版教材,最后选择了非常著名的美国加州大学圣地亚哥分校计算机科学系的Walter Savitch 教授编写的“Problem Solving with C++”一书。
这是一本非常受师生欢迎的书,在世界各地有数十万的读者。从编排上看,书中除了有基本的讲解外,还有总结框、编程提示、编程陷阱,各章都有自测题、小结和编程项目。有的章还有案例学习(Case Study)。从内容上看,除了有面向对象的基本知识,如I/O流、类、继承、多态,同时还包括比较新的高级主题,如异常处理、模板、标准模板库等。
但这本教材同时也用了大量篇幅讲解了C语言部分的基础知识。对于我国很多院校来说,“面向对象程序设计”是在C语言之后的一门课,因此该书有很多冗余之处。作为“面向对象程序设计”的教材,原书约有1000多页,显得非常厚重。这不仅会增加学生的经济负担,同时也会增加学生的心理压力。为此,我们把C语言部分进行了大量的删减,只留了复习、总结性的叙述。而面向对象部分全部保留,并对这部分做了前后一致化的处理。同时对原书匹配的教学辅助资料(PPT、源代码、自测题和编程题答案)也做了相应的改动。
该教材的改编是“教育部双语教学课程建设项目”的任务之一。它是由在国外学习工作多年、外语好或有多年“面向对象程序设计”双语教学经验的张志华、刘雪飞、秦奕青、施运梅、路旭强和佟俐鹃等老师分工协作、相互配合完成的。马力妮和刘旭红老师在选教材的过程中也做了相当多的工作。
我校教务处对选教材和改编工作给予了高度的重视和大力支持。清华大学出版社的龙启铭老师在该书的改编过程中,自始至终都给予了极大的帮助和热心的指导。在此一并表示衷心的感谢!
虽然我们课题组的全体成员经过反复的讨论、修改,最后定稿。但不尽人意之处仍在所难免,恳请批评指正。
Chapter 1 Introduction to C++ 1
1.1 INTRODUCTION TO C++ 2
Origins of the C++ Language 2
C++ and Object-Oriented Programming 2
A Sample C++ Program 3
PROGRAMMING TIP: Input and Output Syntax 6
Layout of a Simple C++ Program 6
PITFALL: Putting a Space before the include File Name 8
Compiling and Running a C++ Program 8
PROGRAMMING TIP: Getting Your Program to Run 9
1.2 PROGRAM STYLE 10
Comments 11
Naming Constants 11
Chapter Summary 13
Programming Projects 13
chapter 2 C++ Basics 15
2.1 VARIABLES AND ASSIGNMENTS 16
Variables 16
Names: Identifiers 16
Variable Declarations 16
Assignment Statements 17
PITFALL: Uninitialized Variables 17
2.2 INPUT AND OUTPUT 17
Output Using cout 18
PROGRAMMING TIP: End Each Program with a \n or endl 19
Include Directives and Namespaces 20
Formatting for Numbers with a Decimal Point 21
Input Using cin 22
Designing Input and Output 23
PROGRAMMING TIP: Line Breaks in I/O 23
2.3 DATA TYPES AND EXPRESSIONS 24
Simple Types 24
Introduction to the Class string 25
Type Compatibilities 27
Arithmetic Operators and Expressions 28
2.4 FLOW OF CONTROL 28
Boolean Expressions 28
Branching Mechanisms 29
Loop Mechanisms 31
Chapter Summary 33
Programming Projects 33
chapter 3 Functions 35
3.1 THE BASICS OF FUNCTIONS 36
Using Predefined Functions 36
Type Casting 37
Programmer-Defined Functions 37
Namespaces Revisited 40
Case Study: Buying Pizza 42
3.2 CALL-BY-REFERENCE PARAMETERS 48
A View of Call-by-Value 48
A First View of Call-by-Reference 48
Call-by-Reference in Detail 50
PROGRAMMING TIP: What Kind of Parameter to Use 53
3.3 OVERLOADING FUNCTION NAMES 56
Introduction to Overloading 56
PROGRAMMING EXAMPLE: Revised Pizza-Buying Program 58
3.4 PROCEDURAL ABSTRACTION 62
The Black Box Analogy 62
Case Study: Supermarket Pricing 63
3.5 TESTING AND DEBUGGING FUNCTIONS 67
Stubs and Drivers 67
3.6 GENERAL DEBUGGING TECHNIQUES 72
Keep an Open Mind 72
Check Common Errors 73
Localize the Error 73
The assert Macro 75
Chapter Summary 77
Programming Projects 78
chapter 4 Arrays and Pointers 83
4.1 ARRAYS 84
Declaring and Referencing Arrays 84
Arrays in Memory 84
Initializing Arrays 85
4.2 ARRAYS IN FUNCTIONS 85
Indexed Variables as Function Arguments 85
Entire Arrays as Function Arguments 85
The const Parameter Modifier 86
Functions That Return an Array 86
4.3 MULTIDIMENSIONAL ARRAYS 86
Multidimensional Array Basics 86
Multidimensional Array Parameters 87
4.4 POINTERS 87
Pointer Variables 87
Basic Memory Management 92
PITFALL: Dangling Pointers 93
4.5 DYNAMIC ARRAYS 94
Array Variables and Pointer Variables 94
Creating and Using Dynamic Arrays 94
Chapter Summary 98
Programming Projects 99
chapter 5 I/O Streams as an Introduction to Objects and Classes 101
5.1 STREAMS AND BASIC FILE I/O 102
Why Use Files for I/O 103
File I/O 103
Introduction to Classes and Objects 107
PROGRAMMING TIP: check whether a File was opened successfully 108
Techniques for File I/O 110
Appending to a File (Optional) 114
File Names as Input (Optional) 114
5.2 TOOLS FOR STREAM I/O 117
Formatting Output with Stream Functions 117
Manipulators 121
Streams as Arguments to Functions 124
PROGRAMMING TIP: Checking for the End of a File 124
A Note on Namespaces 127
PROGRAMMING EXAMPLE: Cleaning Up a File Format 128
5.3 CHARACTER I/O 130
The Member Functions get and put 130
The putback Member Function (Optional) 133
PROGRAMMING EXAMPLE: Checking Input 134
PITFALL: Unexpected '\n' in Input 136
The eof Member Function 139
PROGRAMMING EXAMPLE: Editing a Text File 141
Predefined Character Functions 141
PITFALL: toupper and tolower Return Values 143
5.4 INHERITANCE 145
Inheritance Among Stream Classes 146
PROGRAMMING EXAMPLE: Another new_line Function 149
Default Arguments for Functions (Optional) 150
5.5 RANDOM ACCESS TO FILES (Optional) 152
Chapter Summary 154
Programming Projects 155
chapter 6 Strings and Vectors 163
6.1 AN ARRAY TYPE FOR STRINGS 164
C-String Values and C-String Variables 164
PITFALL: Using = and == with C Strings 165
Other Functions in <cstring> 165
C-String Input and Output 167
C-String-to-Number Conversions and Robust Input 169
6.2 THE STANDARD string CLASS 174
Introduction to the Standard Class string 174
I/O with the Class string 176
PROGRAMMING TIP: More Versions of getline 179
PITFALL: Mixing cin >> variable; and getline 180
String Processing with the Class string 181
PROGRAMMING EXAMPLE: Palindrome Testing 183
Converting Between string Objects and C Strings 188
6.3 VECTORS 188
Vector Basics 188
PITFALL: Using Square Brackets Beyond the Vector Size 190
PROGRAMMING TIP: Vector Assignment Is Well Behaved 192
Efficiency Issues 192
Chapter Summary 193
Programming Projects 194
chapter 7 Defining Classes 199
7.1 STRUCTURES 200
Structures for Diverse Data 200
PITFALL: Forgetting a Semicolon in a Structure Definition 204
Structures as Function Arguments 205
PROGRAMMING TIP: Use Hierarchical Structures 206
Initializing Structures 207
7.2 CLASSES 210
Defining Classes and Member Functions 210
Public and Private Members 214
PROGRAMMING TIP: Make All Member Variables Private 221
PROGRAMMING TIP: Define Accessor and Mutator Functions 221
PROGRAMMING TIP: Use the Assignment Operator with Objects 223
PROGRAMMING EXAMPLE: BankAccount Class-Version 1 223
Summary of Some Properties of Classes 227
Constructors for Initialization 229
PROGRAMMING TIP: Always Include a Default Constructor 235
PITFALL: Constructors with No Arguments 237
7.3 ABSTRACT DATA TYPES 238
Classes to Produce Abstract Data Types 239
PROGRAMMING EXAMPLE: Alternative Implementation of a Class 242
Chapter Summary 246
Programming Projects 247
chapter 8 Friends, Overloaded Operators, and Arrays in Classes 251
8.1 FRIEND FUNCTIONS 252
PROGRAMMING EXAMPLE: An Equality Function 252
Friend Functions 255
PROGRAMMING TIP: Define Both Accessor Functions
and Friend Functions 257
PROGRAMMING TIP: Use Both Member and Nonmember Functions 258
PROGRAMMING EXAMPLE: Money Class (Version 1) 259
Implementation of digit_to_int (Optional) 265
PITFALL: Leading Zeros in Number Constants 266
The const Parameter Modifier 268
PITFALL: Inconsistent Use of const 269
8.2 OVERLOADING OPERATORS 273
Overloading Operators 273
Constructors for Automatic Type Conversion 276
Overloading Unary Operators 278
Overloading >> and << 279
8.3 ARRAYS AND CLASSES 287
Arrays of Classes 288
Arrays as Class Members 291
PROGRAMMING EXAMPLE: A Class for a Partially Filled Array 292
8.4 CLASSES AND DYNAMIC ARRAYS 294
PROGRAMMING EXAMPLE: A String Variable Class 295
Destructors 298
PITFALL: Pointers as Call-by-Value Parameters 300
Copy Constructors 302
Overloading the Assignment Operator 306
Chapter Summary 309
Programming Projects 309
chapter 9 Separate Compilation and Namespaces 317
9.1 SEPARATE COMPILATION 318
ADTs Reviewed 318
Case Study: DigitalTime——A class Compiled Separately 320
Using #ifndef 328
PROGRAMMING TIP: Defining Other Libraries 330
9.2 NAMESPACES 332
Namespaces and using Directives 332
Creating a Namespace 333
Qualifying Names 336
A Subtle Point About Namespaces (Optional) 337
Unnamed Namespaces 338
PROGRAMMING TIP: Choosing a Name for a Namespace 342
PITFALL: Confusing the Global Namespace and the Unnamed Namespace 343
Chapter Summary 344
Programming Projects 345
chapter 10 Inheritance 347
10.1 INHERITANCE BASICS 348
Derived Classes 348
Constructors in Derived Classes 356
PITFALL: Use of Private Member Variables from the Base Class 359
PITFALL: Private Member Functions Are Effectively Not Inherited 360
The protected Qualifier 361
Redefinition of Member Functions 364
Redefining Versus Overloading 366
Access to a Redefined Base Function 367
10.2 INHERITANCE DETAILS 369
Functions That Are Not Inherited 369
Assignment Operators and Copy Constructors in Derived Classes 369
Destructors in Derived Classes 370
10.3 POLYMORPHISM 372
Late Binding 372
Virtual Functions in C++ 373
Virtual Functions and Extended Type Compatibility 378
PITFALL: The Slicing Problem 382
PITFALL: Not Using Virtual Member Functions 382
PITFALL: Attempting to Compile Class Definitions Without Definitions
for Every Virtual Member Function 383
PROGRAMMING TIP: Make Destructors Virtual 383
Chapter Summary 385
Programming Projects 385
chapter 11 Exception Handling 393
11.1 EXCEPTION-HANDLING BASICS 394
A Toy Example of Exception Handling 394
Defining Your Own Exception Classes 402
Multiple Throws and Catches 404
PITFALL: Catch the More Specific Exception First 406
PROGRAMMING TIP: Exception Classes Can Be Trivial 407
Throwing an Exception in a Function 408
Exception Specification 408
PITFALL: Exception Specification in Derived Classes 411
11.2 PROGRAMMING TECHNIQUES FOR EXCEPTION HANDLING 412
When to Throw an Exception 413
PITFALL: Uncaught Exceptions 414
PITFALL: Nested try-catch Blocks 414
PITFALL: Overuse of Exceptions 414
Exception Class Hierarchies 415
Testing for Available Memory 415
Rethrowing an Exception 416
Chapter Summary 416
Programming Projects 416
chapter 12 Templates 419
12.1 TEMPLATES FOR ALGORITHM ABSTRACTION 420
Templates for Functions 421
PITFALL: Compiler Complications 424
PROGRAMMING EXAMPLE: A Generic Sorting Function 426
PROGRAMMING TIP: How to Define Templates 430
PITFALL: Using a Template with an Inappropriate Type 430
12.2 TEMPLATES FOR DATA ABSTRACTION 430
Syntax for Class Templates 431
PROGRAMMING EXAMPLE: An Array Class 433
Chapter Summary 438
Programming Projects 438
chapter 13 Standard Template Library 441
13.1 ITERATORS 442
Using Declarations 443
Iterator Basics 443
Kinds of Iterators 448
Constant and Mutable Iterators 451
Reverse Iterators 453
PITFALL: Compiler Problems 454
Other Kinds of Iterators 455
13.2 CONTAINERS 455
Sequential Containers 456
PITFALL: Iterators and Removing Elements 459
PROGRAMMING TIP: Type Definitions in Containers 460
Container Adapters stack and queue 460
Associative Containers set and map 463
Efficiency 467
13.3 GENERIC ALGORITHMS 468
Running Times and Big-O Notation 469
Container Access Running Times 473
Nonmodifying Sequence Algorithms 473
Container Modifying Algorithms 478
Set Algorithms 479
Sorting Algorithms 481
Chapter Summary 481
Programming Projects 482
APPENDIX 1 C++ Keywords 487
APPENDIX 2 Precedence of Operators 488
APPENDIX 3 Some Library Functions 490
APPENDIX 4 Inline Functions 494
APPENDIX 5 Overloading the Array Index Square Brackets 495
APPENDIX 6 The this Pointer 496
APPENDIX 7 Overloading Operators as Member Operators 498
IV
Problem Solving with C++ 6th Edition
XI
Contents