Skip to main content
Welcome to Programming and Data Structures
Andrew Scholer
Contents
Index
Search Book
close
Search Results:
No results.
Dark Mode
Prev
Up
Next
Profile
Course Home
Assignments
Practice
Peer Instruction (Instructor)
Peer Instruction (Student)
Change Course
Instructor Dashboard
Progress Page
Edit Profile
Change Password
Log Out
\( \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \definecolor{fillinmathshade}{gray}{0.9} \newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}} \)
About this book
1
Preface
1.1
The Philosophy Behind the Book
1.2
Why C++?
1.3
Instructional Notes
1.3.1
Exercises (and what they lack)
1.3.2
Case Studies
1.3.3
Chapter Dependencies
1.4
Acknowledgments
2
Computer Programming
2.1
What Is a Computer?
2.2
What Is Programming?
2.3
What Is Computer Science?
2.4
The Hello World Program
2.5
Compiling C++ Programs
2.6
Displaying Two Messages
2.7
Formatting Source Code
2.8
Debugging Programs
2.9
Debugging Syntax Errors
2.10
Other Types of Errors
2.10.1
Run-time errors
2.10.2
Logic errors and semantics
2.11
Vocabulary
2.12
Exercises
3
Variables and Operators
3.1
Values and Literals
3.2
Declaring Variables
3.3
Identifiers
3.3.1
Syntax Requirements
3.3.2
Conventions
3.4
Assigning Variables and Initialization
3.5
Memory Diagrams
3.6
Printing Variables
3.7
Multiple assignment
3.8
Arithmetic Operators
3.8.1
Basics
3.8.2
Common Errors
3.9
Integer Division
3.10
Remainder Operator
3.11
Order of Operations
3.12
Shortcut Operators
3.13
Case Study: Time
3.13.1
A problem and a plan
3.13.2
First steps
3.13.3
Calculating minutes
3.13.4
Calculating hours
3.13.5
Wrapping up
3.14
Basic Input
3.15
Vocabulary
3.16
Exercises
4
Floating-point numbers and Math
4.1
Floating-Point Numbers
4.2
Rounding Errors
4.3
Floating-Point Division
4.4
Implicit Conversions
4.5
Type Casting
4.6
Other Size Types
4.6.1
Integer types
4.6.2
Unsigned types
4.6.3
Floating point types
4.7
Constants
4.8
Math functions
4.9
Finding and Understanding Functions
4.10
Rounding Functions
4.11
Trigonometric Functions
4.12
Function Composition
4.13
Special Values
4.14
Case Study: Point Rotation
4.14.1
The problem
4.14.2
Making a Plan
4.14.3
Checking Our Tools
4.14.4
Calculating x2
4.14.5
Calculating y2
4.14.6
Wrapping up
4.15
Vocabulary
4.16
Exercises
5
Functions and Testing
5.1
Defining Functions
5.2
Flow of Execution
5.3
Returning
5.4
Stack Frames and Scope
5.4.1
Stack Frames
5.4.2
Scope
5.4.3
Global Variables
5.5
Parameters and Arguments
5.5.1
Basics
5.5.2
Parameter Numbers and Types
5.5.3
Argument Types
5.5.4
Passing is by Value
5.6
Default Parameters and Overloads
5.6.1
Overloaded Functions
5.6.2
Default Parameter Values
5.7
More on Returning
5.7.1
Returning Variables
5.7.2
Void Functions
5.8
Declaring vs Defining Functions
5.9
Documenting Functions
5.10
Unit Testing
5.11
Doctest
5.12
Testing Wrapup
5.13
Incremental Development of Functions
5.14
Vocabulary
5.15
Exercises
6
Splitting Code Across Multiple Files
6.1
Projects with Multiple Files
6.2
Exploring Separate Compilation
6.3
Header Files
6.4
Using Header Files
6.5
Modules
6.6
Multiple Programs
6.7
Vocabulary
6.8
Exercises
7
Conditionals and Logic
7.1
Boolean Values and Variables
7.2
Relational Operators
7.3
The if Statement
7.3.1
The condition
7.3.2
The body
7.4
Alternative Execution (if/else)
7.5
Branches and Scope
7.6
Chaining and Nesting
7.7
Logical Operators
7.7.1
And
7.7.2
Or
7.7.3
Not
7.7.4
Short Circuit Evaluation
7.8
Extended order of operation
7.9
Boolean functions
7.10
Simplifying Logical Expressions
7.11
Other Conditional Structures
7.11.1
Switch Statements
7.11.2
Ternary Conditional Operator
7.12
Case Study: Taxes
7.12.1
The problem
7.12.2
Understanding the Problem And Tests
7.12.3
First decision
7.12.4
Single Filers
7.12.5
Married Filers
7.12.6
Debrief
7.13
Vocabulary
7.14
Exercises
8
Loops
8.1
The while Statement
8.2
Increment and Decrement
8.3
Counting Loops
8.4
Accumulating
8.5
The for Statement
8.6
Sentinel Loops
8.7
Tables
8.8
Nested Loops
8.8.1
Basics
8.8.2
Other uses of Nested Loops
8.9
Random Numbers
8.10
Coin Flips
8.11
Do While
8.12
Breaking and Continuing
8.13
Case Study: Finding Primes
8.13.1
The problem
8.13.2
Understanding
isPrime
and Tests
8.13.3
isPrime
8.13.4
Understanding
countPrimes
and Tests
8.13.5
countPrimes
8.13.6
Wrapup
8.14
Vocabulary
8.15
Exercises
9
Strings
9.1
Characters
9.1.1
char
data type
9.1.2
Chars as numbers
9.2
Character Functions
9.3
String Variables and Objects
9.3.1
String Variables
9.3.2
Strings are Objects
9.4
string
s vs C-strings
9.5
size_t
9.6
Indexes in strings
9.7
Other Escape Sequences
9.8
String Iteration
9.8.1
A loop for strings
9.8.2
The ranged-based for loop
9.9
find
in strings
9.10
Substrings
9.11
Operators for Strings
9.11.1
Comparisons
9.11.2
Concatenation
9.12
Strings and Numbers
9.13
Other String Functions
9.13.1
Other finds
9.13.2
String modifying functions
9.14
Writing Functions With String
9.15
Vocabulary
9.16
Exercises
10
References and Exceptions
10.1
References
10.2
References and Functions
10.3
Constant References
10.4
When to Use References
10.5
References in Range-Based For Loops
10.6
Error Handling
10.6.1
Assertions
10.6.2
Special Values
10.6.3
Error Flag
10.6.4
Exceptions
10.7
Exceptions
10.8
Exception Passing Through Levels
10.9
Exception Types
10.10
Exception Throwing
10.11
Exception When
10.12
Vocabulary
10.13
Exercises
11
IO and Formatting
11.1
std::format
11.2
Formatting Numbers
11.2.1
sign
11.2.2
precision
11.2.3
type
11.3
Format Spacing
11.4
Files and Streams
11.4.1
Files
11.4.2
Streams
11.5
File Input
11.6
Input Tokens and Errors
11.7
End of File and Getline
11.8
File Output
11.9
String Streams
11.10
Case Study: Temperature Ranges
11.10.1
Understanding the problem
11.10.2
Reading Data
11.10.3
Calculating Differences
11.10.4
Finding the Max
11.11
Vocabulary
11.12
Exercises
12
Function Design and Program Development
12.1
Why Functions?
12.2
Function Guidelines
12.3
Single Responsibility Principle
12.4
Code Reuse and Abstractions
12.5
Modularity
12.6
Function Length
12.6.1
Challenges with Long Functions
12.6.2
Dealing with Function Length
12.6.3
Too Short Functions
12.7
Top Down Design
12.8
Bottom-Up Design
12.9
From Design to Implementation
12.10
Test Driven Development
12.11
Writing Tests
12.12
Case Study: Building a Multi-File Program
12.12.1
The project structure
12.12.2
Implementing
getMonth
12.12.3
Fixing
getMonth
12.12.4
Using
getMonth
in the real program
12.13
Case Study: Building a Multi-File Program - Part 2
12.13.1
Testing
daysBeforeMonth
12.13.2
Implementing
daysBeforeMonth
12.13.3
Next steps
12.14
Case Study: Building a Multi-File Program - Part 3
12.15
Final Program Using Modules
12.16
Final Program Using Header File
12.17
Vocabulary
12.18
Exercises
13
Vectors
13.1
What are vectors?
13.2
Creating vectors
13.3
Accessing Elements
13.4
Inserting and Removing Elements
13.5
Traversing vectors
13.6
Accessing Consecutive Elements
13.7
Functions and Vectors
13.8
Recipes for vectors
13.8.1
Finding things
13.8.2
Reduce
13.8.3
Map
13.8.4
Filter
13.9
Case Study: Calculating a Grade
13.9.1
Understanding the problem
13.9.2
minValue
13.9.3
Average
13.9.4
Input
13.9.5
Wrapup
13.10
Vocabulary
13.11
Exercises
14
Multidimensional Vectors
14.1
Multidimensional vectors
14.2
Multidimensional Vector Access
14.3
Multidimensional Vector Rows and Elements
14.4
Multidimensional Vector Traversals
14.4.1
A first attempt
14.4.2
A better traversal
14.5
Single Dimension Traversals
14.6
Typedefs
14.7
Functions for Multidimensional Vectors
14.8
Extra Dimensions
14.9
Case Study: Quiz Grading
14.9.1
Understanding the problem
14.9.2
Designing Data
14.9.3
Student Grades
14.9.4
Question Difficulty
14.10
Vocabulary
14.11
Exercises
15
Structs
15.1
Structs
15.2
Accessing instance variables
15.3
Operations on structures
15.4
Structures with Functions
15.5
Pure functions
15.6
Compound Structures
15.7
Enumerated types
15.8
Values for Enums
15.9
Structs and Vectors
15.9.1
Structs containing Vectors
15.10
Vectors of Structs
15.10.1
Avoiding Copies
15.11
Case Study: Students
15.11.1
Understanding the problem
15.11.2
Storing a student
15.11.3
Parsing a Name
15.11.4
Getting the Status
15.11.5
Parsing the Scores
15.11.6
Building the Student
15.12
Case Study: Students - Part 2
15.12.1
Calculating One Average
15.12.2
Calculating all the Averages
15.13
Case Study: Students - Part 3
15.13.1
Reading the File
15.14
Vocabulary
15.15
Exercises
16
Objects
16.1
Why Objects
16.2
Objects
16.3
Classes
16.4
Public and Private
16.4.1
Using Public and Private
16.4.2
Why use Private
16.5
Working with Members
16.5.1
Accessing Member Variables in the Class
16.5.2
Setters and Mutators
16.6
Constructors
16.6.1
What is a Constructor?
16.6.2
Multiple Constructors
16.6.3
Default Values
16.6.4
Making Copies
16.7
Member Functions
16.8
Testing Objects
16.9
Class Diagrams and Documentation
16.9.1
UML Class Diagrams
16.9.2
Doxygen for Classes
16.9.3
Exercises
16.10
Defining Functions Outside of the Class
16.11
Using Headers
16.12
Using Modules
16.13
Case Study: Times
16.13.1
Making a Plan
16.13.2
The Basics
16.13.3
Handling Invalid Times
16.13.4
Displaying Times
16.13.5
Modifiers
16.13.6
Adding Minutes
16.13.7
Wrapping up
16.14
Vocabulary
16.15
Exercises
16.16
Appendix - Time Class as Module
16.17
Appendix - Time class as .h/.cpp
17
Interacting Objects and Composition
17.1
Const Member Functions
17.1.1
Objects as Parameters
17.1.2
Making Members Const
17.2
Objects as Parameters to Methods
17.3
Objects in Vectors
17.4
Class Level Data and Functions
17.4.1
Static Data
17.4.2
Static Functions
17.5
Composition of Classes
17.6
Implementing Composition
17.7
Using the Contained Members
17.8
Modifying the Contained Members
17.9
Constructing Composed Objects
17.10
Returning Composed Objects
17.11
A Final Version of Circle
17.12
More Complex Composition
17.13
Vocabulary
17.14
Exercises
17.15
Point.h and Point.cpp
18
Pointers and Aggregation
18.1
Memory Addresses of Variables
18.2
Pointers
18.3
Types of Pointers
18.4
Dereferencing Pointers
18.5
Null Pointers
18.6
Sharing Memory
18.7
Why Pointers? And Why Not Pointers?
18.8
Object Member Access with Pointers
18.9
Objects and
this
18.10
Introducing Aggregation
18.11
Implementing Aggregation
18.11.1
Variable Ownership Semantics
18.11.2
Aggregation Using Pointers
18.12
Self Aggregation
18.13
A Family Tree
18.14
Vocabulary
18.15
Exercises
19
Inheritance
19.1
What is Inheritance?
19.2
Declaring Inheritance
19.3
Access Modifiers and Inheritance
19.4
Protected Access
19.5
Overriding Members
19.6
Upcasting
19.7
Virtual Functions
19.7.1
Virtual Functions - How
19.7.2
Virtual Functions - When
19.7.3
Virtual Functions - Syntax Notes
19.8
Object Slicing and Dynamic Casting
19.8.1
Object Slicing
19.8.2
Dynamic Casting
19.9
Abstract Methods
19.10
Abstract Classes and Interfaces
19.10.1
Abstract Classes
19.10.2
Interfaces
19.11
Multiple Inheritance
19.12
Designing with Inheritance
19.13
Inheritance vs Composition
19.14
Vocabulary
19.15
Exercises
20
Operator Overloads
20.1
Importance of Operators
20.2
Guidelines
20.3
Basic Member Operators
20.4
Overloading ++
20.5
Overloading ++ (Postfix)
20.6
Overloading = (Assignment)
20.7
Other Operator Overloads
20.8
Vocabulary
20.9
Exercises
21
Recursive functions
21.1
Intro to Recursion
21.2
Recursive Stack Diagrams
21.3
Base vs General Case
21.4
Value-Returning functions
21.5
Designing Recursive Functions
21.6
Recursive Design Practice
21.7
Parameters as Tools
21.8
Iteration & Recursion
21.9
Tail Recursion
21.10
Advanced Recursion - Flood Fill
21.11
Vocabulary
21.12
Exercises
22
Heap and Memory Management
22.1
The Stack
22.2
Stack Issues
22.2.1
Limited Size
22.2.2
Limited Duration
22.3
The Heap
22.4
Dynamic Memory
22.5
Memory Leaks and Delete
22.6
Detecting Leaks and Use-After-Free Errors
22.7
Passing Memory with Pointers
22.8
Arrays
22.9
Using Arrays
22.10
Container Classes
22.11
Destructors
22.12
Copy Constructors
22.13
Assignment Operator
22.14
Rule of Three
22.15
Vocabulary
22.16
Exercises
23
Templates and Array Lists
23.1
Templates
23.2
Type Parameters
23.3
Using the Template Type
23.4
Operations in Templated Code
23.5
Using Templated Code Across Files
23.6
Templated Data Types
23.7
Templated Member Functions
23.8
What is an Array List?
23.9
Basic ArrayList
23.10
Insert End
23.11
Growing
23.12
Access and Removal
23.13
Insert At
23.14
Vocabulary
23.15
Exercises
23.16
Appendix - Array List Module Implementation
23.17
Appendix - Array List Header Implementation
Back Matter
Index
24
Libraries
24.1
SimpleRandom
Chapter
17
Interacting Objects and Composition
In the last chapter, we learned about objects. While doing so, we focused on solitary objects. We did not spend much time considering how multiple objects might interact. In this chapter, we start to explore that idea.
🔗
17.1
Const Member Functions
17.2
Objects as Parameters to Methods
17.3
Objects in Vectors
17.4
Class Level Data and Functions
17.5
Composition of Classes
17.6
Implementing Composition
17.7
Using the Contained Members
17.8
Modifying the Contained Members
17.9
Constructing Composed Objects
17.10
Returning Composed Objects
17.11
A Final Version of Circle
17.12
More Complex Composition
17.13
Vocabulary
17.14
Exercises
17.15
Point.h and Point.cpp
🔗