Home Page Icon
Home Page
Table of Contents for
Chapter 18. Tools for Large Programs
Close
Chapter 18. Tools for Large Programs
by Barbara E. Moo, Josée Lajoie, Stanley B. Lippman
C++ Primer, Fifth Edition
Title Page
Copyright Page
Dedication Page
Contents
New Features in C++11
Preface
Why Read This Book?
Changes to the Fifth Edition
Structure of This Book
Aids to the Reader
A Note about Compilers
Acknowledgments
Chapter 1. Getting Started
Contents
1.1. Writing a Simple C++ Program
1.1.1. Compiling and Executing Our Program
1.2. A First Look at Input/Output
Standard Input and Output Objects
A Program That Uses the IO Library
Writing to a Stream
Using Names from the Standard Library
Reading from a Stream
Completing the Program
1.3. A Word about Comments
Kinds of Comments in C++
Comment Pairs Do Not Nest
1.4. Flow of Control
1.4.1. The while Statement
1.4.2. The for Statement
1.4.3. Reading an Unknown Number of Inputs
1.4.4. The if Statement
1.5. Introducing Classes
1.5.1. The Sales_item Class
1.5.2. A First Look at Member Functions
1.6. The Bookstore Program
Chapter Summary
Defined Terms
Part I. The Basics
Contents
Chapter 2. Variables and Basic Types
Contents
2.1. Primitive Built-in Types
2.1.1. Arithmetic Types
2.1.2. Type Conversions
2.1.3. Literals
2.2. Variables
2.2.1. Variable Definitions
2.2.2. Variable Declarations and Definitions
2.2.3. Identifiers
2.2.4. Scope of a Name
2.3. Compound Types
2.3.1. References
2.3.2. Pointers
2.3.3. Understanding Compound Type Declarations
2.4. const Qualifier
Initialization and const
By Default, const Objects Are Local to a File
2.4.1. References to const
2.4.2. Pointers and const
2.4.3. Top-Level const
2.4.4. constexpr and Constant Expressions
2.5. Dealing with Types
2.5.1. Type Aliases
2.5.2. The auto Type Specifier
2.5.3. The decltype Type Specifier
2.6. Defining Our Own Data Structures
2.6.1. Defining the Sales_data Type
2.6.2. Using the Sales_data Class
2.6.3. Writing Our Own Header Files
Chapter Summary
Defined Terms
Chapter 3. Strings, Vectors, and Arrays
Contents
3.1. Namespace using Declarations
A Separate using Declaration Is Required for Each Name
Headers Should Not Include using Declarations
A Note to the Reader
3.2. Library string Type
3.2.1. Defining and Initializing strings
3.2.2. Operations on strings
3.2.3. Dealing with the Characters in a string
3.3. Library vector Type
3.3.1. Defining and Initializing vectors
3.3.2. Adding Elements to a vector
3.3.3. Other vector Operations
3.4. Introducing Iterators
3.4.1. Using Iterators
3.4.2. Iterator Arithmetic
3.5. Arrays
3.5.1. Defining and Initializing Built-in Arrays
3.5.2. Accessing the Elements of an Array
3.5.3. Pointers and Arrays
3.5.4. C-Style Character Strings
3.5.5. Interfacing to Older Code
3.6. Multidimensional Arrays
Initializing the Elements of a Multidimensional Array
Subscripting a Multidimensional Array
Using a Range for with Multidimensional Arrays
Pointers and Multidimensional Arrays
Type Aliases Simplify Pointers to Multidimensional Arrays
Chapter Summary
Defined Terms
Chapter 4. Expressions
Contents
4.1. Fundamentals
4.1.1. Basic Concepts
4.1.2. Precedence and Associativity
4.1.3. Order of Evaluation
4.2. Arithmetic Operators
4.3. Logical and Relational Operators
Logical AND and OR Operators
Logical NOT Operator
The Relational Operators
Equality Tests and the bool Literals
4.4. Assignment Operators
Assignment Is Right Associative
Assignment Has Low Precedence
Beware of Confusing Equality and Assignment Operators
Compound Assignment Operators
4.5. Increment and Decrement Operators
Combining Dereference and Increment in a Single Expression
Remember That Operands Can Be Evaluated in Any Order
4.6. The Member Access Operators
4.7. The Conditional Operator
Nesting Conditional Operations
Using a Conditional Operator in an Output Expression
4.8. The Bitwise Operators
Bitwise Shift Operators
Bitwise NOT Operator
Bitwise AND, OR, and XOR Operators
Using Bitwise Operators
Shift Operators (aka IO Operators) Are Left Associative
4.9. The sizeof Operator
4.10. Comma Operator
4.11. Type Conversions
When Implicit Conversions Occur
4.11.1. The Arithmetic Conversions
4.11.2. Other Implicit Conversions
4.11.3. Explicit Conversions
4.12. Operator Precedence Table
Chapter Summary
Defined Terms
Chapter 5. Statements
Contents
5.1. Simple Statements
Null Statements
Beware of Missing or Extraneous Semicolons
Compound Statements (Blocks)
5.2. Statement Scope
5.3. Conditional Statements
5.3.1. The if Statement
5.3.2. The switch Statement
5.4. Iterative Statements
5.4.1. The while Statement
5.4.2. Traditional for Statement
5.4.3. Range for Statement
5.4.4. The do while Statement
5.5. Jump Statements
5.5.1. The break Statement
5.5.2. The continue Statement
5.5.3. The goto Statement
5.6. try Blocks and Exception Handling
5.6.1. A throw Expression
5.6.2. The try Block
5.6.3. Standard Exceptions
Chapter Summary
Defined Terms
Chapter 6. Functions
Contents
6.1. Function Basics
Writing a Function
Calling a Function
Parameters and Arguments
Function Parameter List
Function Return Type
6.1.1. Local Objects
6.1.2. Function Declarations
6.1.3. Separate Compilation
6.2. Argument Passing
6.2.1. Passing Arguments by Value
6.2.2. Passing Arguments by Reference
6.2.3. const Parameters and Arguments
6.2.4. Array Parameters
6.2.5. main: Handling Command-Line Options
6.2.6. Functions with Varying Parameters
6.3. Return Types and the return Statement
6.3.1. Functions with No Return Value
6.3.2. Functions That Return a Value
6.3.3. Returning a Pointer to an Array
6.4. Overloaded Functions
Defining Overloaded Functions
Determining Whether Two Parameter Types Differ
Overloading and const Parameters
const_cast and Overloading
Calling an Overloaded Function
6.4.1. Overloading and Scope
6.5. Features for Specialized Uses
6.5.1. Default Arguments
6.5.2. Inline and constexpr Functions
6.5.3. Aids for Debugging
6.6. Function Matching
Determining the Candidate and Viable Functions
Finding the Best Match, If Any
Function Matching with Multiple Parameters
6.6.1. Argument Type Conversions
6.7. Pointers to Functions
Using Function Pointers
Pointers to Overloaded Functions
Function Pointer Parameters
Returning a Pointer to Function
Using auto or decltype for Function Pointer Types
Chapter Summary
Defined Terms
Chapter 7. Classes
Contents
7.1. Defining Abstract Data Types
7.1.1. Designing the Sales_data Class
7.1.2. Defining the Revised Sales_data Class
7.1.3. Defining Nonmember Class-Related Functions
7.1.4. Constructors
7.1.5. Copy, Assignment, and Destruction
7.2. Access Control and Encapsulation
Using the class or struct Keyword
7.2.1. Friends
7.3. Additional Class Features
7.3.1. Class Members Revisited
7.3.2. Functions That Return *this
7.3.3. Class Types
7.3.4. Friendship Revisited
7.4. Class Scope
Scope and Members Defined outside the Class
7.4.1. Name Lookup and Class Scope
7.5. Constructors Revisited
7.5.1. Constructor Initializer List
7.5.2. Delegating Constructors
7.5.3. The Role of the Default Constructor
7.5.4. Implicit Class-Type Conversions
7.5.5. Aggregate Classes
7.5.6. Literal Classes
7.6. static Class Members
Declaring static Members
Using a Class static Member
Defining static Members
In-Class Initialization of static Data Members
static Members Can Be Used in Ways Ordinary Members Can’t
Chapter Summary
Defined Terms
Part II. The C++ Library
Contents
Chapter 8. The IO Library
Contents
8.1. The IO Classes
Relationships among the IO Types
8.1.1. No Copy or Assign for IO Objects
8.1.2. Condition States
8.1.3. Managing the Output Buffer
8.2. File Input and Output
8.2.1. Using File Stream Objects
8.2.2. File Modes
8.3. string Streams
8.3.1. Using an istringstream
8.3.2. Using ostringstreams
Chapter Summary
Defined Terms
Chapter 9. Sequential Containers
Contents
9.1. Overview of the Sequential Containers
Deciding Which Sequential Container to Use
9.2. Container Library Overview
Constraints on Types That a Container Can Hold
9.2.1. Iterators
9.2.2. Container Type Members
9.2.3. begin and end Members
9.2.4. Defining and Initializing a Container
9.2.5. Assignment and swap
9.2.6. Container Size Operations
9.2.7. Relational Operators
9.3. Sequential Container Operations
9.3.1. Adding Elements to a Sequential Container
9.3.2. Accessing Elements
9.3.3. Erasing Elements
9.3.4. Specialized forward_list Operations
9.3.5. Resizing a Container
9.3.6. Container Operations May Invalidate Iterators
9.4. How a vector Grows
Members to Manage Capacity
capacity and size
9.5. Additional string Operations
9.5.1. Other Ways to Construct strings
9.5.2. Other Ways to Change a string
9.5.3. string Search Operations
9.5.4. The compare Functions
9.5.5. Numeric Conversions
9.6. Container Adaptors
Defining an Adaptor
Stack Adaptor
The Queue Adaptors
Chapter Summary
Defined Terms
Chapter 10. Generic Algorithms
Contents
10.1. Overview
How the Algorithms Work
Iterators Make the Algorithms Container Independent, ...
...But Algorithms Do Depend on Element-Type Operations
10.2. A First Look at the Algorithms
10.2.1. Read-Only Algorithms
10.2.2. Algorithms That Write Container Elements
10.2.3. Algorithms That Reorder Container Elements
10.3. Customizing Operations
10.3.1. Passing a Function to an Algorithm
10.3.2. Lambda Expressions
10.3.3. Lambda Captures and Returns
10.3.4. Binding Arguments
10.4. Revisiting Iterators
10.4.1. Insert Iterators
10.4.2. iostream Iterators
10.4.3. Reverse Iterators
10.5. Structure of Generic Algorithms
10.5.1. The Five Iterator Categories
10.5.2. Algorithm Parameter Patterns
10.5.3. Algorithm Naming Conventions
10.6. Container-Specific Algorithms
The splice Members
The List-Specific Operations Do Change the Containers
Chapter Summary
Defined Terms
Chapter 11. Associative Containers
Contents
11.1. Using an Associative Container
Using a map
Using a set
11.2. Overview of the Associative Containers
11.2.1. Defining an Associative Container
11.2.2. Requirements on Key Type
11.2.3. The pair Type
11.3. Operations on Associative Containers
11.3.1. Associative Container Iterators
11.3.2. Adding Elements
11.3.3. Erasing Elements
11.3.4. Subscripting a map
11.3.5. Accessing Elements
11.3.6. A Word Transformation Map
11.4. The Unordered Containers
Using an Unordered Container
Managing the Buckets
Requirements on Key Type for Unordered Containers
Chapter Summary
Defined Terms
Chapter 12. Dynamic Memory
Contents
12.1. Dynamic Memory and Smart Pointers
12.1.1. The shared_ptr Class
12.1.2. Managing Memory Directly
12.1.3. Using shared_ptrs with new
12.1.4. Smart Pointers and Exceptions
12.1.5. unique_ptr
12.1.6. weak_ptr
12.2. Dynamic Arrays
12.2.1. new and Arrays
12.2.2. The allocator Class
12.3. Using the Library: A Text-Query Program
12.3.1. Design of the Query Program
12.3.2. Defining the Query Program Classes
Chapter Summary
Defined Terms
Part III. Tools for Class Authors
Contents
Chapter 13. Copy Control
Contents
13.1. Copy, Assign, and Destroy
13.1.1. The Copy Constructor
13.1.2. The Copy-Assignment Operator
13.1.3. The Destructor
13.1.4. The Rule of Three/Five
13.1.5. Using = default
13.1.6. Preventing Copies
13.2. Copy Control and Resource Management
13.2.1. Classes That Act Like Values
13.2.2. Defining Classes That Act Like Pointers
13.3. Swap
Writing Our Own swap Function
swap Functions Should Call swap, Not std::swap
Using swap in Assignment Operators
13.4. A Copy-Control Example
The Message Class
The save and remove Members
Copy Control for the Message Class
The Message Destructor
Message Copy-Assignment Operator
A swap Function for Message
13.5. Classes That Manage Dynamic Memory
StrVec Class Design
StrVec Class Definition
Using construct
The alloc_n_copy Member
The free Member
Copy-Control Members
Moving, Not Copying, Elements during Reallocation
Move Constructors and std::move
The reallocate Member
13.6. Moving Objects
13.6.1. Rvalue References
13.6.2. Move Constructor and Move Assignment
13.6.3. Rvalue References and Member Functions
Chapter Summary
Defined Terms
Chapter 14. Overloaded Operations and Conversions
Contents
14.1. Basic Concepts
Calling an Overloaded Operator Function Directly
Some Operators Shouldn’t Be Overloaded
Use Definitions That Are Consistent with the Built-in Meaning
Assignment and Compound Assignment Operators
Choosing Member or Nonmember Implementation
14.2. Input and Output Operators
14.2.1. Overloading the Output Operator <<
14.2.2. Overloading the Input Operator >>
14.3. Arithmetic and Relational Operators
14.3.1. Equality Operators
14.3.2. Relational Operators
14.4. Assignment Operators
Compound-Assignment Operators
14.5. Subscript Operator
14.6. Increment and Decrement Operators
Defining Prefix Increment/Decrement Operators
Differentiating Prefix and Postfix Operators
Calling the Postfix Operators Explicitly
14.7. Member Access Operators
Constraints on the Return from Operator Arrow
14.8. Function-Call Operator
Function-Object Classes with State
14.8.1. Lambdas Are Function Objects
14.8.2. Library-Defined Function Objects
14.8.3. Callable Objects and function
14.9. Overloading, Conversions, and Operators
14.9.1. Conversion Operators
14.9.2. Avoiding Ambiguous Conversions
14.9.3. Function Matching and Overloaded Operators
Chapter Summary
Defined Terms
Chapter 15. Object-Oriented Programming
Contents
15.1. OOP: An Overview
Inheritance
Dynamic Binding
15.2. Defining Base and Derived Classes
15.2.1. Defining a Base Class
15.2.2. Defining a Derived Class
15.2.3. Conversions and Inheritance
15.3. Virtual Functions
Calls to Virtual Functions May Be Resolved at Run Time
Virtual Functions in a Derived Class
The final and override Specifiers
Virtual Functions and Default Arguments
Circumventing the Virtual Mechanism
15.4. Abstract Base Classes
Pure Virtual Functions
Classes with Pure Virtuals Are Abstract Base Classes
A Derived Class Constructor Initializes Its Direct Base Class Only
15.5. Access Control and Inheritance
protected Members
public, private, and protected
Accessibility of Derived-to-Base Conversion
Friendship and Inheritance
Exempting Individual Members
Default Inheritance Protection Levels
15.6. Class Scope under Inheritance
Name Lookup Happens at Compile Time
Name Collisions and Inheritance
Using the Scope Operator to Use Hidden Members
As Usual, Name Lookup Happens before Type Checking
Virtual Functions and Scope
Calling a Hidden Virtual through the Base Class
Overriding Overloaded Functions
15.7. Constructors and Copy Control
15.7.1. Virtual Destructors
15.7.2. Synthesized Copy Control and Inheritance
15.7.3. Derived-Class Copy-Control Members
15.7.4. Inherited Constructors
15.8. Containers and Inheritance
Put (Smart) Pointers, Not Objects, in Containers
15.8.1. Writing a Basket Class
15.9. Text Queries Revisited
15.9.1. An Object-Oriented Solution
15.9.2. The Query_base and Query Classes
15.9.3. The Derived Classes
15.9.4. The eval Functions
Chapter Summary
Defined Terms
Chapter 16. Templates and Generic Programming
Contents
16.1. Defining a Template
16.1.1. Function Templates
16.1.2. Class Templates
16.1.3. Template Parameters
16.1.4. Member Templates
16.1.5. Controlling Instantiations
16.1.6. Efficiency and Flexibility
16.2. Template Argument Deduction
16.2.1. Conversions and Template Type Parameters
16.2.2. Function-Template Explicit Arguments
16.2.3. Trailing Return Types and Type Transformation
16.2.4. Function Pointers and Argument Deduction
16.2.5. Template Argument Deduction and References
16.2.6. Understanding std::move
16.2.7. Forwarding
16.3. Overloading and Templates
Writing Overloaded Templates
Multiple Viable Templates
Nontemplate and Template Overloads
Overloaded Templates and Conversions
Missing Declarations Can Cause the Program to Misbehave
16.4. Variadic Templates
The sizeof... Operator
16.4.1. Writing a Variadic Function Template
16.4.2. Pack Expansion
16.4.3. Forwarding Parameter Packs
16.5. Template Specializations
Defining a Function Template Specialization
Function Overloading versus Template Specializations
Class Template Specializations
Class-Template Partial Specializations
Specializing Members but Not the Class
Chapter Summary
Defined Terms
Part IV. Advanced Topics
Contents
Chapter 17. Specialized Library Facilities
Contents
17.1. The tuple Type
17.1.1. Defining and Initializing tuples
17.1.2. Using a tuple to Return Multiple Values
17.2. The bitset Type
17.2.1. Defining and Initializing bitsets
17.2.2. Operations on bitsets
17.3. Regular Expressions
17.3.1. Using the Regular Expression Library
17.3.2. The Match and Regex Iterator Types
17.3.3. Using Subexpressions
17.3.4. Using regex_replace
17.4. Random Numbers
17.4.1. Random-Number Engines and Distribution
17.4.2. Other Kinds of Distributions
17.5. The IO Library Revisited
17.5.1. Formatted Input and Output
17.5.2. Unformatted Input/Output Operations
17.5.3. Random Access to a Stream
Chapter Summary
Defined Terms
Chapter 18. Tools for Large Programs
Contents
18.1. Exception Handling
18.1.1. Throwing an Exception
18.1.2. Catching an Exception
18.1.3. Function try Blocks and Constructors
18.1.4. The noexcept Exception Specification
18.1.5. Exception Class Hierarchies
18.2. Namespaces
18.2.1. Namespace Definitions
18.2.2. Using Namespace Members
18.2.3. Classes, Namespaces, and Scope
18.2.4. Overloading and Namespaces
18.3. Multiple and Virtual Inheritance
18.3.1. Multiple Inheritance
18.3.2. Conversions and Multiple Base Classes
18.3.3. Class Scope under Multiple Inheritance
18.3.4. Virtual Inheritance
18.3.5. Constructors and Virtual Inheritance
Chapter Summary
Defined Terms
Chapter 19. Specialized Tools and Techniques
Contents
19.1. Controlling Memory Allocation
19.1.1. Overloading new and delete
19.1.2. Placement new Expressions
19.2. Run-Time Type Identification
19.2.1. The dynamic_cast Operator
19.2.2. The typeid Operator
19.2.3. Using RTTI
19.2.4. The type_info Class
19.3. Enumerations
Enumerators
Like Classes, Enumerations Define New Types
Specifying the Size of an enum
Forward Declarations for Enumerations
Parameter Matching and Enumerations
19.4. Pointer to Class Member
19.4.1. Pointers to Data Members
19.4.2. Pointers to Member Functions
19.4.3. Using Member Functions as Callable Objects
19.5. Nested Classes
Declaring a Nested Class
Defining a Nested Class outside of the Enclosing Class
Defining the Members of a Nested Class
Nested-Class static Member Definitions
Name Lookup in Nested Class Scope
The Nested and Enclosing Classes Are Independent
19.6. union: A Space-Saving Class
Defining a union
Using a union Type
Anonymous unions
unions with Members of Class Type
Using a Class to Manage union Members
Managing the Discriminant and Destroying the string
Managing Union Members That Require Copy Control
19.7. Local Classes
Local Classes May Not Use Variables from the Function’s Scope
Normal Protection Rules Apply to Local Classes
Name Lookup within a Local Class
Nested Local Classes
19.8. Inherently Nonportable Features
19.8.1. Bit-fields
19.8.2. volatile Qualifier
19.8.3. Linkage Directives: extern "C"
Chapter Summary
Defined Terms
Appendix A. The Library
Contents
A.1. Library Names and Headers
A.2. A Brief Tour of the Algorithms
A.2.1. Algorithms to Find an Object
A.2.2. Other Read-Only Algorithms
A.2.3. Binary Search Algorithms
A.2.4. Algorithms That Write Container Elements
A.2.5. Partitioning and Sorting Algorithms
A.2.6. General Reordering Operations
A.2.7. Permutation Algorithms
A.2.8. Set Algorithms for Sorted Sequences
A.2.9. Minimum and Maximum Values
A.2.10. Numeric Algorithms
A.3. Random Numbers
A.3.1. Random Number Distributions
A.3.2 Random Number Engines
Index
Add Pages
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
Defined Terms
Next
Next Chapter
Contents
Chapter 18. Tools for Large Programs
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset