You are previewing C++ How to Program, Ninth Edition.
O'Reilly logo
C++ How to Program, Ninth Edition

Book Description

For Introduction to Programming (CS1) and other more intermediate courses covering programming in C++. Also appropriate as a supplement for upper-level courses where the instructor uses a book as a reference for the C++ language.

This best-selling comprehensive text is aimed at readers with little or no programming experience. It teaches programming by presenting the concepts in the context of full working programs and takes an early-objects approach. The authors emphasize achieving program clarity through structured and object-oriented programming, software reuse and component-oriented software construction. The Ninth Edition encourages students to connect computers to the community, using the Internet to solve problems and make a difference in our world. All content has been carefully fine-tuned in response to a team of distinguished academic and industry reviewers.

MyProgrammingLabfor C++ How to Program¿ is a total learning package. MyProgrammingLab is an online homework, tutorial, and assessment program that truly engages students in learning. It helps students better prepare for class, quizzes, and exams–resulting in better performance in the course–and provides educators a dynamic set of tools for gauging individual and class progress. And, MyProgrammingLab comes from Pearson, your partner in providing the best digital learning experience.

Note: If you are purchasing the standalone text or electronic version, MyProgrammingLab does not come automatically packaged with the text. To purchase MyProgrammingLab, please visit: myprogramminglab.com or you can purchase a package of the physical text + MyProgrammingLab by searching the Pearson Higher Education web site.¿ MyProgrammingLab is not a self-paced technology and should only be purchased when required by an instructor.

View the Deitel Buzz online to learn more about the newest publications from the Deitels.

Table of Contents

  1. Title Page
  2. Copyright Page
  3. Deitel® Series Page
    1. How To Program Series
    2. Simply Series
    3. CourseSmart Web Books
    4. Deitel® Developer Series
    5. LiveLessons Video Learning Products
  4. Dedication Page
  5. Contents
  6. Preface
    1. C++11 Standard
    2. Object-Oriented Programming
    3. Pedagogic Features
    4. Other Features
    5. Secure C++ Programming
    6. Online Content
    7. Dependency Chart
    8. Teaching Approach
    9. Obtaining the Software Used in C++ How to Program, 9/e
    10. Instructor Supplements
    11. Online Practice and Assessment with MyProgrammingLab™
    12. Acknowledgments
    13. About the Authors
    14. Corporate Training from Deitel & Associates, Inc.
  7. 1. Introduction to Computers and C++
    1. 1.1. Introduction
    2. 1.2. Computers and the Internet in Industry and Research
    3. 1.3. Hardware and Software
    4. 1.4. Data Hierarchy
    5. 1.5. Machine Languages, Assembly Languages and High-Level Languages
    6. 1.6. C++
    7. 1.7. Programming Languages
    8. 1.8. Introduction to Object Technology
    9. 1.9. Typical C++ Development Environment
    10. 1.10. Test-Driving a C++ Application
    11. 1.11. Operating Systems
    12. 1.12. The Internet and World Wide Web
    13. 1.13. Some Key Software Development Terminology
    14. 1.14. C++11 and the Open Source Boost Libraries
    15. 1.15. Keeping Up to Date with Information Technologies
    16. 1.16. Web Resources
    17. Self-Review Exercises
    18. Answers to Self-Review Exercises
    19. Exercises
    20. Making a Difference
    21. Making a Difference Resources
  8. 2. Introduction to C++ Programming, Input/Output and Operators
    1. 2.1. Introduction
    2. 2.2. First Program in C++: Printing a Line of Text
    3. 2.3. Modifying Our First C++ Program
    4. 2.4. Another C++ Program: Adding Integers
    5. 2.5. Memory Concepts
    6. 2.6. Arithmetic
    7. 2.7. Decision Making: Equality and Relational Operators
    8. 2.8. Wrap-Up
    9. Summary
    10. Self-Review Exercises
    11. Answers to Self-Review Exercises
    12. Exercises
    13. Making a Difference
  9. 3. Introduction to Classes, Objects and Strings
    1. 3.1. Introduction
    2. 3.2. Defining a Class with a Member Function
    3. 3.3. Defining a Member Function with a Parameter
    4. 3.4. Data Members, set Member Functions and get Member Functions
    5. 3.5. Initializing Objects with Constructors
    6. 3.6. Placing a Class in a Separate File for Reusability
    7. 3.7. Separating Interface from Implementation
    8. 3.8. Validating Data with set Functions
    9. 3.9. Wrap-Up
    10. Summary
    11. Self-Review Exercises
    12. Answers to Self-Review Exercises
    13. Exercises
    14. Making a Difference
  10. 4. Control Statements: Part 1; Assignment, ++ and -- Operators
    1. 4.1. Introduction
    2. 4.2. Algorithms
    3. 4.3. Pseudocode
    4. 4.4. Control Structures
    5. 4.5. if Selection Statement
    6. 4.6. if...else Double-Selection Statement
    7. 4.7. while Repetition Statement
    8. 4.8. Formulating Algorithms: Counter-Controlled Repetition
    9. 4.9. Formulating Algorithms: Sentinel-Controlled Repetition
    10. 4.10. Formulating Algorithms: Nested Control Statements
    11. 4.11. Assignment Operators
    12. 4.12. Increment and Decrement Operators
    13. 4.13. Wrap-Up
    14. Summary
    15. Self-Review Exercises
    16. Answers to Self-Review Exercises
    17. Exercises
    18. Making a Difference
  11. 5. Control Statements: Part 2; Logical Operators
    1. 5.1. Introduction
    2. 5.2. Essentials of Counter-Controlled Repetition
    3. 5.3. for Repetition Statement
    4. 5.4. Examples Using the for Statement
    5. 5.5. do...while Repetition Statement
    6. 5.6. switch Multiple-Selection Statement
    7. 5.7. break and continue Statements
    8. 5.8. Logical Operators
    9. 5.9. Confusing the Equality (==) and Assignment (=) Operators
    10. 5.10. Structured Programming Summary
    11. 5.11. Wrap-Up
    12. Summary
    13. Self-Review Exercises
    14. Answers to Self-Review Exercises
    15. Exercises
    16. Making a Difference
  12. 6. Functions and an Introduction to Recursion
    1. 6.1. Introduction
    2. 6.2. Program Components in C++
    3. 6.3. Math Library Functions
    4. 6.4. Function Definitions with Multiple Parameters
    5. 6.5. Function Prototypes and Argument Coercion
    6. 6.6. C++ Standard Library Headers
    7. 6.7. Case Study: Random Number Generation
    8. 6.8. Case Study: Game of Chance; Introducing enum
    9. 6.9. C++11 Random Numbers
    10. 6.10. Storage Classes and Storage Duration
    11. 6.11. Scope Rules
    12. 6.12. Function Call Stack and Activation Records
    13. 6.13. Functions with Empty Parameter Lists
    14. 6.14. Inline Functions
    15. 6.15. References and Reference Parameters
    16. 6.16. Default Arguments
    17. 6.17. Unary Scope Resolution Operator
    18. 6.18. Function Overloading
    19. 6.19. Function Templates
    20. 6.20. Recursion
    21. 6.21. Example Using Recursion: Fibonacci Series
    22. 6.22. Recursion vs. Iteration
    23. 6.23. Wrap-Up
    24. Summary
    25. Self-Review Exercises
    26. Answers to Self-Review Exercises
    27. Exercises
    28. Making a Difference
  13. 7. Class Templates array and vector; Catching Exceptions
    1. 7.1. Introduction
    2. 7.2. arrays
    3. 7.3. Declaring arrays
    4. 7.4. Examples Using arrays
    5. 7.5. Range-Based for Statement
    6. 7.6. Case Study: Class GradeBook Using an array to Store Grades
    7. 7.7. Sorting and Searching arrays
    8. 7.8. Multidimensional arrays
    9. 7.9. Case Study: Class GradeBook Using a Two-Dimensional array
    10. 7.10. Introduction to C++ Standard Library Class Template vector
    11. 7.11. Wrap-Up
    12. Summary
    13. Self-Review Exercises
    14. Answers to Self-Review Exercises
    15. Exercises
    16. Recursion Exercises
    17. Making a Difference
  14. 8. Pointers
    1. 8.1. Introduction
    2. 8.2. Pointer Variable Declarations and Initialization
    3. 8.3. Pointer Operators
    4. 8.4. Pass-by-Reference with Pointers
    5. 8.5. Built-In Arrays
    6. 8.6. Using const with Pointers
    7. 8.7. sizeof Operator
    8. 8.8. Pointer Expressions and Pointer Arithmetic
    9. 8.9. Relationship Between Pointers and Built-In Arrays
    10. 8.10. Pointer-Based Strings
    11. 8.11. Wrap-Up
    12. Summary
    13. Self-Review Exercises
    14. Answers to Self-Review Exercises
    15. Exercises
    16. Special Section: Building Your Own Computer
  15. 9. Classes: A Deeper Look; Throwing Exceptions
    1. 9.1. Introduction
    2. 9.2. Time Class Case Study
    3. 9.3. Class Scope and Accessing Class Members
    4. 9.4. Access Functions and Utility Functions
    5. 9.5. Time Class Case Study: Constructors with Default Arguments
    6. 9.6. Destructors
    7. 9.7. When Constructors and Destructors Are Called
    8. 9.8. Time Class Case Study: A Subtle Trap—Returning a Reference or a Pointer to a private Data Member
    9. 9.9. Default Memberwise Assignment
    10. 9.10. const Objects and const Member Functions
    11. 9.11. Composition: Objects as Members of Classes
    12. 9.12. friend Functions and friend Classes
    13. 9.13. Using the this Pointer
    14. 9.14. static Class Members
    15. 9.15. Wrap-Up
    16. Summary
    17. Self-Review Exercises
    18. Answers to Self-Review Exercises
    19. Exercises
    20. Making a Difference
  16. 10. Operator Overloading; Class string
    1. 10.1. Introduction
    2. 10.2. Using the Overloaded Operators of Standard Library Class string
    3. 10.3. Fundamentals of Operator Overloading
    4. 10.4. Overloading Binary Operators
    5. 10.5. Overloading the Binary Stream Insertion and Stream Extraction Operators
    6. 10.6. Overloading Unary Operators
    7. 10.7. Overloading the Unary Prefix and Postfix ++ and -- Operators
    8. 10.8. Case Study: A Date Class
    9. 10.9. Dynamic Memory Management
    10. 10.10. Case Study: Array Class
    11. 10.11. Operators as Member vs. Non-Member Functions
    12. 10.12. Converting Between Types
    13. 10.13. explicit Constructors and Conversion Operators
    14. 10.14. Overloading the Function Call Operator ()
    15. 10.15. Wrap-Up
    16. Summary
    17. Self-Review Exercises
    18. Answers to Self-Review Exercises
    19. Exercises
  17. 11. Object-Oriented Programming: Inheritance
    1. 11.1. Introduction
    2. 11.2. Base Classes and Derived Classes
    3. 11.3. Relationship between Base and Derived Classes
    4. 11.4. Constructors and Destructors in Derived Classes
    5. 11.5. public, protected and private Inheritance
    6. 11.6. Software Engineering with Inheritance
    7. 11.7. Wrap-Up
    8. Summary
    9. Self-Review Exercises
    10. Answers to Self-Review Exercises
    11. Exercises
  18. 12. Object-Oriented Programming: Polymorphism
    1. 12.1. Introduction
    2. 12.2. Introduction to Polymorphism: Polymorphic Video Game
    3. 12.3. Relationships Among Objects in an Inheritance Hierarchy
    4. 12.4. Type Fields and switch Statements
    5. 12.5. Abstract Classes and Pure virtual Functions
    6. 12.6. Case Study: Payroll System Using Polymorphism
    7. 12.7. (Optional) Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood”
    8. 12.8. Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast, typeid and type_info
    9. 12.9. Wrap-Up
    10. Summary
    11. Self-Review Exercises
    12. Answers to Self-Review Exercises
    13. Exercises
    14. Making a Difference
  19. 13. Stream Input/Output: A Deeper Look
    1. 13.1. Introduction
    2. 13.2. Streams
    3. 13.3. Stream Output
    4. 13.4. Stream Input
    5. 13.5. Unformatted I/O Using read, write and gcount
    6. 13.6. Introduction to Stream Manipulators
    7. 13.7. Stream Format States and Stream Manipulators
    8. 13.8. Stream Error States
    9. 13.9. Tying an Output Stream to an Input Stream
    10. 13.10. Wrap-Up
    11. Summary
    12. Self-Review Exercises
    13. Answers to Self-Review Exercises
    14. Exercises
  20. 14. File Processing
    1. 14.1. Introduction
    2. 14.2. Files and Streams
    3. 14.3. Creating a Sequential File
    4. 14.4. Reading Data from a Sequential File
    5. 14.5. Updating Sequential Files
    6. 14.6. Random-Access Files
    7. 14.7. Creating a Random-Access File
    8. 14.8. Writing Data Randomly to a Random-Access File
    9. 14.9. Reading from a Random-Access File Sequentially
    10. 14.10. Case Study: A Transaction-Processing Program
    11. 14.11. Object Serialization
    12. 14.12. Wrap-Up
    13. Summary
    14. Self-Review Exercises
    15. Answers to Self-Review Exercises
    16. Exercises
    17. Making a Difference
  21. 15. Standard Library Containers and Iterators
    1. 15.1. Introduction
    2. 15.2. Introduction to Containers
    3. 15.3. Introduction to Iterators
    4. 15.4. Introduction to Algorithms
    5. 15.5. Sequence Containers
    6. 15.6. Associative Containers
    7. 15.7. Container Adapters
    8. 15.8. Class bitset
    9. 15.9. Wrap-Up
    10. Summary
    11. Self-Review Exercises
    12. Answers to Self-Review Exercises
    13. Exercises
    14. Recommended Reading
  22. 16. Standard Library Algorithms
    1. 16.1. Introduction
    2. 16.2. Minimum Iterator Requirements
    3. 16.3. Algorithms
    4. 16.4. Function Objects
    5. 16.5. Lambda Expressions
    6. 16.6. Standard Library Algorithm Summary
    7. 16.7. Wrap-Up
    8. Summary
    9. Self-Review Exercises
    10. Answers to Self-Review Exercises
    11. Exercises
  23. 17. Exception Handling: A Deeper Look
    1. 17.1. Introduction
    2. 17.2. Example: Handling an Attempt to Divide by Zero
    3. 17.3. Rethrowing an Exception
    4. 17.4. Stack Unwinding
    5. 17.5. When to Use Exception Handling
    6. 17.6. Constructors, Destructors and Exception Handling
    7. 17.7. Exceptions and Inheritance
    8. 17.8. Processing new Failures
    9. 17.9. Class unique_ptr and Dynamic Memory Allocation
    10. 17.10. Standard Library Exception Hierarchy
    11. 17.11. Wrap-Up
    12. Summary
    13. Self-Review Exercises
    14. Answers to Self-Review Exercises
    15. Exercises
  24. 18. Introduction to Custom Templates
    1. 18.1. Introduction
    2. 18.2. Class Templates
    3. 18.3. Function Template to Manipulate a Class-Template Specialization Object
    4. 18.4. Nontype Parameters
    5. 18.5. Default Arguments for Template Type Parameters
    6. 18.6. Overloading Function Templates
    7. 18.7. Wrap-Up
    8. Summary
    9. Self-Review Exercises
    10. Answers to Self-Review Exercises
    11. Exercises
  25. 19. Custom Templatized Data Structures
    1. 19.1. Introduction
    2. 19.2. Self-Referential Classes
    3. 19.3. Linked Lists
    4. 19.4. Stacks
    5. 19.5. Queues
    6. 19.6. Trees
    7. 19.7. Wrap-Up
    8. Summary
    9. Self-Review Exercises
    10. Answers to Self-Review Exercises
    11. Exercises
    12. Special Section: Building Your Own Compiler
  26. 20. Searching and Sorting
    1. 20.1. Introduction
    2. 20.2. Searching Algorithms
    3. 20.3. Sorting Algorithms
    4. 20.4. Wrap-Up
    5. Summary
    6. Self-Review Exercises
    7. Answers to Self-Review Exercises
    8. Exercises
  27. 21. Class string and String Stream Processing: A Deeper Look
    1. 21.1. Introduction
    2. 21.2. string Assignment and Concatenation
    3. 21.3. Comparing strings
    4. 21.4. Substrings
    5. 21.5. Swapping strings
    6. 21.6. string Characteristics
    7. 21.7. Finding Substrings and Characters in a string
    8. 21.8. Replacing Characters in a string
    9. 21.9. Inserting Characters into a string
    10. 21.10. Conversion to Pointer-Based char * Strings
    11. 21.11. Iterators
    12. 21.12. String Stream Processing
    13. 21.13. C++11 Numeric Conversion Functions
    14. 21.14. Wrap-Up
    15. Summary
    16. Self-Review Exercises
    17. Answers to Self-Review Exercises
    18. Exercises
    19. Making a Difference
  28. 22. Bits, Characters, C Strings and structs
    1. 22.1. Introduction
    2. 22.2. Structure Definitions
    3. 22.3. typedef
    4. 22.4. Example: Card Shuffling and Dealing Simulation
    5. 22.5. Bitwise Operators
    6. 22.6. Bit Fields
    7. 22.7. Character-Handling Library
    8. 22.8. C String-Manipulation Functions
    9. 22.9. C String-Conversion Functions
    10. 22.10. Search Functions of the C String-Handling Library
    11. 22.11. Memory Functions of the C String-Handling Library
    12. 22.12. Wrap-Up
    13. Summary
    14. Self-Review Exercises
    15. Answers to Self-Review Exercises
    16. Exercises
    17. Special Section: Advanced String-Manipulation Exercises
    18. Challenging String-Manipulation Projects
  29. 23. Other Topics
    1. 23.1. Introduction
    2. 23.2. const_cast Operator
    3. 23.3. mutable Class Members
    4. 23.4. namespaces
    5. 23.5. Operator Keywords
    6. 23.6. Pointers to Class Members (.* and ->*)
    7. 23.7. Multiple Inheritance
    8. 23.8. Multiple Inheritance and virtual Base Classes
    9. 23.9. Wrap-Up
    10. Summary
    11. Self-Review Exercises
    12. Answers to Self-Review Exercises
    13. Exercises
  30. Chapters on the Web
  31. A. Operator Precedence and Associativity
  32. B. ASCII Character Set
  33. C. Fundamental Types
  34. D. Number Systems
    1. D.1. Introduction
    2. D.2. Abbreviating Binary Numbers as Octal and Hexadecimal Numbers
    3. D.3. Converting Octal and Hexadecimal Numbers to Binary Numbers
    4. D.4. Converting from Binary, Octal or Hexadecimal to Decimal
    5. D.5. Converting from Decimal to Binary, Octal or Hexadecimal
    6. D.6. Negative Binary Numbers: Two’s Complement Notation
    7. Summary
    8. Self-Review Exercises
    9. Answers to Self-Review Exercises
    10. Exercises
  35. E. Preprocessor
    1. E.1. Introduction
    2. E.2. #include Preprocessing Directive
    3. E.3. #define Preprocessing Directive: Symbolic Constants
    4. E.4. #define Preprocessing Directive: Macros
    5. E.5. Conditional Compilation
    6. E.6. #error and #pragma Preprocessing Directives
    7. E.7. Operators # and ##
    8. E.8. Predefined Symbolic Constants
    9. E.9. Assertions
    10. E.10. Wrap-Up
    11. Summary
    12. Self-Review Exercises
    13. Answers to Self-Review Exercises
    14. Exercises
  36. Appendices on the Web
  37. Index