产品特色
内容简介
《C++11程序设计(第2版)英文版》利用活代码深入研究C++11 和C++ 标准库。内容包括C++11 的新特性,基于模板的标准库容器、迭代器和算法,C++ 标准库字符串,C++ 标准库数组,构造重要类,面向对象编程的案例研究,异常处理,等等。本书示例丰富,包含了从计算机科学、商业、模拟、游戏和其他主题中挑选出来的各种示例程序,并在三个具有工业强度的C++11 编译器上对代码进行了测试;书中配有大量的插图,包括图表、线条图、UML 图、程序和程序输出。
《C++11程序设计(第2版)英文版》适合具有一定高级语言编程背景的程序员阅读。
作者简介
Paul Deitel,Deitel & Associates有限公司的CEO兼CTO,毕业于麻省理工学院,主修信息技术。通过Deitel & Associate有限公司,他已经为行业、政府机关和军队客户提供了数百节编程课程,这些客户包括思科、IBM、西门子、Sun Microsystems、戴尔、Fidelity、肯尼迪航天中心、美国国家强风暴实验室、白沙导弹试验场、Rogue Wave Software、波音公司、SunGard Higher Education、北电网络公司、彪马、iRobot、Invensys等。他和本书的合著者Harvey Deitel博士是畅销全球的编程语言教材、专业书籍和视频的作者。
Harvey Deitel博士,Deitel & Associates有限公司的董事长和首席战略官,在计算机领域中拥有五十多年的经验。Deitel博士获得了麻省理工学院电子工程(学习计算)的学士和硕士学位,并获得了波士顿大学的数学博士学位(学习计算机科学)。在20世纪60年代,通过先进的计算机技术和计算机应用公司,他成为了创建各种IBM操作系统的团队中的一员。在20世纪70年代,他创建了商业操作系统。他拥有丰富的大学教学经验,在1991年与儿子Paul Deitel创办Deitel & Associates有限公司之前,他是波士顿大学计算机科学系的主任,并获得了终身任职权。Deitel的出版物获得了国际上的认可,并被翻译为繁体中文、简体中文、韩语、日语、德语、俄语、西班牙语、法语、波兰语、意大利语、葡萄牙语、希腊语、乌尔都语和土耳其语。Deitel博士为很多大公司、学术研究机构、政府机关和军方提供了数百场专业编程讲座。
目录
前言
1 Introduction
1.1 Introduction
1.2 C++
1.3 Object Technology
1.4 Typical C++ Development Environment
1.5 Test-Driving a C++ Application
1.6 Operating Systems
1.6.1 Windows―A Proprietary Operating System
1.6.2 Linux―An Open-Source Operating System
1.6.3 Apple’s OS X; Apple’s iOS for iPhone?, iPad? and iPod Touch? Devices
1.6.4 Google’s Android
1.7 C++11 and the Open Source Boost Libraries
1.8 Web Resources
2 Introduction to C++ Programming, Input/Output and Operators
2.1 Introduction
2.2 First Program in C++: Printing a Line of Text
2.3 Modifying Our First C++ Program
2.4 Another C++ Program: Adding Integers
2.5 Arithmetic
2.6 Decision Making: Equality and Relational Operators
2.7 Wrap-Up
3 Introduction to Classes, Objects and Strings
3.1 Introduction
3.2 Defining a Class with a Member Function
3.3 Defining a Member Function with a Parameter
3.4 Data Members, set Member Functions and get Member Functions
3.5 Initializing Objects with Constructors
3.6 Placing a Class in a Separate File for Reusability
3.7 Separating Interface from Implementation
3.8 Validating Data with set Functions
3.9 Wrap-Up
4 Control Statements: Part 1; Assignment, ++ and -- Operators
4.1 Introduction
4.2 Control Structures
4.3 if Selection Statement
4.4 if … else Double-Selection Statement
4.5 while Repetition Statement
4.6 Counter-Controlled Repetition
4.7 Sentinel-Controlled Repetition
4.8 Nested Control Statements
4.9 Assignment Operators
4.10 Increment and Decrement Operators
4.11 Wrap-Up
5 Control Statements: Part 2; Logical Operators
5.1 Introduction
5.2 Essentials of Counter-Controlled Repetition
5.3 for Repetition Statement
5.4 Examples Using the for Statement
5.5 do … while Repetition Statement
5.6 switch Multiple-Selection Statement
5.7 break and continue Statements
5.8 Logical Operators
5.9 Confusing the Equality ( == ) and Assignment ( = ) Operators
5.10 Wrap-Up
6 Functions and an Introduction to Recursion
6.1 Introduction
6.2 Math Library Functions
6.3 Function Definitions with Multiple Parameters
6.4 Function Prototypes and Argument Coercion
6.5 C++ Standard Library Headers
6.6 Case Study: Random Number Generation
6.7 Case Study: Game of Chance; Introducing enum
6.8 C++11 Random Numbers
6.9 Storage Classes and Storage Duration
6.10 Scope Rules
6.11 Function Call Stack and Activation Records
6.12 Functions with Empty Parameter Lists
6.13 Inline Functions
6.14 References and Reference Parameters
6.15 Default Arguments
6.16 Unary Scope Resolution Operator
6.17 Function Overloading
6.18 Function Templates
6.19 Recursion
6.20 Example Using Recursion: Fibonacci Series
6.21 Recursion vs. Iteration
6.22 Wrap-Up
7 Class Templates array and vector ;
Catching Exceptions
7.1 Introduction
7.2 array s
7.3 Declaring array s
7.4 Examples Using array s
7.4.1 Declaring an array and Using a Loop to Initialize the array ’s Elements
7.4.2 Initializing an array in a Declaration with an Initializer List
7.4.3 Specifying an array ’s Size with a Constant Variable and Setting array Elements with Calculations
7.4.4 Summing the Elements of an array
7.4.5 Using Bar Charts to Display array Data Graphically
7.4.6 Using the Elements of an array as Counters
7.4.7 Using array s to Summarize Survey Results
7.4.8 Static Local array s and Automatic Local array s
7.5 Range-Based for Statement
7.6 Case Study: Class GradeBook Using an array to Store Grades
7.7 Sorting and Searching array s
7.8 Multidimensional array s
7.9 Case Study: Class GradeBook Using a Two-Dimensional array
7.10 Introduction to C++ Standard Library Class Template vector
7.11 Wrap-Up
8 Pointers
8.1 Introduction
8.2 Pointer Variable Declarations and Initialization
8.3 Pointer Operators
8.4 Pass-by-Reference with Pointers
8.5 Built-In Arrays
8.6 Using const with Pointers
8.6.1 Nonconstant Pointer to Nonconstant Data
8.6.2 Nonconstant Pointer to Constant Data
8.6.3 Constant Pointer to Nonconstant Data
8.6.4 Constant Pointer to Constant Data
8.7 sizeof Operator
8.8 Pointer Expressions and Pointer Arithmetic
8.9 Relationship Between Pointers and Built-In Arrays
8.10 Pointer-Based Strings
8.11 Wrap-Up
9 Classes: A Deeper Look; Throwing Exceptions
9.1 Introduction
9.2 Time Class Case Study
9.3 Class Scope and Accessing Class Members
9.4 Access Functions and Utility Functions
9.5 Time Class Case Study: Constructors with Default Arguments
9.6 Destructors
9.7 When Constructors and Destructors Are Called
9.8 Time Class Case Study: A Subtle Trap―Returning a Reference or a Pointer to a private Data Member
9.9 Default Memberwise Assignment
9.10 const Objects and const Member Functions
9.11 Composition: Objects as Members of Classes
9.12 friend Functions and friend Classes
9.13 Using the this Pointer
9.14 static Class Members
9.15 Wrap-Up
10 Operator Overloading; Class string
10.1 Introduction
10.2 Using the Overloaded Operators of Standard Library Class string
10.3 Fundamentals of Operator Overloading
10.4 Overloading Binary Operators
10.5 Overloading the Binary Stream Insertion and Stream Extraction Operators
10.6 Overloading Unary Operators
10.7 Overloading the Unary Prefix and Postfix ++ and -- Operators
10.8 Case Study: A Date Class
10.9 Dynamic Memory Management
10.10 Case Study: Array Class
10.10.1 Using the Array Class
10.10.2 Array Class Definition
10.11 Operators as Member vs. Non-Member Functions
10.12 Converting Between Types
10.13 explicit Constructors and Conversion Operators
10.14 Overloading the Function Call Operator ()
10.15 Wrap-Up
11 Object-Oriented Programming: Inheritance
11.1 Introduction
11.2 Base Classes and Derived Classes
11.3 Relationship between Base and Derived Classes
11.3.1 Creating and Using a CommissionEmployee Class
11.3.2 Creating a BasePlusCommissionEmployee Class Without Using Inheritance
11.3.3 Creating a CommissionEmployee ? BasePlusCommissionEmployee Inheritance Hierarchy
11.3.4 CommissionEmployee ? BasePlusCommissionEmployee Inheritance Hierarchy Using protected Data
11.3.5 CommissionEmployee ? BasePlusCommissionEmployee Inheritance Hierarchy Using private Data
11.4 Constructors and Destructors in Derived Classes
11.5 public , protected and private Inheritance
11.6 Software Engineering with Inheritance
11.7 Wrap-Up
12 Object-Oriented Programming: Polymorphism
12.1 Introduction
12.2 Introduction to Polymorphism: Polymorphic Video Game
12.3 Relationships Among Objects in an Inheritance Hierarchy
12.3.1 Invoking Base-Class Functions from Derived-Class Objects
12.3.2 Aiming Derived-Class Pointers at Base-Class Objects
12.3.3 Derived-Class Member-Function Calls via Base-Class Pointers
12.3.4 Virtual Functions and Virtual Destructors
12.4 Type Fields and switch Statements
12.5 Abstract Classes and Pure virtual Functions
12.6 Case Study: Payroll System Using Polymorphism
12.6.1 Creating Abstract Base Class Employee
12.6.2 Creating Concrete Derived Class SalariedEmployee
12.6.3 Creating Concrete Derived Class CommissionEmployee
12.6.4 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee
12.6.5 Demonstrating Polymorphic Processing
12.7 (Optional) Polymorphism, Virtual Functions and Dynamic Binding“Under the Hood”
12.8 Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast , typeid and
type_info
12.9 Wrap-Up
13 Stream Input/Output: A Deeper Look
13.1 Introduction
13.2 Streams
13.2.1 Classic Streams vs. Standard Streams
13.2.2 iostream Library Headers
13.2.3 Stream Input/Output Classes and Objects
13.3 Stream Output
13.3.1 Output of char * Variables
13.3.2 Character Output Using Member Function put
13.4 Stream Input
13.4.1 get and getline Member Functions
13.4.2 istream Member Functions peek , putback and ignore
13.4.3 Type-Safe I/O
13.5 Unformatted I/O Using read , write and gcount
13.6 Introduction to Stream Manipulators
13.6.1 Integral Stream Base: dec , oct , hex and setbase
13.6.2 Floating-Point Precision ( precision , setprecision )
13.6.3 Field Width ( width , setw )
13.6.4 User-Defined Output Stream Manipulators
13.7 Stream Format States and Stream Manipulators
13.7.1 Trailing Zeros and Decimal Points ( showpoint )
13.7.2 Justification ( left , right and internal )
13.7.3 Padding ( fill , setfill )
13.7.4 Integral Stream Base ( dec , oct , hex , showbase )
13.7.5 Floating-Point Numbers; Scientific and Fixed Notation( scientific , fixed )
13.7.6 Uppercase/Lowercase Control ( uppercase )
13.7.7 Specifying Boolean Format ( boolalpha )
13.7.8 Setting and Resetting the Format State via Member Function flags
13.8 Stream Error States
13.9 Tying an Output Stream to an Input Stream
13.10 Wrap-Up
14 File Processing
14.1 Introduction
14.2 Files and Streams
14.3 Creating a Sequential File
14.4 Reading Data from a Sequential File
14.5 Updating Sequential Files
14.6 Random-Access Files
14.7 Creating a Random-Access File
14.8 Writing Data Randomly to a Random-Access File
14.9 Reading from a Random-Access File Sequentially
14.10 Case Study: A Transaction-Processing Program
14.11 Object Serialization
14.12 Wrap-Up
15 Standard Library Containers and Iterators
15.1 Introduction
15.2 Introduction to Containers
15.3 Introduction to Iterators
15.4 Introduction to Algorithms
15.5 Sequence Containers
15.5.1 vector Sequence Container
15.5.2 list Sequence Container
15.5.3 deque Sequence Container
15.6 Associative Containers
15.6.1 multiset Associative Container
15.6.2 set Associative Container
15.6.3 multimap Associative Container
15.6.4 map Associative Container
15.7 Container Adapters
15.7.1 stack Adapter
15.7.2 queue Adapter
15.7.3 priority_queue Adapter
15.8 Class bitset
15.9 Wrap-Up
16 Standard Library Algorithms
16.1 Introduction
16.2 Minimum Iterator Requirements
16.3 Algorithms
16.3.1 fill , fill_n , generate and generate_n
16.3.2 equal , mismatch and lexicographical_compare
16.3.3 remove , remove_if , remove_copy and remove_copy_if
16.3.4 replace , replace_if , replace_copy and replace_copy_if
16.3.5 Mathematical Algorithms
16.3.6 Basic Searching and Sorting Algorithms
16.3.7 swap , iter_swap and swap_ranges
16.3.8 copy_backward , merge , unique and reverse
16.3.9 inplace_merge , unique_copy and reverse_copy
16.3.10 Set Operations
16.3.11 lower_bound , upper_bound and equal_range
16.3.12 Heapsort
16.3.13 min , max , minmax and minmax_element
16.4 Function Objects
16.5 Lambda Expressions
16.6 Standard Library Algorithm Summary
16.7 Wrap-Up
17 Exception Handling: A Deeper Look
17.1 Introduction
17.2 Example: Handling an Attempt to Divide by Zero
17.3 Rethrowing an Exception
17.4 Stack Unwinding
17.5 When to Use Exception Handling
17.6 Constructors, Destructors and Exception Handling
17.7 Exceptions and Inheritance
17.8 Processing new Failures
17.9 Class unique_ptr and Dynamic Memory Allocation
17.10 Standard Library Exception Hierarchy
17.11 Wrap-Up
18 Introduction to Custom Templates
18.1 Introduction
18.2 Class Templates
18.3 Function Template to Manipulate a Class-Template Specialization Object
18.4 Nontype Parameters
18.5 Default Arguments for Template Type Parameters
18.6 Overloading Function Templates
18.7 Wrap-Up
19 Class string and String Stream Processing: A Deeper Look
19.1 Introduction
19.2 string Assignment and Concatenation
19.3 Comparing string s
19.4 Substrings
19.5 Swapping string s
19.6 string Characteristics
19.7 Finding Substrings and Characters in a string
19.8 Replacing Characters in a string
19.9 Inserting Characters into a string
19.10 Conversion to Pointer-Based char * Strings
19.11 Iterators
19.12 String Stream Processing
19.13 C++11 Numeric Conversion Functions
19.14 Wrap-Up
20 Bits, Characters, C Strings and struct s
20.1 Introduction
20.2 Structure Definitions
20.3 typedef
20.4 Example: Card Shuffling and Dealing Simulation
20.5 Bitwise Operators
20.6 Bit Fields
20.7 Character-Handling Library
20.8 C String-Manipulation Functions
20.9 C String-Conversion Functions
20.10 Search Functions of the C String-Handling Library
20.11 Memory Functions of the C String-Handling Library
20.12 Wrap-Up
21 Other Topics
21.1 Introduction
21.2 const_cast Operator
21.3 mutable Class Members
21.4 namespace s
21.5 Operator Keywords
21.6 Pointers to Class Members ( .* and ->* )
21.7 Multiple Inheritance
21.8 Multiple Inheritance and virtual Base Classes
21.9 Wrap-Up
22 ATM Case Study, Part 1:Object-Oriented Design with the UML
22.1 Introduction
22.2 Introduction to Object-Oriented Analysis and Design
22.3 Examining the ATM Requirements Document
22.4 Identifying the Classes in the ATM Requirements Document
22.5 Identifying Class Attributes
22.6 Identifying Objects’ States and Activities
22.7 Identifying Class Operations
22.8 Indicating Collaboration Among Objects
22.9 Wrap-Up
23 ATM Case Study, Part 2:Implementing an Object-Oriented Design
23.1 Introduction
23.2 Starting to Program the Classes of the ATM System
23.3 Incorporating Inheritance into the ATM System
23.4 ATM Case Study Implementation
23.4.1 Class ATM
23.4.2 Class Screen
23.4.3 Class Keypad
23.4.4 Class CashDispenser
23.4.5 Class DepositSlot
23.4.6 Class Account
23.4.7 Class BankDatabase
23.4.8 Class Transaction
23.4.9 Class BalanceInquiry
23.4.10 Class Withdrawal
23.4.11 Class Deposit
23.4.12 Test Program ATMCaseStudy.cpp
23.5 Wrap-Up
A Operator Precedence and Associativity
B ASCII Character Set
C Fundamental Types
D Number Systems
D.1 Introduction
D.2 Abbreviating Binary Numbers as Octal and Hexadecimal Numbers
D.3 Converting Octal and Hexadecimal Numbers to Binary Numbers
D.4 Converting from Binary, Octal or Hexadecimal to Decimal
D.5 Converting from Decimal to Binary, Octal or Hexadecimal
D.6 Negative Binary Numbers: Two’s Complement Notation
E Preprocessor
E.1 Introduction
E.2 #include Preprocessing Directive
E.3 #define Preprocessing Directive: Symbolic Constants
E.4 #define Preprocessing Directive: Macros
E.5 Conditional Compilation
E.6 #error and #pragma Preprocessing Directives
E.7 Operators # and ##
E.8 Predefined Symbolic Constants
E.9 Assertions
E.10 Wrap-Up
Index
线上章节及附录
第 24 章及附录 F~K 为 PDF 文档
前言/序言
“语言的首要价值是清晰……”
——Galen
欢迎阅读本书。本书会向软件开发人员介绍最先进的计算技术。
我们专注于软件工程的最佳实践。本书沿用了Deitel特色的“程序实况解说”风格——用完整的可运行程序(而不是代码片段)来解释相关概念。每个完整的代码示例都配有生动的示例。所有的源代码都可以从下面的网址下载:
如果你在阅读本书时遇到任何困难,请发送邮件到:
我们会及时回复你。要了解本书的最新信息,加入我们的Facebook社区、Twitter社区、Google+社区和LinkedIn社区,你还可以订阅Deitel Buzz Online电子邮件新闻简报。
本书特色
本书的主要特色如下所述。
C++11标准
2011年发布的C++11新标准是我们写这本书的动机。我们使用标有“11”的图标来表示在本书中提到的每个C++11新特性,如本页的空白处所示。这一版包含的一些C++11特性如下。
符合C++11新标准。广泛地覆盖了C++11的新特性。
在3个具有工业强度的C++11编译器上对代码进行了测试。我们在GNU C++ 4.7、Microsoft Visual C++ 2012和Apple Xcode 4.5的LLVM上对代码示例进行了测试。
智能指针。智能指针可以帮助你避免动态由于提供了内置指针无法实现的功能而导致的内存管理错误。我们将在第17章讨论unique_ptr,在第24章讨论shared_ptr和weak_ptr。
更早地介绍基于模板的标准库容器、迭代器和算法,并用C++11新特性对这些内容进行了扩充。我们将第1版中位于第20章的标准库容器、迭代器和算法的处理移到了第15章和第16章,并且使用C++11新特性对这些内容进行了扩充。你需要的绝大多数数据结构都可以通过重用这些标准库来实现。
在线阅读第24章。我们将在这一章介绍C++11的其他主题。C++11新标准是在2011年发布的,但并非所有的C++编译器都完全支持这些特性。在我们编写本书时,如果某个C++特性已经在这3个主要的编译器中实现了,我们就用文本和代码实例来介绍该特性。如果这3个编译器都没有实现该特性,我们就用粗斜体标题和对这个特性的简短说明来介绍它。当这些特性被编译器实现时,我们就会在第24章对这些简短说明进行扩充。之所以将本章放在网上,是因为这样做我们就可以动态更新其内容。本章包含对正则表达式、shared_ptr和weak_ptr智能指针、移动语义等的讨论。你可以通过以下网址访问本章:
R随机数生成、模拟和游戏。为了让程序更安全(参见下一页的“C++安全编程”一节),我们会在本书中讨论C++11的非确定性随机数生成机制。
面向对象程序设计
提早介绍对象。我们在本书的第1章就介绍了对象技术的基本概念和术语,在第3章将会带你开发一个自定义的C++类和对象。
C++标准库字符串。C++提供了两种类型的字符串——字符串类对象(从第3章开始使用)和C字符串(来自C语言)。我们用C++字符串类的实例替换了大多数C字符串,以便让程序变得更健壮,并消除C字符串带来的很多安全问题。我们稍后会在本书中讨论C字符串,以便让你能够使用工业上的遗留代码。在新的开发项目中,你应该支持字符串对象。
C++标准库数组。我们在本书中使用的主要是标准库的数组类模板,而不是内置的、C风格的、基于指针的数组。但我们还是会介绍内置数组,因为它们仍然会被用在C++中,而且了解它才能够阅读遗留代码。C++提供了三种类型的数组——类模板数组、vector(从第7章开始使用),以及C风格的、基于指针的数组(在第8章介绍)。我们在本书中会酌情使用模板数组和类模板vector(偶尔),而不是C数组。在新的开发项目中,你应该支持类模板数组和vector。
构造重要类。本书的一个主要目标是让你构造重要的、可重用的C++类。在第10章的案例研究中,你将创建自己的自定义Array类。第10章以类模板字符串的测试驱动开始,这样在你使用重载操作符实现自己的自定义类前,可以先看到如何使用操作符重载。
面向对象编程的案例研究。我们提供了跨越多个章节并涵盖了软件开发生命周期的案例研究。这些案例研究包括第3~7章的GradeBook类,第9章的Time类和第11章、第12章的Employee类。第12章用详细的图表说明了C++如何“在幕后”实现多态、虚函数和动态绑定。
可选案例研究:使用UML创建一个ATM的面向对象设计和C++实现。UML(统一建模语言,Unified Modeling Language)是为面向对象系统建模的行业标准图形化语言。我们在本书的前几章就介绍了UML。第22章和第23章包含使用UML来进行面向对象设计的可选案例研究。我们设计并实现了一个简单的自动取款机(ATM)的软件。我们分析了一个指定如何构建系统的典型需求文档。我们确定了实现该系统所需要的类、这些类需要拥有的属性和这些类需要表现出的行为,我们还指定了这些类必须如何交互才能满足系统需求。我们根据设计产生了一个完整的C++实现。读者经常反馈说这个案例研究“将所有内容都联系起来”,并让他们对面向对象有了更深入的了解。
异常处理。在本书的前几章就包含了基本的异常处理。你可以从第17章来了解异常处理的更多内容。
主要的编程范式。我们在本书中讨论了面向对象编程和泛型编程。
教学特色
示例。本书包含了从计算机科学、商业、模拟、游戏和其他主题中挑选出来的各种示例程序。
插图和图。本书包含了大量的图表、线条图、UML图、程序和程序输出。
其他特色
指针。本书详细地介绍了内置指针,以及内置指针、C字符串和内置数组之间的密切关系。
调试器附录。本书包含了3个调试器附录——附录H,讲述Visual Studio调试器的使用;附录I,讲述GNU C++调试器的使用;附录J,讲述Xcode调试器的使用。
C++安全编程
很难创建出可以抵抗病毒、蠕虫等其他“恶意软件”攻击的、具有工业强度的系统。如今,通过互联网,这类攻击瞬间就可以对全球范围造成影响。从开发周期的一开始就在软件中加入安全性,可以大大减少安全风险。
人们创建了CERT Coordination Center来分析和及时应对攻击。CERT(计算机安全应急响应小组,Computer Emergency Response Team)是一个政府资助的组织,位于卡内基梅隆大学软件工程研究所。CERT会发布和推广各种流行编程语言的安全编码标准,帮助软件开发人员实现具有工业强度的系统,从而避免会造成开放式系统受到攻击的编程实践。
我们要感谢Robert C. Seacord,他是CERT的安全编程管理员和卡内基梅隆大学计算机科学系的副教授。Seacord先生是C How to Program, 7e一书的技术评审,他从安全角度审查了这本书中的C语言程序,并建议我们遵守CERT C语言安全编程标准。
本书也遵守CERT C++安全编码标准指南(符合书籍水平),你可以通过以下网址找到该指南:
www.securecoding.cert.org
我们很高兴地发现,从20世纪90年代早期开始我们就在书籍中推荐了这些编程实践。如果你要创建具有工业强度的C++系统,就一定不要错过Secure Coding in C and C++, 2e(Robert Seacord,Addison-Wesley Professional出版)。
教学方法
本书强调的是程序的清晰性,并专注于构建良好的软件工程。
程序实况解说。本书包含上百个“程序实况解说”示例——每个新概念都是在完整的可运行C++程序中提出的,紧接着是显示该程序输入和输出的一个或者多个实际执行。
语法着色。为了增加可读性,我们为代码添加了语法阴影,这与大多数IDE和代码编辑器给代码的语法添加颜色是类似的。我们采用的代码的着色约定是:
注释用常规字体表示
关键字用粗黑字体表示
常量和字面值用比关键字稍浅一点的粗灰字来表示
所有的其他代码以黑体表示
代码高亮显示。本书在每个源代码程序的关键代码段都放置了一个灰色的矩形框。
使用字体突出显示。为了便于参考,我们用粗体表示每个定义性出现的关键概念和索引的页面引用,用加粗的Helvetica字体来强调屏幕组件(例如,File菜单),用Lucida字体来强调C语言程序文本(例如,int x = 5;)。
网页存取。所有的源代码示例都可以从以下网址下载:
目标。每章都包含一个本章目标的列表。
编程技巧。我们提供了编程技巧来帮助你关注程序开发的重要方面。这些技巧和实践是我们在8年编程和企业培训经验中积累的精华。最佳编程实践
最佳编程实践让读者关注那些有助于产生更清晰、更易理解和维护程序的技术。
常见编程错误
指出这些常见的编程错误可以降低读者犯类似错误的可能性。
错误预防技巧
这些技巧包括揭露和删除程序中bug的建议;大多数技巧描述的是如何预防将bug带入C++程序中。
性能技巧
这些技巧强调的是如何让程序运行得更快,或者尽量减少它们占用的内存。
可移植性技巧
可移植性技巧可以帮助你编写能够在各种平台上运行的代码。
软件工程意见
软件工程意见强调的是影响软件系统(尤其是大型系统)构建的架构和设计问题。
在线章节和附录
下面的章节和附录可以在线获取。
第24章,C++11的其他主题(C++11: Additional Features)。
附录F,C遗留代码主题(C Legacy Code Topics)。
附录G,UML 2:其他图表类型(UML 2: Additional Diagram Types)。
附录H,Visual Studio调试器的使用(Using the Visual Studio Debugger)。
附录I,GNU C++调试器的使用(Using the GNU C++ Debugger)。
附录J,Xcode调试器的使用(Using the Xcode Debugger)。
附录K,对Mac OS X上C++程序的测试驱动(Test Driving a C++ Program on Mac OS X)。
可以通过以下网址访问在线章节和附录:
www.informit.com/register
你需要注册一个InformIT账号,并登录。登录后,你就能看到一个“Register a Product”框。输入本书的ISBN(9780133439854)就可以访问本书的在线章节和附录了。
获取本书使用的软件
本书的代码示例是通过下面的C++开发工具来编写的:
用于Windows桌面的免费Visual Studio Express 2012,其中包含了Visual C++和其他Microsoft开发工具。它可以在Windows 7和Windows 8中运行,并可以从如下网址下载。
www.microsoft.com/express
GNU的免费GNU C++(gcc.gnu.org/install/binaries.html),其中大多数Linux系统都已经预先安装了该编译器,它还可以被安装到Mac OS X和Windows系统中。
Apple的免费Xcode,OS X用户可以从Mac App Store中下载它。
C++11基础:LiveLessons视频培训产品的第I部分~第III部分
“C++ 11基础:LiveLessons视频培训产品的第I部分~第III部分”介绍了开始使用C++构建健壮的、强大的软件时所需要知道的内容。它包含了二十多个小时与本书同步的专家培训。要了解关于Deitel LiveLesssons视频产品的更多信息,请访问
www.deitel.com/livelessons
或者可以通过deitel@deitel.com联系我们。如果你已经订阅了Safari Books Online(www.safaribooksonline.com),你还可以获取我们的LiveLessons视频。这些LiveLessons已于2013年夏季上线。
致谢
我们要感谢Deitel & Associates有限公司的Abbey Deitel和Barbara Deitel。他们为这个项目投入了大量时间。Abbey参与撰写了本书的第1章和前言,她和Barbara仔细研究了C++11的新功能。
我们有幸与Prentice Hall/Pearson的出版专家团队共同完成该项目。我们要感谢培生科技出版集团的主编Mark L. Taub给我们的指导和为此付出的精力。我们要感谢Carole Snyder出色地完成了招募优秀的C++社区成员来审稿的工作;感谢Chuti Prasertsith为本书设计了具有创造力和精确度的封面——我们把自己对封面的构想告诉他,而他准确地实现了它;感谢John Fuller出色地完成了我们的Deitel开发人员系统图书和LiveLessons视频产品的出版工作。
审稿人员
还要感谢本书审稿人员的付出,他们提出的建设性的批评意见帮助我们完成了本书的新版本。他们完成了对本书文本和程序的审查,并为提高本书的表述方式提供了无数建议,他们是:Dean Michael Berris(谷歌,ISO C++委员会成员)、Danny Kaley(C++专家,经过认证的系统分析师和C++标准委员会的前成员)、Linda M. Krause(艾姆赫斯特学院)、James P. McNellis(微软公司)、Robert C.Seacord(SEI/CERT的安全编码管理员、Secure Coding in C and C++一书的作者)、José Antonio González Seco(西班牙安达卢西亚议会议员)、Virginia Bailey(杰克逊州立大学)、Thomas J. Borrelli(罗彻斯特理工学院)、Ed Brey(科勒有限公司)、Chris Cox(Adobe系统有限公司)、Gregory Dai(eBay)、Peter J. DePasquale(美国新泽西学院)、John Dibling(SpryWare)、Susan Gauch(阿肯色大学)、Doug Gregor(苹果公司)、Jack Hagemeister(华盛顿州立大学)、Williams M. Higdon(美国印第安纳大学)、Anne B. Horton(Lockheed Martin公司)、Terrell Hull(Logicalis集成解决方案)、Ed James-Beckham(Borland公司)、Wing-Ning Li(阿肯色大学)、Dean Mathias(犹他州立大学)、Robert A. McLain(潮水社区学院)、Robert Myers(佛罗里达州立大学)、Gavin Osborne(萨斯喀彻温省应用科技学院)、Amar Raheja(加利福尼亚大学波莫纳分校)、April Reagan(微软)、Raymond Stephenson(微软)、Dave Topham(美国奥龙尼学院)、Anthony Williams(作家和C++标准委员会成员)和Chad Willwerth(华盛顿大学塔科马分校)。
真诚地欢迎读者提出可以提高本书质量的意见、批评、更正和建议,请将它们发送
我们会及时回复你。我们在编写本书时享受到了很多乐趣。希望你也能在本书的阅读过程中得到乐趣。
Paul Deitel
Harvey Deitel
《C++11程序设计(第2版)》:一本现代C++编程的深度指南 《C++11程序设计(第2版)》是一本旨在全面深入地讲解C++11标准的权威著作。本书在原有基础上进行了修订与完善,不仅涵盖了C++11引入的各项核心特性,更深入探讨了这些特性在实际编程中的应用、设计理念以及最佳实践。对于希望掌握现代C++编程精髓、提升代码质量和开发效率的开发者而言,本书无疑是一本不可或缺的参考。 本书的独到之处与价值体现 本书并非简单罗列C++11的新增语法,而是着力于构建起一套完整的现代C++编程思维体系。作者以清晰的逻辑、丰富的实例和严谨的论述,引导读者理解C++11的强大之处,并学会如何将其运用于解决实际编程问题。 一、 C++11核心特性的深度解析 本书对C++11引入的各项重要特性进行了细致入微的讲解,力求让读者不仅知其然,更知其所以然。 自动类型推导(`auto`)与统一初始化(Uniform Initialization): 自动类型推导极大地简化了变量声明,减少了冗余,提高了代码的可读性。本书详细阐述了`auto`在不同上下文中的行为,以及其与迭代器、函数返回类型等结合时的强大威力。统一初始化,特别是使用花括号 `{}` 的初始化方式,为各种类型的对象提供了更一致、更安全、更易于理解的初始化机制,避免了令人头疼的“most vexing parse”问题。书中会深入剖析其在类成员、数组、STL容器等场景下的应用,以及它如何与列表初始化(list initialization)协同工作。 右值引用(Rvalue References)与移动语义(Move Semantics): 这是C++11中最具革命性的特性之一,对性能优化有着至关重要的作用。本书将详细介绍右值(rvalue)和左值(lvalue)的概念,以及右值引用的语法。在此基础上,会深入讲解移动构造函数(move constructor)、移动赋值运算符(move assignment operator)以及`std::move`和`std::forward`等辅助工具。通过丰富的示例,读者将理解如何利用移动语义来避免不必要的拷贝,从而显著提升处理大型对象(如字符串、向量、文件流等)时的性能,特别是在函数返回值优化(RVO)和返回值优化(NRVO)结合使用时,其效果尤为显著。 智能指针(Smart Pointers): 内存泄漏是C++编程中一个普遍存在且难以调试的问题。C++11引入的智能指针,如`std::unique_ptr`、`std::shared_ptr`和`std::weak_ptr`,提供了一种 RAII(Resource Acquisition Is Initialization)的现代内存管理方式,极大地降低了内存泄漏的风险。本书会详细解析这三种智能指针的设计理念、使用场景、性能特点以及它们之间的区别与联系。读者将学会如何使用它们来管理动态分配的资源,并理解其如何与容器、函数等协同工作,实现自动化的资源释放。 Lambda表达式(Lambda Expressions): Lambda表达式为创建匿名函数对象提供了简洁、灵活的方式,尤其是在需要传递简单函数作为参数时(例如STL算法)。本书会深入讲解Lambda表达式的语法,包括参数列表、捕获列表(值捕获、引用捕获、默认捕获)以及返回值推导。通过大量实例,读者将学会如何利用Lambda表达式来简化代码,编写更具表现力、更易于维护的算法,例如在`std::for_each`、`std::sort`、`std::transform`等算法中。 并发与并行编程(Concurrency and Parallelism): 随着多核处理器的普及,并发和并行编程已成为提升程序性能的关键。C++11提供了标准库支持,包括线程(`std::thread`)、互斥量(`std::mutex`)、条件变量(`std::condition_variable`)、原子操作(`std::atomic`)以及Future/Promise机制(`std::future`、`std::promise`)。本书将详细介绍这些工具的使用方法,并引导读者理解并发编程的基本概念,如竞态条件(race condition)、死锁(deadlock)等,以及如何通过锁机制和原子操作来编写安全、高效的并发程序。 范围for循环(Range-based for loop): 范围for循环为遍历容器或序列提供了更简洁、更自然的语法,消除了手动管理迭代器或索引的繁琐。本书会展示范围for循环在各种容器(数组、`std::vector`、`std::list`、`std::map`等)上的应用,以及如何结合`auto`关键字来进一步简化代码。 新STL容器与算法: C++11在STL方面也有许多重要的改进,例如新增了`std::array`(固定大小的数组)、`std::forward_list`(单向链表)以及一系列新的算法。本书将对这些新组件进行详细介绍,并阐述它们在不同场景下的适用性。 二、 设计模式与最佳实践 除了核心特性的讲解,本书更注重将这些特性融入到现代C++的设计模式和最佳实践中。 面向对象设计的进化: C++11的特性使得面向对象设计更加强大和灵活。本书会探讨如何结合右值引用和移动语义来优化对象的拷贝和赋值,从而实现更高效的资源管理。同时,也会介绍如何利用Lambda表达式和智能指针来编写更简洁、更安全的面向对象代码。 泛型编程与STL的深入应用: 泛型编程是C++的核心优势之一。本书将引导读者更深入地理解STL的工作原理,并学会如何编写高效的模板代码。通过对STL算法和容器的深入剖析,以及如何与C++11的新特性结合,读者将能够构建出更强大、更具复用性的代码库。 性能优化的艺术: C++语言以其高性能著称,而C++11的许多特性正是为了进一步提升性能而设计的。本书会从多个角度讲解性能优化,包括避免不必要的拷贝、利用移动语义、选择合适的容器和算法、以及编写高效的并发代码。 代码的可读性与可维护性: 尽管C++语言功能强大,但写出清晰、易于理解和维护的代码同样重要。本书将强调如何利用C++11的新特性来写出更具表现力、更简洁的代码,从而提升整体的可读性和可维护性。 三、 适合的读者群体 本书适合以下人群: C++初学者: 对于有一定编程基础,但希望学习现代C++的初学者,本书将提供一个坚实的基础。 有经验的C++开发者: 对于已经熟悉C++但希望深入了解和掌握C++11及以后版本特性的开发者,本书将提供宝贵的见解和实践指导。 希望提升代码质量和性能的开发者: 本书将教授如何利用C++11的特性来编写更高效、更健壮、更易于维护的代码。 从事系统编程、游戏开发、高性能计算等领域的开发者: 这些领域对C++的性能和底层控制要求较高,本书中的许多内容将直接适用于这些场景。 结语 《C++11程序设计(第2版)》并非一本简单的教程,而是一本能够陪伴开发者不断成长、不断精进的“武林秘籍”。通过对本书的学习,读者将能够深刻理解现代C++的精髓,并将其转化为实际的开发优势,编写出更优秀、更具竞争力的软件。这是一次对C++语言深度探索的旅程,也是一次对自身编程技艺的全面升级。