C++语言程序设计(基础篇)(英文版·原书第5版) / 经典原版书库
定价:¥149.00
作者: [美]梁勇(Y.Daniel Liang)
出版时间:2025-03-14
出版社:机械工业出版社
- 机械工业出版社
- 9787111774778
- 1-1
- 2025-03-14
- 1530
内容简介
本书是关于C++程序设计的经典教材,分为基础篇和进阶篇,主要介绍程序设计基础、面向对象程序设计和数据结构。本书采用“基础优先,问题驱动”的教学方式,在设计自定义类之前介绍基本的编程概念和技术,同时侧重于解决问题而非语法,通过不同领域的示例说明相关概念。本书引导读者循序渐进地学习,从基本的编程技术到面向对象编程,从简单的数据类型到经典的结构,为读者构建了友好的学习曲线。本书可作为高等院校计算机相关专业程序设计课程的教材,也可作为C++语言及编程爱好者的参考书。
目录
目 录
Introduction to C++ Programming and Data Structures, Fifth Edition
第1章 计算机、程序和C++概述 1
1.1 简介 1
1.2 什么是计算机 2
1.2.1 中央处理器 3
1.2.2 比特和字节 4
1.2.3 内存 5
1.2.4 存储设备 6
1.2.5 输入和输出设备 8
1.2.6 通信设备 9
1.3 程序设计语言 10
1.3.1 机器语言 10
1.3.2 汇编语言 10
1.3.3 高级语言 11
1.4 操作系统 15
1.4.1 控制和监视系统活动 16
1.4.2 分配和派发系统资源 16
1.4.3 调度操作 17
1.5 C++的历史 18
1.6 一个简单的C++程序 19
1.7 C++程序开发周期 26
1.8 程序设计风格和文档 29
1.8.1 适当的注释和注释风格 30
1.8.2 适当的缩进和间距 30
1.8.3 块样式 30
1.9 程序设计错误 31
1.9.1 语法错误 32
1.9.2 运行时错误 33
1.9.3 逻辑错误 34
1.9.4 常见错误 35
关键术语 37
章节总结 38
编程练习 39
第2章 程序设计初步 43
2.1 简介 44
2.2 编写一个简单程序 44
2.3 从键盘读取输入 48
2.4 标识符 52
2.5 变量 53
2.6 赋值语句和赋值表达式 56
2.7 命名常量 58
2.8 数值数据类型和操作 61
2.8.1 数值字面量 66
2.8.2 数值运算符 67
2.8.3 指数运算 72
2.9 计算表达式和运算符优先级 73
2.10 案例研究:显示当前时间 75
2.11 复合赋值运算符 78
2.12 递增和递减运算符 80
2.13 数值类型转换 84
2.14 软件开发过程 88
2.15 案例研究:计算货币单位 93
2.16 常见错误 97
关键术语 99
章节总结 100
编程练习 101
第3章 选择 114
3.1 简介 115
3.2 bool数据类型 115
3.3 if语句 118
3.4 双分支if-else语句 121
3.5 嵌套if和多分支if-else
语句 124
3.6 常见错误和陷阱 128
3.7 案例研究:计算体重指数 134
3.8 案例研究:计算税费 137
3.9 生成随机数 141
3.10 逻辑运算符 144
3.11 案例研究:确定闰年 151
3.12 案例研究:彩票 152
3.13 switch语句 155
3.14 条件运算符 160
3.15 运算符优先级和结合律 162
3.16 调试 164
关键术语 166
章节总结 166
编程练习 167
第4章 数学函数、字符和
字符串 183
4.1 简介 184
4.2 数学函数 184
4.2.1 三角函数 184
4.2.2 指数函数 186
4.2.3 取整函数 188
4.2.4 min、max和abs函数 189
4.2.5 案例研究:计算三角形的
角度 190
4.3 字符数据类型和运算 192
4.3.1 ASCII码 192
4.3.2 从键盘读取字符 194
4.3.3 特殊字符的转义序列 194
4.3.4 char型数据和数值型数据
之间的转换 196
4.3.5 字符的比较和检测 199
4.4 案例研究:生成随机字符 201
4.5 案例研究:猜生日 204
4.6 字符函数 206
4.7 案例研究:将十六进制数转换为
十进制数 209
4.8 string类型 212
4.8.1 字符串索引和下标运算符 214
4.8.2 连接字符串 215
4.8.3 比较字符串 216
4.8.4 读取字符串 217
4.9 案例研究:使用字符串修改彩票
程序 222
4.10 格式化控制台输出 224
4.10.1 setprecision(n)
操纵器 227
4.10.2 fixed操纵器 228
4.10.3 showpoint操纵器 229
4.10.4 setw(width)操纵器 229
4.10.5 left和right操纵器 231
4.11 简单文件输入和输出 235
4.11.1 写入文件 235
4.11.2 从文件中读取 237
关键术语 239
章节总结 240
编程练习 240
第5章 循环 252
5.1 简介 252
5.2 while循环 253
5.3 案例研究:猜数字 259
5.4 循环设计策略 264
5.5 使用用户确认或哨兵值控制
循环 266
5.6 输入和输出重定向以及从文件中
读取所有数据 270
5.7 do-while循环 272
5.8 for循环 277
5.9 使用哪个循环 282
5.10 嵌套循环 286
5.11 最小化数值误差 290
5.12 案例研究 291
5.12.1 案例研究:求最大公约数 291
5.12.2 案例研究:预测未来学费 294
5.12.3 案例研究:将十进制数
转换为十六进制数 296
5.13 关键字break和continue 299
5.14 案例研究:检查回文 306
5.15 案例研究:显示质数 309
关键术语 312
章节总结 313
编程练习 313
第6章 函数 332
6.1 简介 332
6.2 定义函数 334
6.3 调用函数 337
6.4 void函数 341
6.5 通过值传递参数 348
6.6 模块化代码 349
6.7 重载函数 355
6.8 函数原型 360
6.9 默认参数 362
6.10 内联函数 364
6.11 局部、全局和静态局部变量 367
6.11.1 for循环中的变量作用域 370
6.11.2 静态局部变量 371
6.12 通过引用传递参数 374
6.13 常量引用参数 386
6.14 案例研究:将十六进制数转换
为十进制数 386
6.15 函数抽象和逐步细化 390
6.15.1 自顶向下设计 392
6.15.2 自顶向下或自底向上实现 394
6.15.3 实现细节 394
6.15.4 逐步细化的好处 397
关键术语 398
章节总结 398
编程练习 400
第7章 一维数组和C字符串 419
7.1 简介 420
7.2 数组基础知识 420
7.2.1 声明数组 420
7.2.2 访问数组元素 422
7.2.3 数组初始化语句 424
7.2.4 处理数组 425
7.2.5 foreach循环 429
7.3 案例研究:分析数字 431
7.4 案例研究:一副牌 434
7.5 将数组传递给函数 437
7.6 防止函数中数组参数的更改 442
7.7 从函数返回数组 444
7.8 案例研究:统计每个字母的出现
次数 446
7.9 查找数组 449
7.9.1 线性查找法 450
7.9.2 二分查找法 451
7.10 数组排序 456
7.11 C字符串 460
7.11.1 C字符串的输入和输出 461
7.11.2 C字符串函数 462
7.11.3 用strcpy和strncpy
复制字符串 465
7.11.4 用strcat和strncat
连接字符串 466
7.11.5 比较字符串 467
7.11.6 字符串与C字符串相互
转换 469
7.11.7 将字符串转换为数字 469
7.12 将数字转换为字符串 470
关键术语 471
章节总结 471
编程练习 472
第8章 多维数组 492
8.1 简介 492
8.2 声明二维数组 493
8.3 处理二维数组 496
8.4 将二维数组传递给函数 499
8.5 案例研究:对选择题测验评分 502
8.6 案例研究:寻找最近点对 505
8.7 案例研究:数独 508
8.8 多维数组 513
8.8.1 案例研究:每日温度和
湿度 514
8.8.2 案例研究:猜测生日 516
关键术语 519
章节总结 519
编程练习 519
第9章 对象和类 540
9.1 简介 540
9.2 为对象定义类 541
9.3 示例:定义类和创建对象 543
9.4 构造函数 548
9.5 构造和使用对象 549
9.6 将类定义与实现分离 553
9.7 防止多重包含 557
9.8 类中的内联函数 559
9.9 数据字段封装 560
9.10 变量作用域 564
9.11 类抽象和封装 566
关键术语 573
章节总结 574
编程练习 575
第10章 面向对象思维 581
10.1 简介 581
10.2 string类 582
10.2.1 构造字符串 582
10.2.2 追加字符串 583
10.2.3 字符串赋值 584
10.2.4 函数at、clear、erase、empty、back和front 586
10.2.5 函数length、size、capacity和c_str() 587
10.2.6 比较字符串 588
10.2.7 获取子字符串 589
10.2.8 在字符串中查找 590
10.2.9 插入和替换字符串 591
10.2.10 字符串运算符 593
10.2.11 用stringstream将
数字转换为字符串 596
10.2.12 拆分字符串 596
10.2.13 案例研究:替换
字符串 597
10.3 将对象传递给函数 599
10.4 对象数组 603
10.5 实例成员和静态成员 605
10.6 常量成员函数 612
10.7 面向对象的思想 616
10.8 类关系 622
10.8.1 关联 622
10.8.2 聚合和组合 623
10.9 案例研究:
StackOfIntegers类 626
10.10 构造函数初始化列表 629
10.11 类设计指南 634
10.11.1 内聚性 635
10.11.2 一致性 635
10.11.3 封装性 635
10.11.4 清晰性 635
10.11.5 完整性 636
10.11.6 实例与静态 636
关键术语 637
章节总结 637
编程练习 638
第11章 指针与动态内存管理 646
11.1 简介 647
11.2 指针基础 647
11.3 使用typedef关键字定义
同义类型 655
11.4 将const与指针一起使用 656
11.5 数组和指针 658
11.6 在函数调用中传递指针参数 663
11.7 从函数返回指针 667
11.8 有用的数组函数 669
11.9 动态持久内存分配 672
11.10 创建和访问动态对象 678
11.11 this指针 680
11.12 析构函数 681
11.13 案例研究:Course类 686
11.14 复制构造函数 690
11.15 自定义复制构造函数 694
关键术语 698
章节总结 699
编程练习 700
第12章 模板、向量和栈 708
12.1 简介 708
12.2 模板基础知识 709
12.3 示例:泛型排序 714
12.4 类模板 717
12.5 改进Stack类 726
12.6 C++ vector类 729
12.7 向量的插入和删除及其他
函数 734
12.8 使用vector类替换数组 736
12.9 案例研究:计算表达式 741
12.10 使用智能指针自动销毁对象 744
关键术语 747
章节总结 747
编程练习 748
第13章 文件输入和输出 759
13.1 简介 760
13.2 文本I/O 760
13.2.1 将数据写入文件 760
13.2.2 从文件读取数据 763
13.2.3 检测文件是否存在 767
13.2.4 检测文件的结尾 767
13.2.5 用户输入文件名 770
13.3 格式化输出 771
13.4 函数:getline、get和
put 773
13.5 fstream和文件打开模式 777
13.6 测试流状态 780
13.7 二进制I/O 783
13.7.1 write函数 784
13.7.2 read函数 788
13.7.3 示例:二进制数组I/O 790
13.7.4 示例:二进制对象I/O 791
13.8 随机访问文件 796
13.9 更新文件 800
关键术语 802
章节总结 802
编程练习 803
第14章 运算符重载 808
14.1 简介 809
14.2 Rational类 810
14.3 运算符函数 817
14.4 重载下标运算符[] 821
14.5 重载复合赋值运算符 824
14.6 重载一元运算符 826
14.7 重载++和––运算符 827
14.8 友元函数和友元类 829
14.9 重载<<和>>运算符 832
14.10 自动类型转换 835
14.10.1 转换为基元数据类型 835
14.10.2 转换为对象类型 836
14.11 为重载运算符定义非成员
函数 837
14.12 带有重载函数运算符的
Rational类 838
14.13 重载=运算符 843
关键术语 850
章节总结 850
编程练习 851
第15章 继承与多态性 856
15.1 简介 857
15.2 基类和派生类 857
15.3 泛型编程 869
15.4 构造函数和析构函数 870
15.4.1 调用基类构造函数 870
15.4.2 构造函数链和析构函数链 871
15.5 重定义函数 878
15.6 多态性 880
15.7 虚拟函数和动态绑定 883
15.8 C++11 override和final
关键字 890
15.9 protected关键字 894
15.10 抽象类和纯虚拟函数 897
15.11 转换:静态转换与动态转换 908
关键术语 913
章节总结 914
编程练习 915
第16章 异常处理 918
16.1 简介 918
16.2 异常处理概述 919
16.3 异常类 929
16.4 自定义异常类 936
16.5 多次捕获 943
16.6 异常传播 949
16.7 重抛异常 950
16.8 何时使用异常 952
关键术语 953
章节总结 953
编程练习 954
附录A C++关键字 956
附录B ASCII字符集 958
附录C 运算符优先级表 960
附录D 数字系统 961
附录E 按位运算 968
附录F 使用命令行参数 971
附录G 枚举类型 975
附录H 正则表达式 980
附录I 大O、大Omega和
大Theta表示法 989
Contents
1: Introduction to Computers,
Programs, and C++ 1
1.1: Introduction 1
1.2: What Is a Computer? 2
1.2.1: Central Processing Unit 3
1.2.2: Bits and Bytes 4
1.2.3: Memory 5
1.2.4: Storage Devices 6
1.2.5: Input and Output Devices 8
1.2.6: Communication Devices 9
1.3: Programming Languages 10
1.3.1: Machine Language 10
1.3.2: Assembly Language 10
1.3.3: High-Level Language 11
1.4: Operating Systems 15
1.4.1: Controlling and Monitoring
System Activities 16
1.4.2: Allocating and Assigning System
Resources 16
1.4.3: Scheduling Operations 17
1.5: History of C++ 18
1.6: A Simple C++ Program 19
1.7: C++ Program-Development Cycle 26
1.8: Programming Style and
Documentation 29
1.8.1: Appropriate Comments and
Comment Styles 30
1.8.2: Proper Indentation and Spacing 30
1.8.3: Proper Indentation and Spacing 30
1.9: Programming Errors 31
1.9.1: Syntax Errors 32
1.9.2: Runtime Errors 33
1.9.3: Logic Errors 34
1.9.4: Common Errors 35
Key Terms 37
Chapter Summary 38
Programming Exercises 39
2: Elementary Programming 43
2.1: Introduction 44
2.2: Writing a Simple Program 44
2.3: Reading Input from the Keyboard 48
2.4: Identifiers 52
2.5: Variables 53
2.6: Assignment Statements and
Assignment Expressions 56
2.7: Named Constants 58
2.8: Numeric Data Types and Operations 61
2.8.1: Numeric Literals 66
2.8.2: Numeric Operators 67
2.8.3: Exponent Operators 72
2.9: Evaluating Expressions and Operator
Precedence 73
2.10: Case Study: Displaying the Current
Time 75
2.11: Augmented Assignment Operators 78
2.12: Increment and Decrement
Operators 80
2.13: Numeric Type Conversions 84
2.14: Software Development Process 88
2.15: Case Study: Counting Monetary
Units 93
2.16: Common Errors 97
Key Terms 99
Chapter Summary 100
Programming Exercises 101
3: Selections 114
3.1: Introduction 115
3.2: The bool Data Type 115
3.3: if Statements 118
3.4: Two-Way if-else Statements 121
3.5: Nested if and Multi-Way if-else
Statements 124
3.6: Common Errors and Pitfalls 128
3.7: Case Study: Computing Body Mass
Index 134
3.8: Case Study: Computing
Taxes 137
3.9: Generating Random Numbers 141
3.10: Logical Operators 144
3.11: Case Study: Determining Leap
Year 151
3.12: Case Study: Lottery 152
3.13: switch Statements 155
3.14: Conditional Operators 160
3.15: Operator Precedence and
Associativity 162
3.16: Debugging 164
Key Terms 166
Chapter Summary 166
Programming Exercises 167
4: Mathematical Functions,
Characters, and Strings 183
4.1: Introduction 184
4.2: Mathematical Functions 184
4.2.1: Trigonometric Functions 184
4.2.2: Exponent Functions 186
4.2.3: Rounding Functions 188
4.2.4: The min, max, and abs
Functions 189
4.2.5: Case Study: Computing Angles
of a Triangle 190
4.3: Character Data Type and
Operations 192
4.3.1: ASCII Code 192
4.3.2: Reading a Character from the
Keyboard 194
4.3.3: Escape Sequence for Special
Characters 194
4.3.4: Casting between char and
Numeric Types 196
4.3.5: Comparing and Testing
Characters 199
4.4: Case Study: Generating Random Characters 201
4.5: Case Study: Guessing Birthdays 204
4.6: Character Functions 206
4.7: Case Study: Converting a Hexadecimal
Digit to a Decimal Value 209
4.8: The string Type 212
4.8.1: String Index and Subscript
Operator 214
4.8.2: Concatenating Strings 215
4.8.3: Comparing Strings 216
4.8.4: Reading Strings 217
4.9: Case Study: Revising the Lottery
Program Using Strings 222
4.10: Formatting Console Output 224
4.10.1: The setprecision(n) Manipulator 227
4.10.2: The fixed Manipulator 228
4.10.3: The showpoint
Manipulator 229
4.10.4: The setw(width)
Manipulator 229
4.10.5: The left and right
Manipulators 231
4.11: Simple File Input and Output 235
4.11.1: Writing to a File 235
4.11.2: Reading from a File 237
Key Terms 239
Chapter Summary 240
Programming Exercises 240
5: Loops 252
5.1: Introduction 252
5.2: The while Loop 253
5.3: Case Study: Guessing Numbers 259
5.4: Loop Design Strategies 264
5.5: Controlling a Loop with User
Confirmation or a Sentinel Value 266
5.6: Input and Output Redirections and
Read All Data from a File 270
5.7: The do-while Loop 272
5.8: The for Loop 277
5.9: Which Loop to Use? 282
5.10: Nested Loops 286
5.11: Minimizing Numeric Errors 290
5.12: Case Studies 291
5.12.1: Case Study: Finding the Greatest
Common Divisor 291
5.12.2: Case Study: Predicting the Future
Tuition 294
5.12.3: Case Study: Converting Decimals
to Hexadecimals 296
5.13: Keywords break and
continue 299
5.14: Case Study: Checking
Palindromes 306
5.15: Case Study: Displaying Prime
Numbers 309
Key Terms 312
Chapter Summary 313
Programming Exercises 313
6: Functions 332
6.1: Introduction 332
6.2: Defining a Function 334
6.3: Calling a Function 337
6.4: void Functions 341
6.5: Passing Arguments by Value 348
6.6: Modularizing Code 349
6.7: Overloading Functions 355
6.8: Function Prototypes 360
6.9: Default Arguments 362
6.10: Inline Functions 364
6.11: Local, Global, and Static Local
Variables 367
6.11.1: The Scope of Variables in a for Loop 370
6.11.2: Static Local Variables 371
6.12: Passing Arguments by Reference 374
6.13: Constant Reference Parameters 386
6.14: Case Study: Converting
Hexadecimals to Decimals 386
6.15: Function Abstraction and Stepwise
Refinement 390
6.15.1: Top-Down Design 392
6.15.2: Top-Down or Bottom-Up
Implementation 394
6.15.3: Implementation Details 394
6.15.4: Benefits of Stepwise
Refinement 397
Key Terms 398
Chapter Summary 398
Programming Exercises 400
7: Single-Dimensional Arrays
and C-Strings 419
7.1: Introduction 420
7.2: Array Basics 420
7.2.1: Declaring Arrays 420
7.2.2: Accessing Array Elements 422
7.2.3: Array Initializers 424
7.2.4: Processing Arrays 425
7.2.5: Foreach Loops 429
7.3: Case Study: Analyzing Numbers 431
7.4: Case Study: Deck of Cards 434
7.5: Passing Arrays to Functions 437
7.6: Preventing Changes of Array
Arguments in Functions 442
7.7: Returning Arrays from Functions 444
7.8: Case Study: Counting the Occurrences
of Each Letter 446
7.9: Searching Arrays 449
7.9.1: The Linear Search Approach 450
7.9.2: The Binary Search Approach 451
7.10: Sorting Arrays 456
7.11: C-Strings 460
7.11.1: Input and Output of
C-Strings 461
7.11.2: C-String Functions 462
7.11.3: Copying Strings Using strcpy
and strncpy 465
7.11.4: Concatenating Strings Using
strcat and strncat 466
7.11.5: Comparing Strings 467
7.11.6: Converting Strings to
C-Strings 469
7.11.7: Converting Strings to
Numbers 469
7.12: Converting Numbers to Strings 470
Key Terms 471
Chapter Summary 471
Programming Exercises 472
8: Multidimensional Arrays 492
8.1: Introduction 492
8.2: Declaring Two-Dimensional
Arrays 493
8.3: Processing Two-Dimensional
Arrays 496
8.4: Passing Two-Dimensional Arrays to
Functions 499
8.5: Case Study: Grading a Multiple-
Choice Test 502
8.6: Case Study: Finding a Closest Pair 505
8.7: Case Study: Sudoku 508
8.8: Multidimensional Arrays 513
8.8.1: Case Study: Daily Temperature
and Humidity 514
8.8.2: Case Study: Guessing
Birthdays 516
Key Terms 519
Chapter Summary 519
Programming Exercises 519
9: Objects and Classes 540
9.1: Introduction 540
9.2: Defining Classes for Objects 541
9.3: Example: Defining Classes and
Creating Objects 543
9.4: Constructors 548
9.5: Constructing and Using Objects 549
9.6: Separating Class Definition from
Implementation 553
9.7: Preventing Multiple Inclusions 557
9.8: Inline Functions in Classes 559
9.9: Data Field Encapsulation 560
9.10: The Scope of Variables 564
9.11: Class Abstraction and
Encapsulation 566
Key Terms 573
Chapter Summary 574
Programming Exercises 575
10: Object-Oriented Thinking 581
10.1: Introduction 581
10.2: The string Class 582
10.2.1: Constructing a String 582
10.2.2: Appending to a String 583
10.2.3: Assigning a String 584
10.2.4: Functions at, clear, erase, empty, back, and front 586
10.2.5: Functions length, size, capacity, and c_str() 587
10.2.6: Comparing Strings 588
10.2.7: Obtaining Substrings 589
10.2.8: Seaching in a String 590
10.2.9: Inserting and Replacing
Strings 591
10.2.10: String Operators 593
10.2.11: Converting Numbers to Strings
Using stringstream 596
10.2.12: Splitting Strings 596
10.2.13: Case Study: Replacing
Strings 597
10.3: Passing Objects to Functions 599
10.4: Array of Objects 603
10.5: Instance and Static Members 605
10.6: Constant Member Functions 612
10.7: Thinking in Objects 616
10.8: Class Relationships 622
10.8.1: Association 622
10.8.2: Aggregation and Composition 623
10.9: Case Study: The StackOfIntegers
Class 626
10.10: Constructor Initializer Lists 629
10.11: Class Design Guidelines 634
10.11.1: Cohesion 635
10.11.2: Consistency 635
10.11.3: Encapsulation 635
10.11.4: Clarity 635
10.11.5: Completeness 636
10.11.6: Instance vs. Static 636
Key Terms 637
Chapter Summary 637
Programming Exercises 638
11: Pointers and Dynamic
Memory Management 646
11.1: Introduction 647
11.2: Pointer Basics 647
11.3: Defining Synonymous Types Using
the typedef Keyword 655
11.4: Using const with Pointers 656
11.5: Arrays and Pointers 658
11.6: Passing Pointer Arguments in a
Function Call 663
11.7: Returning a Pointer from
Functions 667
11.8: Useful Array Functions 669
11.9: Dynamic Persistent Memory
Allocation 672
11.10: Creating and Accessing Dynamic
Objects 678
11.11: The this Pointer 680
11.12: Destructors 681
11.13: Case Study: The Course Class 686
11.14: Copy Constructors 690
11.15: Customizing Copy Constructors 694
Key Terms 698
Chapter Summary 699
Programming Exercises 700
12: Templates, Vectors, and
Stacks 708
12.1: Introduction 708
12.2: Templates Basics 709
12.3: Example: A Generic Sort 714
12.4: Class Templates 717
12.5: Improving the Stack Class 726
12.6: The C++ vector Class 729
12.7: Insertion and Deletion and Other
Functions for a Vector 734
12.8: Replacing Arrays Using the vector
Class 736
12.9: Case Study: Evaluating
Expressions 741
12.10: Using Smart Pointers for Automatic
Object Destruction 744
Key Terms 747
Chapter Summary 747
Programming Exercises 748
13: File Input and Output 759
13.1: Introduction 760
13.2: Text I/O 760
13.2.1: Writing Data to a File 760
13.2.2: Reading Data from a File 763
13.2.3: Testing File Existence 767
13.2.4: Testing End of File 767
13.2.5: Letting the User Enter a
Filename 770
13.3: Formatting Output 771
13.4: Functions: getline, get, and
put 773
13.5: fstream and File Open Modes 777
13.6: Testing Stream States 780
13.7: Binary I/O 783
13.7.1: The write Function 784
13.7.2: The read Function 788
13.7.3: Example: Binary Array I/O 790
13.7.4: Example: Binary Object I/O 791
13.8: Random Access File 796
13.9: Updating Files 800
Key Terms 802
Chapter Summary 802
Programming Exercises 803
14: Operator Overloading 808
14.1: Introduction 809
14.2: The Rational Class 810
14.3: Operator Functions 817
14.4: Overloading the Subscript
Operator [] 821
14.5: Overloading Augmented
Assignment Operators 824
14.6: Overloading the Unary
Operators 826
14.7: Overloading the ++ and --
Operators 827
14.8: friend Functions and friend
Classes 829
14.9: Overloading the << and >>
Operators 832
14.10: Automatic Type Conversions 835
14.10.1: Converting to a Primitive Data Type 835
14.10.2: Converting to an Object
Type 836
14.11: Defining Nonmember Functions
for Overloading Operators 837
14.12: The Rational Class with
Overloaded Function Operators 838
14.13: Overloading the = Operators 843
Key Terms 850
Chapter Summary 850
Programming Exercises 851
15: Inheritance and
Polymorphism 856
15.1: Introduction 857
15.2: Base Classes and Derived Classes 857
15.3: Generic Programming 869
15.4: Constructors and Destructors 870
15.4.1: Calling Base Class
Constructors 870
15.4.2: Constructor and Destructor
Chaining 871
15.5: Redefining Functions 878
15.6: Polymorphism 880
15.7: Virtual Functions and Dynamic
Binding 883
15.8: The C++11 override and final
Keywords 890
15.9: The protected Keyword 894
15.10: Abstract Classes and Pure Virtual
Functions 897
15.11: Casting: static_cast versus
dynamic_cast 908
Key Terms 913
Chapter Summary 914
Programming Exercises 915
16: Exception Handling 918
16.1: Introduction 918
16.2: Exception-Handling Overview 919
16.3: Exception Classes 929
16.4: Custom Exception Classes 936
16.5: Multiple Catches 943
16.6: Exception Propagation 949
16.7: Rethrowing Exceptions 950
16.8: When to Use Exceptions 952
Key Terms 953
Chapter Summary 953
Programming Exercises 954
Appendix A: C++ Keywords 956
Appendix B: The ASCII Character
Set 958
Appendix C: Operator Precedence
Chart 960
Appendix D: Number Systems 961
Appendix E: Bitwise
Operations 968
Appendix F: Using Command-
Line Arguments 971
Appendix G: Enumerated
Types 975
Appendix H: Regular
Expressions 980
Appendix I: The Big-O,
Big-Omega,
and Big-Theta
Notations 989
Introduction to C++ Programming and Data Structures, Fifth Edition
第1章 计算机、程序和C++概述 1
1.1 简介 1
1.2 什么是计算机 2
1.2.1 中央处理器 3
1.2.2 比特和字节 4
1.2.3 内存 5
1.2.4 存储设备 6
1.2.5 输入和输出设备 8
1.2.6 通信设备 9
1.3 程序设计语言 10
1.3.1 机器语言 10
1.3.2 汇编语言 10
1.3.3 高级语言 11
1.4 操作系统 15
1.4.1 控制和监视系统活动 16
1.4.2 分配和派发系统资源 16
1.4.3 调度操作 17
1.5 C++的历史 18
1.6 一个简单的C++程序 19
1.7 C++程序开发周期 26
1.8 程序设计风格和文档 29
1.8.1 适当的注释和注释风格 30
1.8.2 适当的缩进和间距 30
1.8.3 块样式 30
1.9 程序设计错误 31
1.9.1 语法错误 32
1.9.2 运行时错误 33
1.9.3 逻辑错误 34
1.9.4 常见错误 35
关键术语 37
章节总结 38
编程练习 39
第2章 程序设计初步 43
2.1 简介 44
2.2 编写一个简单程序 44
2.3 从键盘读取输入 48
2.4 标识符 52
2.5 变量 53
2.6 赋值语句和赋值表达式 56
2.7 命名常量 58
2.8 数值数据类型和操作 61
2.8.1 数值字面量 66
2.8.2 数值运算符 67
2.8.3 指数运算 72
2.9 计算表达式和运算符优先级 73
2.10 案例研究:显示当前时间 75
2.11 复合赋值运算符 78
2.12 递增和递减运算符 80
2.13 数值类型转换 84
2.14 软件开发过程 88
2.15 案例研究:计算货币单位 93
2.16 常见错误 97
关键术语 99
章节总结 100
编程练习 101
第3章 选择 114
3.1 简介 115
3.2 bool数据类型 115
3.3 if语句 118
3.4 双分支if-else语句 121
3.5 嵌套if和多分支if-else
语句 124
3.6 常见错误和陷阱 128
3.7 案例研究:计算体重指数 134
3.8 案例研究:计算税费 137
3.9 生成随机数 141
3.10 逻辑运算符 144
3.11 案例研究:确定闰年 151
3.12 案例研究:彩票 152
3.13 switch语句 155
3.14 条件运算符 160
3.15 运算符优先级和结合律 162
3.16 调试 164
关键术语 166
章节总结 166
编程练习 167
第4章 数学函数、字符和
字符串 183
4.1 简介 184
4.2 数学函数 184
4.2.1 三角函数 184
4.2.2 指数函数 186
4.2.3 取整函数 188
4.2.4 min、max和abs函数 189
4.2.5 案例研究:计算三角形的
角度 190
4.3 字符数据类型和运算 192
4.3.1 ASCII码 192
4.3.2 从键盘读取字符 194
4.3.3 特殊字符的转义序列 194
4.3.4 char型数据和数值型数据
之间的转换 196
4.3.5 字符的比较和检测 199
4.4 案例研究:生成随机字符 201
4.5 案例研究:猜生日 204
4.6 字符函数 206
4.7 案例研究:将十六进制数转换为
十进制数 209
4.8 string类型 212
4.8.1 字符串索引和下标运算符 214
4.8.2 连接字符串 215
4.8.3 比较字符串 216
4.8.4 读取字符串 217
4.9 案例研究:使用字符串修改彩票
程序 222
4.10 格式化控制台输出 224
4.10.1 setprecision(n)
操纵器 227
4.10.2 fixed操纵器 228
4.10.3 showpoint操纵器 229
4.10.4 setw(width)操纵器 229
4.10.5 left和right操纵器 231
4.11 简单文件输入和输出 235
4.11.1 写入文件 235
4.11.2 从文件中读取 237
关键术语 239
章节总结 240
编程练习 240
第5章 循环 252
5.1 简介 252
5.2 while循环 253
5.3 案例研究:猜数字 259
5.4 循环设计策略 264
5.5 使用用户确认或哨兵值控制
循环 266
5.6 输入和输出重定向以及从文件中
读取所有数据 270
5.7 do-while循环 272
5.8 for循环 277
5.9 使用哪个循环 282
5.10 嵌套循环 286
5.11 最小化数值误差 290
5.12 案例研究 291
5.12.1 案例研究:求最大公约数 291
5.12.2 案例研究:预测未来学费 294
5.12.3 案例研究:将十进制数
转换为十六进制数 296
5.13 关键字break和continue 299
5.14 案例研究:检查回文 306
5.15 案例研究:显示质数 309
关键术语 312
章节总结 313
编程练习 313
第6章 函数 332
6.1 简介 332
6.2 定义函数 334
6.3 调用函数 337
6.4 void函数 341
6.5 通过值传递参数 348
6.6 模块化代码 349
6.7 重载函数 355
6.8 函数原型 360
6.9 默认参数 362
6.10 内联函数 364
6.11 局部、全局和静态局部变量 367
6.11.1 for循环中的变量作用域 370
6.11.2 静态局部变量 371
6.12 通过引用传递参数 374
6.13 常量引用参数 386
6.14 案例研究:将十六进制数转换
为十进制数 386
6.15 函数抽象和逐步细化 390
6.15.1 自顶向下设计 392
6.15.2 自顶向下或自底向上实现 394
6.15.3 实现细节 394
6.15.4 逐步细化的好处 397
关键术语 398
章节总结 398
编程练习 400
第7章 一维数组和C字符串 419
7.1 简介 420
7.2 数组基础知识 420
7.2.1 声明数组 420
7.2.2 访问数组元素 422
7.2.3 数组初始化语句 424
7.2.4 处理数组 425
7.2.5 foreach循环 429
7.3 案例研究:分析数字 431
7.4 案例研究:一副牌 434
7.5 将数组传递给函数 437
7.6 防止函数中数组参数的更改 442
7.7 从函数返回数组 444
7.8 案例研究:统计每个字母的出现
次数 446
7.9 查找数组 449
7.9.1 线性查找法 450
7.9.2 二分查找法 451
7.10 数组排序 456
7.11 C字符串 460
7.11.1 C字符串的输入和输出 461
7.11.2 C字符串函数 462
7.11.3 用strcpy和strncpy
复制字符串 465
7.11.4 用strcat和strncat
连接字符串 466
7.11.5 比较字符串 467
7.11.6 字符串与C字符串相互
转换 469
7.11.7 将字符串转换为数字 469
7.12 将数字转换为字符串 470
关键术语 471
章节总结 471
编程练习 472
第8章 多维数组 492
8.1 简介 492
8.2 声明二维数组 493
8.3 处理二维数组 496
8.4 将二维数组传递给函数 499
8.5 案例研究:对选择题测验评分 502
8.6 案例研究:寻找最近点对 505
8.7 案例研究:数独 508
8.8 多维数组 513
8.8.1 案例研究:每日温度和
湿度 514
8.8.2 案例研究:猜测生日 516
关键术语 519
章节总结 519
编程练习 519
第9章 对象和类 540
9.1 简介 540
9.2 为对象定义类 541
9.3 示例:定义类和创建对象 543
9.4 构造函数 548
9.5 构造和使用对象 549
9.6 将类定义与实现分离 553
9.7 防止多重包含 557
9.8 类中的内联函数 559
9.9 数据字段封装 560
9.10 变量作用域 564
9.11 类抽象和封装 566
关键术语 573
章节总结 574
编程练习 575
第10章 面向对象思维 581
10.1 简介 581
10.2 string类 582
10.2.1 构造字符串 582
10.2.2 追加字符串 583
10.2.3 字符串赋值 584
10.2.4 函数at、clear、erase、empty、back和front 586
10.2.5 函数length、size、capacity和c_str() 587
10.2.6 比较字符串 588
10.2.7 获取子字符串 589
10.2.8 在字符串中查找 590
10.2.9 插入和替换字符串 591
10.2.10 字符串运算符 593
10.2.11 用stringstream将
数字转换为字符串 596
10.2.12 拆分字符串 596
10.2.13 案例研究:替换
字符串 597
10.3 将对象传递给函数 599
10.4 对象数组 603
10.5 实例成员和静态成员 605
10.6 常量成员函数 612
10.7 面向对象的思想 616
10.8 类关系 622
10.8.1 关联 622
10.8.2 聚合和组合 623
10.9 案例研究:
StackOfIntegers类 626
10.10 构造函数初始化列表 629
10.11 类设计指南 634
10.11.1 内聚性 635
10.11.2 一致性 635
10.11.3 封装性 635
10.11.4 清晰性 635
10.11.5 完整性 636
10.11.6 实例与静态 636
关键术语 637
章节总结 637
编程练习 638
第11章 指针与动态内存管理 646
11.1 简介 647
11.2 指针基础 647
11.3 使用typedef关键字定义
同义类型 655
11.4 将const与指针一起使用 656
11.5 数组和指针 658
11.6 在函数调用中传递指针参数 663
11.7 从函数返回指针 667
11.8 有用的数组函数 669
11.9 动态持久内存分配 672
11.10 创建和访问动态对象 678
11.11 this指针 680
11.12 析构函数 681
11.13 案例研究:Course类 686
11.14 复制构造函数 690
11.15 自定义复制构造函数 694
关键术语 698
章节总结 699
编程练习 700
第12章 模板、向量和栈 708
12.1 简介 708
12.2 模板基础知识 709
12.3 示例:泛型排序 714
12.4 类模板 717
12.5 改进Stack类 726
12.6 C++ vector类 729
12.7 向量的插入和删除及其他
函数 734
12.8 使用vector类替换数组 736
12.9 案例研究:计算表达式 741
12.10 使用智能指针自动销毁对象 744
关键术语 747
章节总结 747
编程练习 748
第13章 文件输入和输出 759
13.1 简介 760
13.2 文本I/O 760
13.2.1 将数据写入文件 760
13.2.2 从文件读取数据 763
13.2.3 检测文件是否存在 767
13.2.4 检测文件的结尾 767
13.2.5 用户输入文件名 770
13.3 格式化输出 771
13.4 函数:getline、get和
put 773
13.5 fstream和文件打开模式 777
13.6 测试流状态 780
13.7 二进制I/O 783
13.7.1 write函数 784
13.7.2 read函数 788
13.7.3 示例:二进制数组I/O 790
13.7.4 示例:二进制对象I/O 791
13.8 随机访问文件 796
13.9 更新文件 800
关键术语 802
章节总结 802
编程练习 803
第14章 运算符重载 808
14.1 简介 809
14.2 Rational类 810
14.3 运算符函数 817
14.4 重载下标运算符[] 821
14.5 重载复合赋值运算符 824
14.6 重载一元运算符 826
14.7 重载++和––运算符 827
14.8 友元函数和友元类 829
14.9 重载<<和>>运算符 832
14.10 自动类型转换 835
14.10.1 转换为基元数据类型 835
14.10.2 转换为对象类型 836
14.11 为重载运算符定义非成员
函数 837
14.12 带有重载函数运算符的
Rational类 838
14.13 重载=运算符 843
关键术语 850
章节总结 850
编程练习 851
第15章 继承与多态性 856
15.1 简介 857
15.2 基类和派生类 857
15.3 泛型编程 869
15.4 构造函数和析构函数 870
15.4.1 调用基类构造函数 870
15.4.2 构造函数链和析构函数链 871
15.5 重定义函数 878
15.6 多态性 880
15.7 虚拟函数和动态绑定 883
15.8 C++11 override和final
关键字 890
15.9 protected关键字 894
15.10 抽象类和纯虚拟函数 897
15.11 转换:静态转换与动态转换 908
关键术语 913
章节总结 914
编程练习 915
第16章 异常处理 918
16.1 简介 918
16.2 异常处理概述 919
16.3 异常类 929
16.4 自定义异常类 936
16.5 多次捕获 943
16.6 异常传播 949
16.7 重抛异常 950
16.8 何时使用异常 952
关键术语 953
章节总结 953
编程练习 954
附录A C++关键字 956
附录B ASCII字符集 958
附录C 运算符优先级表 960
附录D 数字系统 961
附录E 按位运算 968
附录F 使用命令行参数 971
附录G 枚举类型 975
附录H 正则表达式 980
附录I 大O、大Omega和
大Theta表示法 989
Contents
1: Introduction to Computers,
Programs, and C++ 1
1.1: Introduction 1
1.2: What Is a Computer? 2
1.2.1: Central Processing Unit 3
1.2.2: Bits and Bytes 4
1.2.3: Memory 5
1.2.4: Storage Devices 6
1.2.5: Input and Output Devices 8
1.2.6: Communication Devices 9
1.3: Programming Languages 10
1.3.1: Machine Language 10
1.3.2: Assembly Language 10
1.3.3: High-Level Language 11
1.4: Operating Systems 15
1.4.1: Controlling and Monitoring
System Activities 16
1.4.2: Allocating and Assigning System
Resources 16
1.4.3: Scheduling Operations 17
1.5: History of C++ 18
1.6: A Simple C++ Program 19
1.7: C++ Program-Development Cycle 26
1.8: Programming Style and
Documentation 29
1.8.1: Appropriate Comments and
Comment Styles 30
1.8.2: Proper Indentation and Spacing 30
1.8.3: Proper Indentation and Spacing 30
1.9: Programming Errors 31
1.9.1: Syntax Errors 32
1.9.2: Runtime Errors 33
1.9.3: Logic Errors 34
1.9.4: Common Errors 35
Key Terms 37
Chapter Summary 38
Programming Exercises 39
2: Elementary Programming 43
2.1: Introduction 44
2.2: Writing a Simple Program 44
2.3: Reading Input from the Keyboard 48
2.4: Identifiers 52
2.5: Variables 53
2.6: Assignment Statements and
Assignment Expressions 56
2.7: Named Constants 58
2.8: Numeric Data Types and Operations 61
2.8.1: Numeric Literals 66
2.8.2: Numeric Operators 67
2.8.3: Exponent Operators 72
2.9: Evaluating Expressions and Operator
Precedence 73
2.10: Case Study: Displaying the Current
Time 75
2.11: Augmented Assignment Operators 78
2.12: Increment and Decrement
Operators 80
2.13: Numeric Type Conversions 84
2.14: Software Development Process 88
2.15: Case Study: Counting Monetary
Units 93
2.16: Common Errors 97
Key Terms 99
Chapter Summary 100
Programming Exercises 101
3: Selections 114
3.1: Introduction 115
3.2: The bool Data Type 115
3.3: if Statements 118
3.4: Two-Way if-else Statements 121
3.5: Nested if and Multi-Way if-else
Statements 124
3.6: Common Errors and Pitfalls 128
3.7: Case Study: Computing Body Mass
Index 134
3.8: Case Study: Computing
Taxes 137
3.9: Generating Random Numbers 141
3.10: Logical Operators 144
3.11: Case Study: Determining Leap
Year 151
3.12: Case Study: Lottery 152
3.13: switch Statements 155
3.14: Conditional Operators 160
3.15: Operator Precedence and
Associativity 162
3.16: Debugging 164
Key Terms 166
Chapter Summary 166
Programming Exercises 167
4: Mathematical Functions,
Characters, and Strings 183
4.1: Introduction 184
4.2: Mathematical Functions 184
4.2.1: Trigonometric Functions 184
4.2.2: Exponent Functions 186
4.2.3: Rounding Functions 188
4.2.4: The min, max, and abs
Functions 189
4.2.5: Case Study: Computing Angles
of a Triangle 190
4.3: Character Data Type and
Operations 192
4.3.1: ASCII Code 192
4.3.2: Reading a Character from the
Keyboard 194
4.3.3: Escape Sequence for Special
Characters 194
4.3.4: Casting between char and
Numeric Types 196
4.3.5: Comparing and Testing
Characters 199
4.4: Case Study: Generating Random Characters 201
4.5: Case Study: Guessing Birthdays 204
4.6: Character Functions 206
4.7: Case Study: Converting a Hexadecimal
Digit to a Decimal Value 209
4.8: The string Type 212
4.8.1: String Index and Subscript
Operator 214
4.8.2: Concatenating Strings 215
4.8.3: Comparing Strings 216
4.8.4: Reading Strings 217
4.9: Case Study: Revising the Lottery
Program Using Strings 222
4.10: Formatting Console Output 224
4.10.1: The setprecision(n) Manipulator 227
4.10.2: The fixed Manipulator 228
4.10.3: The showpoint
Manipulator 229
4.10.4: The setw(width)
Manipulator 229
4.10.5: The left and right
Manipulators 231
4.11: Simple File Input and Output 235
4.11.1: Writing to a File 235
4.11.2: Reading from a File 237
Key Terms 239
Chapter Summary 240
Programming Exercises 240
5: Loops 252
5.1: Introduction 252
5.2: The while Loop 253
5.3: Case Study: Guessing Numbers 259
5.4: Loop Design Strategies 264
5.5: Controlling a Loop with User
Confirmation or a Sentinel Value 266
5.6: Input and Output Redirections and
Read All Data from a File 270
5.7: The do-while Loop 272
5.8: The for Loop 277
5.9: Which Loop to Use? 282
5.10: Nested Loops 286
5.11: Minimizing Numeric Errors 290
5.12: Case Studies 291
5.12.1: Case Study: Finding the Greatest
Common Divisor 291
5.12.2: Case Study: Predicting the Future
Tuition 294
5.12.3: Case Study: Converting Decimals
to Hexadecimals 296
5.13: Keywords break and
continue 299
5.14: Case Study: Checking
Palindromes 306
5.15: Case Study: Displaying Prime
Numbers 309
Key Terms 312
Chapter Summary 313
Programming Exercises 313
6: Functions 332
6.1: Introduction 332
6.2: Defining a Function 334
6.3: Calling a Function 337
6.4: void Functions 341
6.5: Passing Arguments by Value 348
6.6: Modularizing Code 349
6.7: Overloading Functions 355
6.8: Function Prototypes 360
6.9: Default Arguments 362
6.10: Inline Functions 364
6.11: Local, Global, and Static Local
Variables 367
6.11.1: The Scope of Variables in a for Loop 370
6.11.2: Static Local Variables 371
6.12: Passing Arguments by Reference 374
6.13: Constant Reference Parameters 386
6.14: Case Study: Converting
Hexadecimals to Decimals 386
6.15: Function Abstraction and Stepwise
Refinement 390
6.15.1: Top-Down Design 392
6.15.2: Top-Down or Bottom-Up
Implementation 394
6.15.3: Implementation Details 394
6.15.4: Benefits of Stepwise
Refinement 397
Key Terms 398
Chapter Summary 398
Programming Exercises 400
7: Single-Dimensional Arrays
and C-Strings 419
7.1: Introduction 420
7.2: Array Basics 420
7.2.1: Declaring Arrays 420
7.2.2: Accessing Array Elements 422
7.2.3: Array Initializers 424
7.2.4: Processing Arrays 425
7.2.5: Foreach Loops 429
7.3: Case Study: Analyzing Numbers 431
7.4: Case Study: Deck of Cards 434
7.5: Passing Arrays to Functions 437
7.6: Preventing Changes of Array
Arguments in Functions 442
7.7: Returning Arrays from Functions 444
7.8: Case Study: Counting the Occurrences
of Each Letter 446
7.9: Searching Arrays 449
7.9.1: The Linear Search Approach 450
7.9.2: The Binary Search Approach 451
7.10: Sorting Arrays 456
7.11: C-Strings 460
7.11.1: Input and Output of
C-Strings 461
7.11.2: C-String Functions 462
7.11.3: Copying Strings Using strcpy
and strncpy 465
7.11.4: Concatenating Strings Using
strcat and strncat 466
7.11.5: Comparing Strings 467
7.11.6: Converting Strings to
C-Strings 469
7.11.7: Converting Strings to
Numbers 469
7.12: Converting Numbers to Strings 470
Key Terms 471
Chapter Summary 471
Programming Exercises 472
8: Multidimensional Arrays 492
8.1: Introduction 492
8.2: Declaring Two-Dimensional
Arrays 493
8.3: Processing Two-Dimensional
Arrays 496
8.4: Passing Two-Dimensional Arrays to
Functions 499
8.5: Case Study: Grading a Multiple-
Choice Test 502
8.6: Case Study: Finding a Closest Pair 505
8.7: Case Study: Sudoku 508
8.8: Multidimensional Arrays 513
8.8.1: Case Study: Daily Temperature
and Humidity 514
8.8.2: Case Study: Guessing
Birthdays 516
Key Terms 519
Chapter Summary 519
Programming Exercises 519
9: Objects and Classes 540
9.1: Introduction 540
9.2: Defining Classes for Objects 541
9.3: Example: Defining Classes and
Creating Objects 543
9.4: Constructors 548
9.5: Constructing and Using Objects 549
9.6: Separating Class Definition from
Implementation 553
9.7: Preventing Multiple Inclusions 557
9.8: Inline Functions in Classes 559
9.9: Data Field Encapsulation 560
9.10: The Scope of Variables 564
9.11: Class Abstraction and
Encapsulation 566
Key Terms 573
Chapter Summary 574
Programming Exercises 575
10: Object-Oriented Thinking 581
10.1: Introduction 581
10.2: The string Class 582
10.2.1: Constructing a String 582
10.2.2: Appending to a String 583
10.2.3: Assigning a String 584
10.2.4: Functions at, clear, erase, empty, back, and front 586
10.2.5: Functions length, size, capacity, and c_str() 587
10.2.6: Comparing Strings 588
10.2.7: Obtaining Substrings 589
10.2.8: Seaching in a String 590
10.2.9: Inserting and Replacing
Strings 591
10.2.10: String Operators 593
10.2.11: Converting Numbers to Strings
Using stringstream 596
10.2.12: Splitting Strings 596
10.2.13: Case Study: Replacing
Strings 597
10.3: Passing Objects to Functions 599
10.4: Array of Objects 603
10.5: Instance and Static Members 605
10.6: Constant Member Functions 612
10.7: Thinking in Objects 616
10.8: Class Relationships 622
10.8.1: Association 622
10.8.2: Aggregation and Composition 623
10.9: Case Study: The StackOfIntegers
Class 626
10.10: Constructor Initializer Lists 629
10.11: Class Design Guidelines 634
10.11.1: Cohesion 635
10.11.2: Consistency 635
10.11.3: Encapsulation 635
10.11.4: Clarity 635
10.11.5: Completeness 636
10.11.6: Instance vs. Static 636
Key Terms 637
Chapter Summary 637
Programming Exercises 638
11: Pointers and Dynamic
Memory Management 646
11.1: Introduction 647
11.2: Pointer Basics 647
11.3: Defining Synonymous Types Using
the typedef Keyword 655
11.4: Using const with Pointers 656
11.5: Arrays and Pointers 658
11.6: Passing Pointer Arguments in a
Function Call 663
11.7: Returning a Pointer from
Functions 667
11.8: Useful Array Functions 669
11.9: Dynamic Persistent Memory
Allocation 672
11.10: Creating and Accessing Dynamic
Objects 678
11.11: The this Pointer 680
11.12: Destructors 681
11.13: Case Study: The Course Class 686
11.14: Copy Constructors 690
11.15: Customizing Copy Constructors 694
Key Terms 698
Chapter Summary 699
Programming Exercises 700
12: Templates, Vectors, and
Stacks 708
12.1: Introduction 708
12.2: Templates Basics 709
12.3: Example: A Generic Sort 714
12.4: Class Templates 717
12.5: Improving the Stack Class 726
12.6: The C++ vector Class 729
12.7: Insertion and Deletion and Other
Functions for a Vector 734
12.8: Replacing Arrays Using the vector
Class 736
12.9: Case Study: Evaluating
Expressions 741
12.10: Using Smart Pointers for Automatic
Object Destruction 744
Key Terms 747
Chapter Summary 747
Programming Exercises 748
13: File Input and Output 759
13.1: Introduction 760
13.2: Text I/O 760
13.2.1: Writing Data to a File 760
13.2.2: Reading Data from a File 763
13.2.3: Testing File Existence 767
13.2.4: Testing End of File 767
13.2.5: Letting the User Enter a
Filename 770
13.3: Formatting Output 771
13.4: Functions: getline, get, and
put 773
13.5: fstream and File Open Modes 777
13.6: Testing Stream States 780
13.7: Binary I/O 783
13.7.1: The write Function 784
13.7.2: The read Function 788
13.7.3: Example: Binary Array I/O 790
13.7.4: Example: Binary Object I/O 791
13.8: Random Access File 796
13.9: Updating Files 800
Key Terms 802
Chapter Summary 802
Programming Exercises 803
14: Operator Overloading 808
14.1: Introduction 809
14.2: The Rational Class 810
14.3: Operator Functions 817
14.4: Overloading the Subscript
Operator [] 821
14.5: Overloading Augmented
Assignment Operators 824
14.6: Overloading the Unary
Operators 826
14.7: Overloading the ++ and --
Operators 827
14.8: friend Functions and friend
Classes 829
14.9: Overloading the << and >>
Operators 832
14.10: Automatic Type Conversions 835
14.10.1: Converting to a Primitive Data Type 835
14.10.2: Converting to an Object
Type 836
14.11: Defining Nonmember Functions
for Overloading Operators 837
14.12: The Rational Class with
Overloaded Function Operators 838
14.13: Overloading the = Operators 843
Key Terms 850
Chapter Summary 850
Programming Exercises 851
15: Inheritance and
Polymorphism 856
15.1: Introduction 857
15.2: Base Classes and Derived Classes 857
15.3: Generic Programming 869
15.4: Constructors and Destructors 870
15.4.1: Calling Base Class
Constructors 870
15.4.2: Constructor and Destructor
Chaining 871
15.5: Redefining Functions 878
15.6: Polymorphism 880
15.7: Virtual Functions and Dynamic
Binding 883
15.8: The C++11 override and final
Keywords 890
15.9: The protected Keyword 894
15.10: Abstract Classes and Pure Virtual
Functions 897
15.11: Casting: static_cast versus
dynamic_cast 908
Key Terms 913
Chapter Summary 914
Programming Exercises 915
16: Exception Handling 918
16.1: Introduction 918
16.2: Exception-Handling Overview 919
16.3: Exception Classes 929
16.4: Custom Exception Classes 936
16.5: Multiple Catches 943
16.6: Exception Propagation 949
16.7: Rethrowing Exceptions 950
16.8: When to Use Exceptions 952
Key Terms 953
Chapter Summary 953
Programming Exercises 954
Appendix A: C++ Keywords 956
Appendix B: The ASCII Character
Set 958
Appendix C: Operator Precedence
Chart 960
Appendix D: Number Systems 961
Appendix E: Bitwise
Operations 968
Appendix F: Using Command-
Line Arguments 971
Appendix G: Enumerated
Types 975
Appendix H: Regular
Expressions 980
Appendix I: The Big-O,
Big-Omega,
and Big-Theta
Notations 989




