You are previewing Programming: Principles and Practice Using C++, Second Edition.
O'Reilly logo
Programming: Principles and Practice Using C++, Second Edition

Book Description

An Introduction to Programming by the Inventor of C++

Preparation for Programming in the Real World

The book assumes that you aim eventually to write non-trivial programs, whether for work in software development or in some other technical field.

Focus on Fundamental Concepts and Techniques

The book explains fundamental concepts and techniques in greater depth than traditional introductions. This approach will give you a solid foundation for writing useful, correct, maintainable, and efficient code.

Programming with Today’s C++ (C++11 and C++14)

The book is an introduction to programming in general, including object-oriented programming and generic programming. It is also a solid introduction to the C++ programming language, one of the most widely used languages for real-world software. The book presents modern C++ programming techniques from the start,

introducing the C++ standard library and C++11 and C++14 features to simplify programming tasks.

For Beginners—And Anyone Who Wants to Learn Something New

The book is primarily designed for people who have never programmed before, and it has been tested with many thousands of first-year university students. It has also been extensively used for self-study. Also, practitioners and advanced students have gained new insight and guidance by seeing how a master approaches the elements of his art.

Provides a Broad View

The first half of the book covers a wide range of essential concepts, design and programming techniques, language features, and libraries. Those will enable you to write programs involving input, output, computation, and simple graphics. The second half explores more specialized topics (such as text processing, testing, and the C programming language) and provides abundant reference material. Source code and support supplements are available from the author’s website.

Table of Contents

  1. About This eBook
  2. Title Page
  3. Copyright Page
  4. Contents
  5. Preface
    1. A note to students
    2. A note to teachers
    3. ISO standard C++
    4. Support
    5. Acknowledgments
  6. 0. Notes to the Reader
    1. 0.1 The structure of this book
    2. 0.2 A philosophy of teaching and learning
    3. 0.3 Programming and computer science
    4. 0.4 Creativity and problem solving
    5. 0.5 Request for feedback
    6. 0.6 References
    7. 0.7 Biographies
    8. Bjarne Stroustrup
    9. Lawrence “Pete” Petersen
    10. Postscript
  7. 1. Computers, People, and Programming
    1. 1.1 Introduction
    2. 1.2 Software
    3. 1.3 People
    4. 1.4 Computer science
    5. 1.5 Computers are everywhere
    6. 1.6 Ideals for programmers
    7. Review
    8. Terms
    9. Exercises
    10. Postscript
  8. Part I: The Basics
    1. 2. Hello, World!
      1. 2.1 Programs
      2. 2.2 The classic first program
      3. 2.3 Compilation
      4. 2.4 Linking
      5. 2.5 Programming environments
      6. Drill
      7. Review
      8. Terms
      9. Exercises
      10. Postscript
    2. 3. Objects, Types, and Values
      1. 3.1 Input
      2. 3.2 Variables
      3. 3.3 Input and type
      4. 3.4 Operations and operators
      5. 3.5 Assignment and initialization
      6. 3.6 Composite assignment operators
      7. 3.7 Names
      8. 3.8 Types and objects
      9. 3.9 Type safety
      10. Drill
      11. Review
      12. Terms
      13. Exercises
      14. Postscript
    3. 4. Computation
      1. 4.1 Computation
      2. 4.2 Objectives and tools
      3. 4.3 Expressions
      4. 4.4 Statements
      5. 4.5 Functions
      6. 4.6 vector
      7. 4.7 Language features
      8. Drill
      9. Review
      10. Terms
      11. Exercises
      12. Postscript
    4. 5. Errors
      1. 5.1 Introduction
      2. 5.2 Sources of errors
      3. 5.3 Compile-time errors
      4. 5.4 Link-time errors
      5. 5.5 Run-time errors
      6. 5.6 Exceptions
      7. 5.7 Logic errors
      8. 5.8 Estimation
      9. 5.9 Debugging
      10. 5.10 Pre- and post-conditions
      11. 5.11 Testing
      12. Drill
      13. Review
      14. Terms
      15. Exercises
      16. Postscript
    5. 6. Writing a Program
      1. 6.1 A problem
      2. 6.2 Thinking about the problem
      3. 6.3 Back to the calculator!
      4. 6.4 Grammars
      5. 6.5 Turning a grammar into code
      6. 6.6 Trying the first version
      7. 6.7 Trying the second version
      8. 6.8 Token streams
      9. 6.9 Program structure
      10. Drill
      11. Review
      12. Terms
      13. Exercises
      14. Postscript
    6. 7. Completing a Program
      1. 7.1 Introduction
      2. 7.2 Input and output
      3. 7.3 Error handling
      4. 7.4 Negative numbers
      5. 7.5 Remainder: %
      6. 7.6 Cleaning up the code
      7. 7.7 Recovering from errors
      8. 7.8 Variables
      9. Drill
      10. Review
      11. Terms
      12. Exercises
      13. Postscript
    7. 8. Technicalities: Functions, etc.
      1. 8.1 Technicalities
      2. 8.2 Declarations and definitions
      3. 8.3 Header files
      4. 8.4 Scope
      5. 8.5 Function call and return
      6. 8.6 Order of evaluation
      7. 8.7 Namespaces
      8. Drill
      9. Review
      10. Terms
      11. Exercises
      12. Postscript
    8. 9. Technicalities: Classes, etc.
      1. 9.1 User-defined types
      2. 9.2 Classes and members
      3. 9.3 Interface and implementation
      4. 9.4 Evolving a class
      5. 9.5 Enumerations
      6. 9.6 Operator overloading
      7. 9.7 Class interfaces
      8. 9.8 The Date class
      9. Drill
      10. Review
      11. Terms
      12. Exercises
      13. Postscript
  9. Part II: Input and Output
    1. 10. Input and Output Streams
      1. 10.1 Input and output
      2. 10.2 The I/O stream model
      3. 10.3 Files
      4. 10.4 Opening a file
      5. 10.5 Reading and writing a file
      6. 10.6 I/O error handling
      7. 10.7 Reading a single value
      8. 10.8 User-defined output operators
      9. 10.9 User-defined input operators
      10. 10.10 A standard input loop
      11. 10.11 Reading a structured file
      12. Drill
      13. Review
      14. Terms
      15. Exercises
      16. Postscript
    2. 11. Customizing Input and Output
      1. 11.1 Regularity and irregularity
      2. 11.2 Output formatting
      3. 11.3 File opening and positioning
      4. 11.4 String streams
      5. 11.5 Line-oriented input
      6. 11.6 Character classification
      7. 11.7 Using nonstandard separators
      8. 11.8 And there is so much more
      9. Drill
      10. Review
      11. Terms
      12. Exercises
      13. Postscript
    3. 12. A Display Model
      1. 12.1 Why graphics?
      2. 12.2 A display model
      3. 12.3 A first example
      4. 12.4 Using a GUI library
      5. 12.5 Coordinates
      6. 12.6 Shapes
      7. 12.7 Using Shape primitives
      8. 12.8 Getting this to run
      9. Drill
      10. Review
      11. Terms
      12. Exercises
      13. Postscript
    4. 13. Graphics Classes
      1. 13.1 Overview of graphics classes
      2. 13.2 Point and Line
      3. 13.3 Lines
      4. 13.4 Color
      5. 13.5 Line_style
      6. 13.6 Open_polyline
      7. 13.7 Closed_polyline
      8. 13.8 Polygon
      9. 13.9 Rectangle
      10. 13.10 Managing unnamed objects
      11. 13.11 Text
      12. 13.12 Circle
      13. 13.13 Ellipse
      14. 13.14 Marked_polyline
      15. 13.15 Marks
      16. 13.16 Mark
      17. 13.17 Images
      18. Drill
      19. Review
      20. Terms
      21. Exercises
      22. Postscript
    5. 14. Graphics Class Design
      1. 14.1 Design principles
      2. 14.2 Shape
      3. 14.3 Base and derived classes
      4. 14.4 Benefits of object-oriented programming
      5. Drill
      6. Review
      7. Terms
      8. Exercises
      9. Postscript
    6. 15. Graphing Functions and Data
      1. 15.1 Introduction
      2. 15.2 Graphing simple functions
      3. 15.3 Function
      4. 15.4 Axis
      5. 15.5 Approximation
      6. 15.6 Graphing data
      7. Drill
      8. Review
      9. Terms
      10. Exercises
      11. Postscript
    7. 16. Graphical User Interfaces
      1. 16.1 User interface alternatives
      2. 16.2 The “Next” button
      3. 16.3 A simple window
      4. 16.4 Button and other Widgets
      5. 16.5 An example
      6. 16.6 Control inversion
      7. 16.7 Adding a menu
      8. 16.8 Debugging GUI code
      9. Drill
      10. Review
      11. Terms
      12. Exercises
      13. Postscript
  10. Part III: Data and Algorithms
    1. 17. Vector and Free Store
      1. 17.1 Introduction
      2. 17.2 vector basics
      3. 17.3 Memory, addresses, and pointers
      4. 17.4 Free store and pointers
      5. 17.5 Destructors
      6. 17.6 Access to elements
      7. 17.7 Pointers to class objects
      8. 17.8 Messing with types: void* and casts
      9. 17.9 Pointers and references
      10. 17.10 The this pointer
      11. Drill
      12. Review
      13. Terms
      14. Exercises
      15. Postscript
    2. 18. Vectors and Arrays
      1. 18.1 Introduction
      2. 18.2 Initialization
      3. 18.3 Copying
      4. 18.4 Essential operations
      5. 18.5 Access to vector elements
      6. 18.6 Arrays
      7. 18.7 Examples: palindrome
      8. Drill
      9. Review
      10. Terms
      11. Exercises
      12. Postscript
    3. 19. Vector, Templates, and Exceptions
      1. 19.1 The problems
      2. 19.2 Changing size
      3. 19.3 Templates
      4. 19.4 Range checking and exceptions
      5. 19.5 Resources and exceptions
      6. Drill
      7. Review
      8. Terms
      9. Exercises
      10. Postscript
    4. 20. Containers and Iterators
      1. 20.1 Storing and processing data
      2. 20.2 STL ideals
      3. 20.3 Sequences and iterators
      4. 20.4 Linked lists
      5. 20.5 Generalizing vector yet again
      6. 20.6 An example: a simple text editor
      7. 20.7 vector, list, and string
      8. 20.8 Adapting our vector to the STL
      9. 20.9 Adapting built-in arrays to the STL
      10. 20.10 Container overview
      11. Drill
      12. Review
      13. Terms
      14. Exercises
      15. Postscript
    5. 21. Algorithms and Maps
      1. 21.1 Standard library algorithms
      2. 21.2 The simplest algorithm: find()
      3. 21.3 The general search: find_if()
      4. 21.4 Function objects
      5. 21.5 Numerical algorithms
      6. 21.6 Associative containers
      7. 21.7 Copying
      8. 21.8 Sorting and searching
      9. 21.9 Container algorithms
      10. Drill
      11. Review
      12. Terms
      13. Exercises
      14. Postscript
  11. Part IV: Broadening the View
    1. 22. Ideals and History
      1. 22.1 History, ideals, and professionalism
      2. 22.2 Programming language history overview
      3. Review
      4. Terms
      5. Exercises
      6. Postscript
    2. 23. Text Manipulation
      1. 23.1 Text
      2. 23.2 Strings
      3. 23.3 I/O streams
      4. 23.4 Maps
      5. 23.5 A problem
      6. 23.6 The idea of regular expressions
      7. 23.7 Searching with regular expressions
      8. 23.8 Regular expression syntax
      9. 23.9 Matching with regular expressions
      10. 23.10 References
      11. Drill
      12. Review
      13. Terms
      14. Exercises
      15. Postscript
    3. 24. Numerics
      1. 24.1 Introduction
      2. 24.2 Size, precision, and overflow
      3. 24.3 Arrays
      4. 24.4 C-style multidimensional arrays
      5. 24.5 The Matrix library
      6. 24.6 An example: solving linear equations
      7. 24.7 Random numbers
      8. 24.8 The standard mathematical functions
      9. 24.9 Complex numbers
      10. 24.10 References
      11. Drill
      12. Review
      13. Terms
      14. Exercises
      15. Postscript
    4. 25. Embedded Systems Programming
      1. 25.1 Embedded systems
      2. 25.2 Basic concepts
      3. 25.3 Memory management
      4. 25.4 Addresses, pointers, and arrays
      5. 25.5 Bits, bytes, and words
      6. 25.6 Coding standards
      7. Drill
      8. Review
      9. Terms
      10. Exercises
      11. Postscript
    5. 26. Testing
      1. 26.1 What we want
      2. 26.2 Proofs
      3. 26.3 Testing
      4. 26.4 Design for testing
      5. 26.5 Debugging
      6. 26.6 Performance
      7. 26.7 References
      8. Drill
      9. Review
      10. Terms
      11. Exercises
      12. Postscript
    6. 27. The C Programming Language
      1. 27.1 C and C++: siblings
      2. References
      3. 27.2 Functions
      4. 27.3 Minor language differences
      5. 27.4 Free store
      6. 27.5 C-style strings
      7. 27.6 Input/output: stdio
      8. 27.7 Constants and macros
      9. 27.8 Macros
      10. 27.9 An example: intrusive containers
      11. Drill
      12. Review
      13. Terms
      14. Exercises
      15. Postscript
  12. Part V: Appendices
    1. A. Language Summary
      1. A.1 General
      2. A.2 Literals
      3. A.3 Identifiers
      4. A.4 Scope, storage class, and lifetime
      5. A.5 Expressions
      6. A.6 Statements
      7. A.7 Declarations
      8. A.8 Built-in types
      9. A.9 Functions
      10. A.10 User-defined types
      11. A.11 Enumerations
      12. A.12 Classes
      13. A.13 Templates
      14. A.14 Exceptions
      15. A.15 Namespaces
      16. A.16 Aliases
      17. A.17 Preprocessor directives
    2. B. Standard Library Summary
      1. B.1 Overview
      2. B.2 Error handling
      3. B.3 Iterators
      4. B.4 Containers
      5. B.5 Algorithms
      6. B.6 STL utilities
      7. B.7 I/O streams
      8. B.8 String manipulation
      9. B.9 Numerics
      10. B.10 Time
      11. B.11 C standard library functions
      12. B.12 Other libraries
    3. C. Getting Started with Visual Studio
      1. C.1 Getting a program to run
      2. C.2 Installing Visual Studio
      3. C.3 Creating and running a program
      4. C.4 Later
    4. D. Installing FLTK
      1. D.1 Introduction
      2. D.2 Downloading FLTK
      3. D.3 Installing FLTK
      4. D.4 Using FLTK in Visual Studio
      5. D.5 Testing if it all worked
    5. E. GUI Implementation
      1. E.1 Callback implementation
      2. E.2 Widget implementation
      3. E.3 Window implementation
      4. E.4 Vector_ref
      5. E.5 An example: manipulating Widgets
  13. Glossary
  14. Bibliography
  15. Index
  16. Photo Citations and Credits