
1. Basics
- 1.1. Software Development Paradigms and Processes
- 1.2. Programming Languages and Paradigms
- 1.3. Object-Oriented Programming Review: C++ vs. Java
- 1.4. Machine Code
- 1.5. C++ Compiler Operation
- 1.6. Terminology
- 1.7. Program Data
- C++ fundamental, built-in, primitive data types
- Constant examples
- The characteristics of a variable
- Variable definition examples
- Context sensitive interpretation
- Houses on a street as a metaphor for variables in memory
- Variable scoping rules
- C++ variable initialization syntax
- Variable initialization examples
- Variables analogized as a drinking glass
- Creating symbolic constants with const
- Type promotion examples
- Type deduction examples
- Identifier Rules
- 1.7.1. The Swapping Problem (1)
- 1.8. C++ Console Input / Output
- 1.9. Comments
- 1.10. The First C++ Program
- 1.10.1. Hello, World!
- 1.10.2. Programming With An IDE
- 1.10.3. Visual Studio Demonstration
2. Core Operations
- 2.1. Operators and Operands
- 2.2. Common Operators
- 2.3. Common Operator Examples
- 2.3.1. ftoc.cpp
- 2.3.2. money.cpp
- 2.4. Program Termination: exit and return
- 2.5. Math Library: Functions and Constants
- 2.6. Converting Formulas to C++ Statements
- 2.7. Math Library Examples
- 2.7.1. circle.cpp
- 2.7.2. hypot.cpp
- 2.7.3. payment.cpp
- 2.8. Unusual Operators
- Understanding the auto-increment and auto-decrement operators
- Arithmetic operators with assignment
- The conditional operator
- Calculating a minimum with the conditional operator
- Examples of the
sizeof
operator - Binary conversion and output: a
sizeof
example - Illustrating side effects with the assignment operator
- Using the comma operator in a for-loop
- 2.9. Bugs and Basic Program Debugging
- Visual Studio error detection and reporting
- The evolution of error messages
- A link error caused by a busy file
- 2.9.1. Logical Errors And Using The Debugger
- Steps for locating a logical error
- Step 1: Calculate intermediate values
- Step 2: Instrument the code
- Step 3: Trace the calculations
- Debugging the temperature program
- Examining expression values with the debugger
- Examining evaluation order with the debugger
- Debugger context controls
- Manually instrumenting code
- Debugging with a breakpoint
- Setting a tracepoint
- Set a tracepoint action
- Specifying an action
- Add tracepoints for all variables you wish to examine
- Clearing the output window
- Creating and viewing trace messages
- 2.9.2. Runtime Errors
- 2.10 Practice Problems
- 2.11. Supplemental: Bitwise Operators
- Bitwise-AND
- Bitwise-OR
- Bitwise XOR (Exclusive-OR)
- Bitwise Complement
- Forming bit patterns
- Masks and bitwise-AND
- Masks and bitwise-OR
- Left Shift Operator
- Right Shift Operator
- Right Shift Operator with sign extension
- Operation with assignment with bitwise operators
- 2.11.1. File Access Control
- 2.11.2. Binary Output
- 2.11.3. Free Space Management
3. Control Statements
- 3.1. Basic Flow Of Control
- 3.2. Block Structure and Scope
- 3.3. Logical Expressions
- 3.4. Branching Overview
- Branches in a computer program
- 3.4.1. If-Statements
- A logic diagram of a simple if-statement
- if-Statement variations
- Simple if-statement behavior
- A logic diagram of an if-else statement
- if-else statement variations
- Logic diagram of an if-else ladder
- if-else ladder variations
- Diagonal if-else ladder
- Complex if-else behavior
- The dangling-else problem
- Understanding the dangling-else code
- The correcting the dangling-else problem
- 3.4.2. temp.cpp (if Version)
- 3.4.3. switch Statements
- 3.4.4. temp.cpp (switch Version)
- 3.4.5. Switches vs. If-Else Ladders
- 3.5. Looping Overview
- C++ loop overview
- 3.5.1. For-Loops
- 3.5.2. multtab.cpp
- 3.5.3. While-Loops
- 3.5.4. gcd.cpp
- 3.5.5. Do-While-Loops
- 3.5.6. calc.cpp
- 3.5.7. Understanding Loops
- 3.5.8. The Fence Post Problem
- 3.5.9. Loops And The Debugger
- Demonstrating the debugger with multtab.cpp
- The "Run To Cursor" operation
- Examining variable contents
- Watching variables
- The "Watch" window
- Watch a second variable
- Updating the watch window
- The single-step button
- Single-Stepping
- Single-Stepping through nested loops
- Stepping through the inner loop
- Ending the inner loop
- The second iteration of the outer loop
- Ending the debug session
- Clearing watches
- 3.6. Loop Interruption: break And continue
- 3.7. Null Statements: Tricky Behavior Examined
- 3.8. More I/O Functions And Manipulators
- Inserting newlines with endl
- I/O stream flag functions
- Configuring stream objects
- Some useful manipulators
- A partial but useful list of stream (istream and ostream) member functions
- Left and right alignment
- Columnar formatting: setw, left, and right
- Formatting output with manipulators
- Data and manipulator order
- Formatting floating-point numbers
- Formatting numbers with manipulators
- get function examples
- ignore function options
- The newline problem
- Reading single characters
- Non-standard input functions
- A _getch example
- 3.9. Character Classification and Conversion Functions
- 3.10. Programming Examples
- 3.10.1. Special Variables: Flags And Accumulators
- 3.10.2. temp2.cpp
- 3.10.3. temp3.cpp
- 3.10.4. mortgage.cpp
- 3.10.5. pyramid.cpp
- 3.10.6. wc.cpp
- 3.12. Supplemental: Legitimate Uses For goto
4. Pointers
- 4.1. Introduction To Pointers
- 4.2 Variables And Memory Addresses
- 4.3. Pointer Operators
- 4.4. Dereferencing A Pointer: The Indirection Operator
- 4.5. pointers.cpp
- 4.6. Memory Management: Stack And Heap
- 4.7. Dynamic Memory: new And delete
- 4.8. Member Selection: Dot And Arrow
- 4.9. Arithmetic Operations With Pointers
- 4.10. Uses For Pointers: Dynamic Data Structures
- 4.11. Pointers Summary
5. Structures
- 5.1. Introduction To Structures
- 5.2. Enumerations (enum)
- 5.3. Structures
- Defining structure variables
- A structure Specification
- Defining structure variables and objects
- Aggregate initialization
- Default field initialization
- Designated initializers
- The dot operator
- The dot operator
- The assignment operator works with structures
- Passing a structure argument to a function
- Returning a structure object from a function
- 5.4. Structures And Pointers
- 5.5. Structures and Multi-File Programs
- 5.6. Creating Multi-File Programs With Visual Studio
- 5.7. Worked Examples
- 5.7.1. The Time Structure Example (1)
- 5.7.2. The American Structure Example
- 5.7.3. The Swapping Problem (2)
- 5.8. Supplemental
- 5.8.1 Enumerations And Scope
- 5.8.2 Unions And Bit-Fields
- 5.8.3 typedef Statements
6. Functions
- 6.1. Functions
- 6.2. Function Basics
- A function as a black box
- The parts of a function
- Function call examples
- 6.2.1. Function Definitions and Declarations
- 6.2.2. Functions and Variable Scope
- Defining and initializing a local variable
- The relationship between a function call and a stack frame
- Function-call vs. function-definition scope
- Using a static variable
auto
vs.static
variables- Using global variables to eliminate function arguments
- Variable name confusion and resolution
- Controlling scope with the extern and static keywords
- An abstract representation of function coupling
- 6.2.3. Function Return, Part 1
- 6.3. Function Arguments and Parameters
- Argument vs. parameter
- Passing arguments to parameters
- 6.3.1. Pass-By-Value
- 6.3.2. Pass-By-Pointer
- 6.3.3. Pass-By-Reference
- 6.3.4. Function Argument Passing Summary
- 6.3.5. Function Return Part 2
- 6.4. Overloaded Functions
- 6.5. Default Arguments
- 6.6. Functions and the const Keyword
- 6.7. Macros and Inline Functions
- 6.8. Recursion
- Direct and indirect recursion
- Simple recursion examples
- Recursive function calls and stack frames
- 6.8.1. Recursion Examples
- 6.8.2. Recursion V. Iteration
- 6.9. From Documentation To Programs
- 6.10. Functions And Link Errors
- 6.11. Avoiding Runtime Errors
- The quadratic formula demonstrates detecting and responding to runtime errors
- NaN and Inf: The IEEE 754 floating-point standard
- System calls and error status
- Application functions returning an error status
- Using errno to detect and report errors
- Reporting errors with perror
- Assertion system examples
- 6.12. Worked Examples
- 6.12.1. The Time Structure Example (2)
- 6.12.2. The Swapping Problem (3)
- 6.13. Functions And The Debugger
- 6.14. Practice Problems
- 6.14.1. Functions With Loops
- Problems 4 - 6 Hints: Average or Mean
- Problem 1 Solution: Maximum Number With A For-Loop
- Problem 2 Solution: Maximum Number With A While Loop
- Problem 3 Solution: Maximum Number With A Do-While Loop
- Problem 4 Solution: Average With A For-Loop
- Problem 5 Solution: Average With A While-Loop
- Problem 6 Solution: Average With A Do-While-Loop
- 6.14.1. Functions With Loops
- 6.15. Supplemental
- 6.15.1. Storage Modifiers
- 6.15.2. Variable Argument Lists
- 6.15.3. Pointers To Functions: Callback Functions
7. Arrays
- 7.1. Introduction To Arrays
- 7.2. Visualizing Arrays
- 7.3. Creating Arrays
- 7.4. Initializing Arrays
- 7.5. Arrays: C++ vs. Java
- 7.6. Arrays And Loops
- 7.7. Arrays And Functions
- An array as a function argument
- Passing arrays as function arguments
- A function returning an array: A frequent error
- Make the array static
- A potential problem with the static solution
- Define the array in the function caller's scope
- One function but multiple array arguments
- Allocate the array with the
new
operator - The dynamic array solution is flexible
- Returning the number of used or filled elements in an array
- Array of arrays: Creating and returning a two-dimensional array
- 7.8. Worked Examples
- 7.8.1. average.cpp
- 7.8.2. multtab.cpp
- 7.8.3. The Chi-Squared Statistic
- 7.8.4. rms.cpp: Arrays, Functions, And Data Input
- 7.9. Index Order
- 7.10. Row-Major Ordering
- 7.11. Arrays And Security
- 7.12. Stacks And Stack Operations
- The primary stack operations
- Implementing a stack as an array
- The push operation illustrated
- The pop operation illustrated
- 7.12.1. Automatic Stack Implementation
- 7.12.2. Dynamic Stack Implementation
- 7.13. Dynamic And Multi-Dimensional Arrays
- 7.14. More Array Examples
- 7.14.1. Selection Sort
- Swapping the smallest and top elements
- A selection sort testing driver
- ssort function (1)
- ssort function (2) swap
- ssort function (3) with swap
- Testing ssort with an array of structures
- sort orders the structures by student ID
- Implementing ssort with a swap function
- Passing an array to the swap function
- 7.14.2. Binary Search
- 7.14.3. Tic-Tac-Toe
- 7.14.1. Selection Sort
8. Strings And C-Strings
- 8.1. Introduction To Strings And C-Strings
- 8.2. C-Strings
- Defining and initializing C-strings
- Uninitialized character arrays
- 8.2.1. C-string I/O
- 8.2.2. C-String Documentation And Functions
- The C-String header files
- Null vs. empty C-Strings
- Example C-string function documentation
- Reading C-string documentation: A common error
- 8.2.2.1. strlen
- 8.2.2.2. strcpy
- 8.2.2.3. strcat
- 8.2.2.4. strcmp
- 8.2.2.5. More C-String Functions
- 8.2.2.6. C-Strings And Number Conversions
- 8.2.3. name_box.cpp, Version 1
- 8.2.4. Command Line Arguments
- 8.2.5. name_box.cpp, Version 2
- 8.2.6. The Palindrome-Number Problem
- 8.2.7. cpalnumber.cpp
- 8.3. The C++ string Class
- The string class header file
- string class constructors
- string class constructors
- string class operators
- string operator examples
- 8.3.1. string Class I/O
- 8.3.2. string Functions: Documentation And Examples
- 8.3.3. name_box.cpp, Version 3
- 8.3.4. palnumber.cpp
- 8.3.5. ipalnumber.cpp With string Iterators
- 8.4. A Comparison of the string Class and C-Strings
- 8.5. C-Strings and Scope
- 8.6. Bulletproof Code: Strings → Numbers
- 8.7. Worked Examples
- 8.7.1. pyramid.cpp, Version 2
- 8.7.2. cube.cpp and ccube.cpp
- 8.7.3. Software Development: The Anagram Problem
- The anagram normalization algorithm
- Algorithm for counting the number of occurrences of each letter
- Algorithm for detecting an anagram
- Intermediate array-based algorithms
- Mapping letters to indexes
- Reading the input and printing the result
- Program input
- Converting a string to a normal form
- Counting letter occurrences
- C++ implementations of the detection algorithm
- For-range loop syntax
- For-range versions of two anagram algorithms
- 8.8. Searching and Sorting: bsearch And qsort
- Ordering functions
- Using void pointers
- Prototypes for the qsort and bsearch library functions
- An array of integers
- demo1.cpp
- C-string arrays
- demo2.cpp
- demo3.cpp
- An array of structure objects
- demo4.cpp prototypes
- Searching and sorting objects by "name"
- Searching and sorting objects by "id"
- Searching and sorting objects by "gpa"
9. Classes And Objects
- 9.1. Classes And Objects
- 9.2. UML Class Diagrams
- 9.3. Test Yourself: Translating Basic UML to C++
- Translating a UML class diagram to C++
- 9.3.1. C++ Class Self Test Answers
- 9.4. Class Development
- 9.5. Member Functions and Program Organization
- 9.6. Constructors And Initializer Lists
- Overloaded constructors
- Default constructor examples
- Conversion constructors
- Copy constructor example
- A special case for returning objects
- A general constructor
- Argument list notation
- Initializer list vs. assignment: Member initialization
- Constructor data flow
- Using separate header and source code files
- A common error: Braces form a function body
- Converting UML to C++
- Locating default arguments and initializer lists
- Alternate member initialization
- Constructor chaining (aka constructor delegation)
- Using a helper function to implement common constructor code
- 9.7. student Class Example
- 9.8. Creating And Destroying Objects: new And delete
- 9.9. Access Functions: Getters and Setters
- 9.10. The this Pointer: class stack
- 9.11. const And Classes
- 9.12. Namespaces And The Scope Resolution Operator
- 9.13. static Variables And Functions
- 9.14. An Introduction To Exception Handling
- 9.15. Worked Examples
- 9.15.1. Time Example (Class Version)
- 9.15.2. fraction 1 Example
- 9.15.3 Software Development With Objects: The Pouring Puzzle
- The Pouring Puzzle
- The roles and attributes of glasses in the pouring puzzle
- Glass UML class diagram
- Calculating how much water to pour
- Glass objects and their interactions
- Selection logic with six separate variables
- A more elegant solution based on an array of Glass objects
- Defining the glasses array and calling the pour function
- The pouring puzzle problem logic diagram
- 9.15.3.1. Pouring Puzzle: Pass-by-Reference
- 9.15.3.2. Pouring Puzzle: Pass-by-Pointer
- 9.15.4. Problem Solving: Strings
- String elements
- String abstract data type
- 9.15.4.1. Length-Prefixed String Example
- Length-prefixed string
- The LPString class
- The LPString default constructor
- The LPString(char*) conversion constructor
- The LPString(char) conversion constructor
- The LPString copy constructor
- The LPString length function
- The LPString at function
- The LPString static print function
- The LPString print and println member functions
- The LPString readln function
- The LPString clear function
- The LPString append function
- The LPString copy function
- The LPString substring function
- The LPString equals function
- The LPString order function
- The LPString concat function
- The LPString insert function
- LPString concat Solution
- LPString insert Solution
- 9.15.4.2. CString Class Example
- CString: A C-string based class
- The CString class specification
- CString constructors and destructor: building and destroying objects
- CString access functions
- CString I/O functions
- The CString readln() function
- The CString append and clear functions
- The CString insert function
- The CString copy and concat functions
- The CString substring function
- The CString equals and order functions
- The CString grow function
- 9.15.4.3. String Class Example
- The String class
- String constructors and destructor: building and destroying objects
- String access functions
- I/O system calls
- String I/O functions
- String functions that modify this object
- Functions that create, fill, and return a new String object
- The String equals and order functions
- The String grow and set_capacity function
- 9.15.5. Stopwatch Example
10. Multi-Class Programs & The UML
- 10.1. Multi-Class Programs And The UML
- 10.2. Inheritance
- Inheritance
- Inheritance as generalization
- Inheriting functions and variables
- Class similarities and differences
- Instantiating a subclass: An abstract representation
- Substitutability
- Inheritance property values
- 10.2.1. Building Inheritance
- 10.2.2. Using Inheritance
- 10.2.3. Inheritance Example: Actor 1
- 10.2.4. #pragma and #ifndef: Actor 2
- 10.2.5. Multiple Inheritance
- 10.3. Constructive Relationships
- 10.4. Composition: Embedded Objects
- The Time class and object
- Building a whole-part object with composition
- Composition property values
- 10.4.1. Building Composition
- 10.4.2. Using Composition
- 10.5. Aggregation: Pointer Member Variables
- A class with a pointer member variable
- Building a whole-part object with aggregation
- Aggregation property values
- 10.5.1. Building Aggregation
- 10.5.2. Destructors
- 10.5.3. Using Aggregation
- 10.5.4. Overriding The Copy Constructor
- 10.6. Inheritance And Whole-Part: Actor 3
- 10.7. Multiplicity
- The Hand, Deck, and Card classes: An introduction to multiplicity
- UML multiplicity operators
- Relationship names vs. multiplicity operators
- Shuffling the deck
- Implementing multiple aggregation: array version
- Deck shuffle and swap member functions: array versions
- Aggregation driver
- The Deck class: vector version
- The Deck deal function: vector version
- Overloaded swap functions
- The Deck shuffle functions: vector versions
- 10.8. Association
- Building objects related by association
- Clarifying the association reading direction
- Solving association's cross reference problem with forward declarations
- Association restricts some inline functions
- Association property values
- 10.8.1. Building Association
- 10.8.2. Using Association
- 10.8.3. Multiple Association
- 10.9. Dependency
- 10.10. Vet 1 Example
- Relationships: Categories And Properties
11. Overloaded Operators
- 11.1. Overloaded Operators and friend Functions
- 11.2. Overloaded Operators As Member Functions
- The fraction constructor with default arguments
- Overloaded operators are implemented with member functions
- A simple driver to test the overloaded fraction operators
- Two ways of calling an overloaded binary operator
- The relationship between operands and member-function arguments
- Adding a fraction and an integer
- 11.3. Overloaded Operators As friend Functions
- 11.4. Conversion Operations
- 11.5. operator<< And operator>>
- Overloaded functions
- The inserter and extractor operator prototypes in <iostram>
- 11.5.1. Overloading operator<< and operator>>
- 11.5.2. operator<< And operator>>With Inheritance
- 11.5.3. operator<< And operator>> With Whole-Part
- 11.5.4. Actor 4: Chaining Operators And Function Calls
- Program architecture: The Actor 4 UML class diagram
- Actor 4 support functions: Constructors, destructor, and setter functions
- Actor 4: Chaining general constructors
- Actor 4: Chaining member functions
- Actor 4: Part classes I/O functions
- Actor 4: Person overloaded I/O operators
- Actor 4: Actor and Star overloaded I/O operators
- Actor 4: main
- Actor 4 output
- 11.6. operator=
- The compiler-generated assignment operator
- The compiler-created assignment operator function
- Overloaded assignment operator for complex objects
- 11.6.1. Person.cpp: Copy Constructor and Assignment Operator
- 11.7. operator[]
- The fundamental or original index operator
- Overloading operator[]
- Fundamental vs. overloaded index operators
- 11.7.1. Array 1: Overloaded operator[]
- 11.8. Practice Problems / Worked Examples
- Overloaded operators problem with the Time class
- Overloaded operators problem with the fraction class
- 11.8.1. Time Example (Overloaded Operators Version)
- 11.8.2. fraction 2 Example (Overloaded Operators Version)
12. Polymorphism
- 12.1. An Introduction To Polymorphism
- 12.2. Casting Objects
- Upcasting in the inserter operator
- Upcasting options
- Upcasting with assignment
- Upcasting by calling a function
- Downcast example
- 12.2.1. Casting And Member Variables
- 12.2.2. Safe Casting
- 12.2.3. Casting And Member Functions
- 12.3. Polymorphism In Depth
- 12.4. Actor 5: A Polymorphism Example
- 12.5. Pure Virtual Functions and Abstract Classes
- 12.6. Implementing Polymorphism
- 12.7. Mahjong Tiles: Outlining A Polymorphic Solution
13. Templates
- 13.1. Introduction To Templates
- 13.2. Template Functions
- Defining and calling a template-based swap function
- <algorithm> min template functions
- The min template function and fundamental data types
- The min template function and "natural" ordering (operator<)
- The min template function and comparator functions
- The min template function and comparator objects
- 13.3. Template Classes
- 13.4. Binary Trees: Template Examples
- Standard dynamic data structure operations
- A binary tree and the operational pointers
- Descending the tree: searching and inserting
- The remove operation: Leaf (no subtrees)
- The remove operation: One subtree
- The remove operation: Two subtrees
- 13.4.1. Tree Example 1: One Template Variable
- Employee.h: The Employee class specification
- Tree.h: The Tree class specification
- Tree.h: The Tree destructor
- Tree.h: The Tree insert function
- Tree.h: The Tree search function
- Three.h: The
public
Tree remove function - Tree.h: The
private
Tree remove function - Tree.h: The
private
Tree _list function - Tree.h: The Tree tree_view function
- Tree.h: The
private
subtrees function - Using a template class
- 13.4.2. Tree Example 2: Two Template Variables
- Two template-variable binary tree class
- insert and other member functions
- Modified Employee class
- A simple driver validating the KVTree and its functions
- The two-template variable Employee example
- Specifying a nested class
- KVTree members supporting iterators
- The iterator class and functions
- The complete WordCount program
- 13.5. Array 2: Flexible Arrays
- 13.6. The Standard Template Library (STL)
- 13.7. An Introduction To Smart Pointers