Cover image for Practical C++ Programming, 2nd Edition

Book description

C++ is a powerful, highly flexible, and adaptable programminglanguage that allows software engineers to organize and processinformation quickly and effectively. But this high-level languageis relatively difficult to master, even if you already know the Cprogramming language. The 2nd edition of Practical C++Programming is a complete introduction to the C++ language forprogrammers who are learning C++. Reflecting the latest changes tothe C++ standard, this 2nd edition takes a useful down-to-earthapproach, placing a strong emphasis on how to design clean, elegantcode. In short, to-the-point chapters, all aspects of programmingare covered including style, software engineering, programmingdesign, object-oriented design, and debugging. It also coverscommon mistakes and how to find (and avoid) them. End of chapterexercises help you ensure you've mastered the material. PracticalC++ Programming thoroughly covers:

  • C++ Syntax

  • Coding standards and style

  • Creation and use of object classes

  • Templates

  • Debugging and optimization

  • Use of the C++ preprocessor

  • File input/output

Steve Oualline's clear, easy-going writing style and hands-onapproach to learning make Practical C++ Programming a nearlypainless way to master this complex but powerful programminglanguage.

Table of Contents

  1. Practical C++ Programming
  2. A Note Regarding Supplemental Files
  3. Preface
    1. Scope of This Handbook
    2. How This Book Is Organized
    3. How to Read This Book If You Already Know C
    4. Font Conventions
    5. How to Contact Us
    6. Acknowledgments for the First Edition
    7. Acknowledgments for the Second Edition
  4. I. The Basics
    1. 1. What Is C++?
      1. 1.1. A Brief History of C++
      2. 1.2. C++ Organization
      3. 1.3. How to Learn C++
    2. 2. The Basics of Program Writing
      1. 2.1. Programs from Conception to Execution
      2. 2.2. Creating a Real Program
        1. 2.2.1. Creating a Program Using a Command-Line Compiler
          1. 2.2.1.1. Step 1: Create a place for your program
          2. 2.2.1.2. Step 2: Create the program
          3. 2.2.1.3. Step 3: Run the compiler
            1. 2.2.1.3.1. Unix CC Compiler (Generic Unix)
            2. 2.2.1.3.2. Free Software Foundation's g++ Compiler
            3. 2.2.1.3.3. Borland's Turbo C++
            4. 2.2.1.3.4. Microsoft Visual C++ .NET
          4. 2.2.1.4. Step 4: Execute the program
        2. 2.2.2. Creating a Program Using an Integrated Development Environment
          1. 2.2.2.1. Borland C++
          2. 2.2.2.2. Microsoft Visual C++
      3. 2.3. Getting Help in Unix
      4. 2.4. Getting Help in an IDE
      5. 2.5. Programming Exercises
    3. 3. Style
      1. 3.1. Comments
      2. 3.2. C++ Code
      3. 3.3. Naming Style
      4. 3.4. Coding Religion
      5. 3.5. Indentation and Code Format
      6. 3.6. Clarity
      7. 3.7. Simplicity
      8. 3.8. Consistency and Organization
      9. 3.9. Further Reading
      10. 3.10. Summary
    4. 4. Basic Declarations and Expressions
      1. 4.1. Basic Program Structure
      2. 4.2. Simple Expressions
      3. 4.3. The std::cout Output Object
      4. 4.4. Variables and Storage
      5. 4.5. Variable Declarations
      6. 4.6. Integers
      7. 4.7. Assignment Statements
      8. 4.8. Floating-Point Numbers
      9. 4.9. Floating-Point Divide Versus Integer Divide
      10. 4.10. Characters
      11. 4.11. Wide Characters
      12. 4.12. Boolean Type
      13. 4.13. Programming Exercises
      14. 4.14. Answers to Chapter Questions
    5. 5. Arrays, Qualifiers, and Reading Numbers
      1. 5.1. Arrays
      2. 5.2. Strings
        1. 5.2.1. Wide Strings
      3. 5.3. Reading Data
      4. 5.4. Initializing Variables
        1. 5.4.1. Bounds Errors
      5. 5.5. Multidimensional Arrays
      6. 5.6. C-Style Strings
        1. 5.6.1. Safety and C Strings
        2. 5.6.2. Reading C-Style Strings
        3. 5.6.3. Converting Between C-Style and C++ Strings
        4. 5.6.4. The Differences Between C++ and C-Style Strings
      7. 5.7. Types of Integers
        1. 5.7.1. Summary of Integer Types
      8. 5.8. Types of Floats
      9. 5.9. Constant and Reference Declarations
      10. 5.10. Qualifiers
        1. 5.10.1. Special
        2. 5.10.2. Constant
        3. 5.10.3. Storage Class
        4. 5.10.4. Size
        5. 5.10.5. Sign
        6. 5.10.6. Type
      11. 5.11. Hexadecimal and Octal Constants
      12. 5.12. Operators for Performing Shortcuts
      13. 5.13. Side Effects
      14. 5.14. Programming Exercises
      15. 5.15. Answers to Chapter Questions
    6. 6. Decision and Control Statements
      1. 6.1. if Statement
      2. 6.2. else Statement
      3. 6.3. How Not to Use std::strcmp
      4. 6.4. Looping Statements
      5. 6.5. while Statement
      6. 6.6. break Statement
      7. 6.7. continue Statement
      8. 6.8. The Assignment Anywhere Side Effect
      9. 6.9. Programming Exercises
      10. 6.10. Answers to Chapter Questions
    7. 7. The Programming Process
      1. 7.1. Setting Up Your Work Area
      2. 7.2. The Specification
      3. 7.3. Code Design
      4. 7.4. The Prototype
      5. 7.5. The Makefile
      6. 7.6. Testing
      7. 7.7. Debugging
      8. 7.8. Maintenance
      9. 7.9. Revisions
      10. 7.10. Electronic Archaeology
      11. 7.11. Mark Up the Program
      12. 7.12. Use the Debugger
      13. 7.13. Use the Text Editor as a Browser
      14. 7.14. Add Comments
      15. 7.15. Programming Exercises
  5. II. Simple Programming
    1. 8. More Control Statements
      1. 8.1. for Statement
      2. 8.2. switch Statement
      3. 8.3. switch, break, and continue
      4. 8.4. Programming Exercises
      5. 8.5. Answers to Chapter Questions
    2. 9. Variable Scope and Functions
      1. 9.1. Scope and Storage Class
        1. 9.1.1. The for Scope
      2. 9.2. Namespaces
        1. 9.2.1. Namespace std
        2. 9.2.2. Global Namespace
        3. 9.2.3. File-Specific Namespace
        4. 9.2.4. Nested Namespaces
        5. 9.2.5. The using Statement
          1. 9.2.5.1. The problem with the using statement
      3. 9.3. Functions
        1. 9.3.1. Returning void
        2. 9.3.2. Namespaces and Functions
        3. 9.3.3. const Parameters and Return Values
        4. 9.3.4. Reference Parameters and Return Values
        5. 9.3.5. Dangling References
        6. 9.3.6. Array Parameters
        7. 9.3.7. Function Overloading
        8. 9.3.8. Default Arguments
        9. 9.3.9. Unused Parameters
        10. 9.3.10. Inline Functions
      4. 9.4. Summary of Parameter Types
      5. 9.5. Recursion
      6. 9.6. Structured Programming Basics
      7. 9.7. Real-World Programming
      8. 9.8. Programming Exercises
      9. 9.9. Answers to Chapter Questions
    3. 10. The C++ Preprocessor
      1. 10.1. #define Statement
        1. 10.1.1. #define Versus const
      2. 10.2. Conditional Compilation
      3. 10.3. #include Files
      4. 10.4. Parameterized Macros
        1. 10.4.1. The # Operator
        2. 10.4.2. Parameterized Macros Versus Inline Functions
      5. 10.5. Advanced Features
      6. 10.6. Summary
      7. 10.7. Programming Exercises
      8. 10.8. Answers to Chapter Questions
    4. 11. Bit Operations
      1. 11.1. Bit Operators
      2. 11.2. The AND Operator (&)
      3. 11.3. Bitwise OR (|)
      4. 11.4. The Bitwise Exclusive OR (^)
      5. 11.5. The Ones Complement Operator (NOT) (~)
      6. 11.6. The Left and Right Shift Operators (<<, >>)
        1. 11.6.1. Right Shift Details
      7. 11.7. Setting, Clearing, and Testing Bits
      8. 11.8. Bitmapped Graphics
      9. 11.9. Programming Exercises
      10. 11.10. Answers to Chapter Questions
  6. III. Advanced Types and Classes
    1. 12. Advanced Types
      1. 12.1. Structures
      2. 12.2. Unions
      3. 12.3. typedef
      4. 12.4. enum Type
      5. 12.5. Bit Members or Packed Structures
      6. 12.6. Arrays of Structures
      7. 12.7. Programming Exercises
      8. 12.8. Answers to Chapter Questions
    2. 13. Simple Classes
      1. 13.1. Stacks
        1. 13.1.1. Designing a Stack
      2. 13.2. Improved Stack
      3. 13.3. Using a Class
      4. 13.4. Introduction to Constructors and Destructors
        1. 13.4.1. Destructors
        2. 13.4.2. Parameterized Constructors
        3. 13.4.3. Parameterized Destructors
        4. 13.4.4. Copy Constructor
      5. 13.5. Automatically Generated Member Functions
        1. 13.5.1. Automatically Generated and Used Functions
        2. 13.5.2. Explicit Constructors
      6. 13.6. Shortcuts
      7. 13.7. Style
      8. 13.8. Structures Versus Classes
      9. 13.9. Programming Exercises
    3. 14. More on Classes
      1. 14.1. Friends
        1. 14.1.1. Friend Functions
        2. 14.1.2. Friend Classes
      2. 14.2. Constant Functions
      3. 14.3. Constant Members
      4. 14.4. Static Member Variables
      5. 14.5. Static Member Functions
      6. 14.6. The Meaning of static
      7. 14.7. Programming Exercises
    4. 15. Simple Pointers
      1. 15.1. const Pointers
      2. 15.2. Pointers and Printing
      3. 15.3. Pointers and Arrays
        1. 15.3.1. Splitting a C-Style String
      4. 15.4. The reinterpret_cast
      5. 15.5. Pointers and Structures
      6. 15.6. Command-Line Arguments
      7. 15.7. Programming Exercises
      8. 15.8. Answers to Chapter Questions
  7. IV. Advanced Programming Concepts
    1. 16. File Input/Output
      1. 16.1. C++ File I/O
        1. 16.1.1. Reading C-Style Strings
        2. 16.1.2. Output Files
      2. 16.2. Conversion Routines
      3. 16.3. Binary and ASCII Files
      4. 16.4. The End-of-Line Puzzle
      5. 16.5. Binary I/O
      6. 16.6. Buffering Problems
      7. 16.7. Unbuffered I/O
      8. 16.8. Designing File Formats
      9. 16.9. C-Style I/O Routines
      10. 16.10. C-Style Conversion Routines
        1. 16.10.1. The std::printf Family of Output Functions
        2. 16.10.2. The std::scanf Family of Input Functions
      11. 16.11. C-Style Binary I/O
      12. 16.12. C- Versus C++- Style I/O
        1. 16.12.1. Simplicity
        2. 16.12.2. Reliability
        3. 16.12.3. Speed
        4. 16.12.4. Which Should You Use?
      13. 16.13. Programming Exercises
      14. 16.14. Answers to Chapter Questions
    2. 17. Debugging and Optimization
      1. 17.1. Code Reviews
        1. 17.1.1. Planning the Review
        2. 17.1.2. The Review Meeting
        3. 17.1.3. Why People Don't Do Code Reviews
        4. 17.1.4. Metrics
      2. 17.2. Serial Debugging
        1. 17.2.1. Divide and Conquer
        2. 17.2.2. The Confessional Method of Debugging
        3. 17.2.3. Debug-Only Code
        4. 17.2.4. Debug Command-Line Switch
      3. 17.3. Going Through the Output
      4. 17.4. Interactive Debuggers
        1. 17.4.1. Basic Debugging Commands
        2. 17.4.2. Debugging a Simple Program
      5. 17.5. Debugging a Binary Search
        1. 17.5.1. The First Bug, a Segmentation Fault
        2. 17.5.2. The Unintended Infinite Loop
      6. 17.6. Interactive Debugging Tips and Tricks
      7. 17.7. Runtime Errors
      8. 17.8. Optimization
        1. 17.8.1. Profiling
        2. 17.8.2. Analyzing and Optimizing code
        3. 17.8.3. Register Declarations
          1. 17.8.3.1. Loop ordering
          2. 17.8.3.2. The power of powers of 2
          3. 17.8.3.3. Making use of pointers
        4. 17.8.4. Using the System Library
      9. 17.9. How to Optimize
      10. 17.10. Case Study: Inline Functions Versus Normal Functions
      11. 17.11. Case Study: Optimizing a Color-Rendering Algorithm
      12. 17.12. Programming Exercises
      13. 17.13. Answers to Chapter Questions
    3. 18. Operator Overloading
      1. 18.1. Creating a Simple Fixed-Point Class
        1. 18.1.1. Fixed Point Basics
        2. 18.1.2. Creating the fixed_pt Class
      2. 18.2. Operator Functions
        1. 18.2.1. Binary Arithmetic Operators
        2. 18.2.2. Relational Operators
        3. 18.2.3. Unary Operators
        4. 18.2.4. Shortcut Operators
        5. 18.2.5. Increment and Decrement Operators
        6. 18.2.6. Logical Operators
        7. 18.2.7. I/O Operators
        8. 18.2.8. Index Operator "[ ]"
        9. 18.2.9. new and delete
        10. 18.2.10. Exotic Operators
      3. 18.3. Operator Member Functions
        1. 18.3.1. Casting
      4. 18.4. Warts
      5. 18.5. Full Definition of the Fixed-Point Class
      6. 18.6. Programming Exercises
      7. 18.7. Answers to Chapter Questions
    4. 19. Floating Point
      1. 19.1. Floating-Point Format
      2. 19.2. Floating Addition/Subtraction
      3. 19.3. Multiplication and Division
      4. 19.4. Overflow and Underflow
      5. 19.5. Roundoff Error
      6. 19.6. Accuracy
      7. 19.7. Minimizing Roundoff Error
      8. 19.8. Determining Accuracy
      9. 19.9. Precision and Speed
      10. 19.10. Power Series
      11. 19.11. Programming Exercises
    5. 20. Advanced Pointers
      1. 20.1. Pointers, Structures, and Classes
      2. 20.2. delete Operator
      3. 20.3. Linked Lists
      4. 20.4. Ordered Linked Lists
      5. 20.5. Doubly Linked Lists
      6. 20.6. Trees
      7. 20.7. Printing a Tree
      8. 20.8. The Rest of the Program
      9. 20.9. Data Structures for a Chess Program
      10. 20.10. Programming Exercises
      11. 20.11. Answers to Chapter Questions
    6. 21. Advanced Classes
      1. 21.1. Derived Classes
      2. 21.2. Virtual Functions
      3. 21.3. Virtual Classes
      4. 21.4. Function Hiding in Derived Classes
      5. 21.5. Constructors and Destructors in Derived Classes
      6. 21.6. The dynamic_cast Operator
      7. 21.7. Summary
      8. 21.8. Programming Exercises
      9. 21.9. Answers to Chapter Questions
  8. V. Other Language Features
    1. 22. Exceptions
      1. 22.1. Adding Exceptions to the Stack Class
        1. 22.1.1. Creating an Exception
        2. 22.1.2. Using a Try Block for Normal Execution
        3. 22.1.3. Throwing an Exception
        4. 22.1.4. Exceptions and Destructors
      2. 22.2. Exceptions Versus assert
      3. 22.3. Programming Exercises
    2. 23. Modular Programming
      1. 23.1. Modules
      2. 23.2. Public and Private
      3. 23.3. The extern Storage Class
      4. 23.4. Headers
      5. 23.5. The Body of the Module
      6. 23.6. A Program to Use Infinite Arrays
      7. 23.7. The Makefile for Multiple Files
      8. 23.8. Using the Infinite Array
      9. 23.9. Dividing a Task into Modules
      10. 23.10. Module Design Guidelines
      11. 23.11. Programming Exercises
    3. 24. Templates
      1. 24.1. What Is a Template?
      2. 24.2. Templates: The Hard Way
      3. 24.3. Templates: The C++ Way
      4. 24.4. Function Specialization
      5. 24.5. Class Templates
      6. 24.6. Class Specialization
      7. 24.7. Implementation Details
        1. 24.7.1. Real-World Templates
        2. 24.7.2. When to Generate Code
        3. 24.7.3. Writing Portable Templates
      8. 24.8. Advanced Features
        1. 24.8.1. Default Parameters
        2. 24.8.2. Partial Specialization
      9. 24.9. Summary
      10. 24.10. Programming Exercises
    4. 25. Standard Template Library
      1. 25.1. STL Basics
        1. 25.1.1. Containers
        2. 25.1.2. Iterators
        3. 25.1.3. Algorithms
      2. 25.2. Class List—A Set of Students
        1. 25.2.1. Iterating Through a Set
        2. 25.2.2. Using std::foreach to Write Out the Set
        3. 25.2.3. Multisets
      3. 25.3. Creating a Waiting List with the STL List
      4. 25.4. Storing Grades in a STL Map
      5. 25.5. Putting It All Together
      6. 25.6. Practical Considerations When Using the STL
        1. 25.6.1. Getting the Types Right
        2. 25.6.2. Error Messages
      7. 25.7. Getting More Information
      8. 25.8. Exercises
    5. 26. Program Design
      1. 26.1. Design Goals
      2. 26.2. Design Factors
      3. 26.3. Design Principles
      4. 26.4. Coding
        1. 26.4.1. Procedure Design
          1. 26.4.1.1. Procedure interface
          2. 26.4.1.2. Global variables
          3. 26.4.1.3. Information hiding
          4. 26.4.1.4. Coding details
        2. 26.4.2. Modules and Structured Programming
          1. 26.4.2.1. Interconnections
        3. 26.4.3. Real-Life Module Organization
        4. 26.4.4. Module Summary
      5. 26.5. Objects
        1. 26.5.1. Interfaces and C++ Classes
      6. 26.6. Real-World Design Techniques
        1. 26.6.1. The Linked List Problem
        2. 26.6.2. Callbacks
        3. 26.6.3. Decoupling the Interface and Implementation
      7. 26.7. Conclusion
    6. 27. Putting It All Together
      1. 27.1. Requirements
      2. 27.2. Code Design
        1. 27.2.1. Token Module
        2. 27.2.2. Character-Type Module
        3. 27.2.3. Statistics Class
      3. 27.3. Coding
      4. 27.4. Functional Description
        1. 27.4.1. char_type Class
        2. 27.4.2. input_file Class
        3. 27.4.3. token Class
        4. 27.4.4. stat Class
        5. 27.4.5. line_counter Class
        6. 27.4.6. brace_counter Class
        7. 27.4.7. paren_counter Class
        8. 27.4.8. comment_counter Class
        9. 27.4.9. do_file Procedure
      5. 27.5. Testing
      6. 27.6. Revisions
      7. 27.7. A Final Warning
      8. 27.8. Program Files
      9. 27.9. Programming Exercises
    7. 28. From C to C++
      1. 28.1. K&R-Style Functions
        1. 28.1.1. Prototypes
      2. 28.2. struct
      3. 28.3. malloc and free
        1. 28.3.1. The C malloc function
        2. 28.3.2. The C free function
      4. 28.4. Turning Structures into Classes
      5. 28.5. setjmp and longjmp
      6. 28.6. Mixing C and C++ Code
      7. 28.7. Summary
      8. 28.8. Programming Exercise
    8. 29. C++'s Dustier Corners
      1. 29.1. do/while
      2. 29.2. goto
      3. 29.3. The ?: Construct
      4. 29.4. The Comma Operator
      5. 29.5. Overloading the ( ) Operator
      6. 29.6. Pointers to Members
      7. 29.7. The asm Statement
      8. 29.8. The mutable Qualifier
      9. 29.9. Run Time Type Identification
      10. 29.10. Trigraphs
      11. 29.11. Answers to Chapter Questions
    9. 30. Programming Adages
      1. 30.1. General
      2. 30.2. Design
      3. 30.3. Declarations
      4. 30.4. switch Statement
      5. 30.5. Preprocessor
      6. 30.6. Style
      7. 30.7. Compiling
      8. 30.8. The Ten Commandments for C++ Programmers
      9. 30.9. Final Note
      10. 30.10. Answers to Chapter Questions
  9. VI. Appendixes
    1. A. ASCII Table
    2. B. Ranges
    3. C. Operator Precedence Rules
      1. C.1. Standard Rules
      2. C.2. Practical Subset of the Operator Precedence Rules
    4. D. Computing Sine Using a Power Series
    5. E. Resources
      1. E.1. Compilers
      2. E.2. Standard Template Library
      3. E.3. Standards
      4. E.4. Programming Tools
  10. Index
  11. About the Author
  12. Colophon
  13. Copyright