The Rook's Guide To C++ Rooks Isbn Version
The%20Rook's%20Guide%20to%20C__%20-%20Jeremy%20A.%20Hansen%2C%20Ph.D_
User Manual: Pdf
Open the PDF directly: View PDF .
Page Count: 160
e Rook’s Guide to C++
26 November 2013
© 2013 Jeremy A. Hansen
All rights reserved.
is work is licensed under a Creative Commons Aribution-NonCommercialShareAlike 3.0 Unported License, as described at
http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
Printed in the United States of America
First edition:
ISBN 978-1-304-66105-0
Rook’s Guide Press
19 Black Road
Berlin, VT 05602
http://rooksguide.org
November 2013
Preface
What you are reading is the first of what I hope to be many everimproving iterations of a useful C++ textbook. We’ve gone fairly
quickly from whim to print on an all-volunteer basis, and as a result,
there are many things that I’d add and change if I had an infinite
amount of time in my schedule. e vast majority of the contents
were wrien in less than 36 hours by 25 students (mostly freshmen!)
at Norwich University over a long weekend. Some of it is mine, and
some was added by our crack team of technical editors as we translated sleep-deprived poor grammar into sleep-deprived beer grammar.
Where it goes from here is mostly up to you! If there’s a section
that’s missing or in need of clarification, please take a bit of time
and make those changes. If you don’t want to bother yourself with
the GitHub repository, send me your additions and modifications directly.
I want to first thank my family for the time I didn’t spend with
them on the writing weekend and throughout the summer when I
was editing and typeseing. I promise I won’t do this next summer!
My next thanks go out to the technical editors and typeseers,
without whom you would have a much uglier book. anks to Ted
Rolle for building the initial LATEXframework and to Ma Jadud for
the incredibly helpful pointers on how to manage the pile of typesetting files in a collaborative environment. I also thank Craig Robbins
iii
and Levi Schuck, who, on different sides of the planet, managed to
contribute extensively to the heavy liing of geing the book into
the shape it’s in now. If we ever meet, I owe you a beer or whatever
you’re having!
I also would like to thank all of the Kickstarter backers not only
for the money which made this possible, but for reinforcing the idea
that this is a worthwhile contribution to the community. Peter Stephenson and Andrew Pedley also contributed food directly over the textbook writing hackathon weekend, and without them we’d never have
goen our saturated fat quota! (Note to future project leaders: there’s
nothing that gets a bunch of college students who are generally lukewarm about programming to write a textbook like free food. It didn’t
even maer what the food was. Really.)
anks to Ma Russo for shooting the video and organizing the
media and social networking efforts with the Kickstarter project through
the writing weekend.
Special thanks to Allyson LeFebvre1 for the textbook photography, several diagrams, and the extensive search through the semifinal textbook that turned up a bunch of mistakes that I missed.
And my last (and not at all least) thanks go out to all the students who showed up in person or digitally. And without geing
too grandiose, you remind us all that we can make the world beer
by showing up. Keep showing up!
Jeremy
jeremyhansen@acm.org
26 November 2013
1
at’s “la-fave”, everyone
Contents
1 History
1
2 Variables
2.1 How do I decide which data type I need?
2.2 Identifiers . . . . . . . . . . . . . . . . .
2.3 Declaring a Variable . . . . . . . . . . . .
2.4 Initializing Variables . . . . . . . . . . . .
2.5 Assignment Statements . . . . . . . . . .
2.6 Review estions . . . . . . . . . . . . .
2.7 Review Answers . . . . . . . . . . . . . .
2.8 Further Reading . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3
4
4
4
5
5
6
6
6
3 Literals and Constants
3.1 Literals . . . . . . .
3.2 Declared Constants
3.3 Review estions .
3.4 Review Answers . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
7
7
7
8
8
4 Assignments
4.1 Review estions . . . . . . . . . . . . . . . . . . . .
4.2 Review Answers . . . . . . . . . . . . . . . . . . . . .
11
13
13
.
.
.
.
.
.
.
.
.
.
.
.
v
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5 Output
5.1 Review estions . . . . . . . . . . . . . . . . . . . .
5.2 Review Answers . . . . . . . . . . . . . . . . . . . . .
5.3 Further Reading . . . . . . . . . . . . . . . . . . . . .
15
18
19
19
6 Input
6.1 Review estions . . . . . . . . . . . . . . . . . . . .
6.2 Review Answers . . . . . . . . . . . . . . . . . . . . .
6.3 Further Reading . . . . . . . . . . . . . . . . . . . . .
21
23
24
24
7 Arithmetic
7.1 Review estions . . . . . . . . . . . . . . . . . . . .
7.2 Review Answers . . . . . . . . . . . . . . . . . . . . .
7.3 Further Reading . . . . . . . . . . . . . . . . . . . . .
25
28
30
31
8 Comments
8.1 Review estions . . . . . . . . . . . . . . . . . . . .
8.2 Review estions . . . . . . . . . . . . . . . . . . . .
33
35
37
9 Data Types and Conversion
9.1 Floating-point types . . . . . . . .
9.2 Other types introduced by C++11
9.3 Conversion Between Types . . . .
9.4 Coercion & Casting . . . . . . . .
9.5 Automatic Types in C++11 . . . .
9.6 Review estions . . . . . . . . .
9.7 Review Answers . . . . . . . . . .
9.8 Further Reading . . . . . . . . . .
.
.
.
.
.
.
.
.
39
40
42
42
43
44
45
45
45
.
.
.
.
47
48
48
49
51
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
10 Conditionals
10.1 if, else, and else if . . . . . . . . .
10.1.1 A small digression on expressions .
10.1.2 Using else . . . . . . . . . . . . .
10.2 switch statements . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
10.3 Review estions . . . . . . . . . . . . . . . . . . . .
10.4 Review Answers . . . . . . . . . . . . . . . . . . . . .
54
54
11 Strings
11.1 Review estions . . . . . . . . . . . . . . . . . . . .
11.2 Review Answers . . . . . . . . . . . . . . . . . . . . .
11.3 Further Reading . . . . . . . . . . . . . . . . . . . . .
55
59
60
60
12 Loops
12.1 Introduction . . . . . . . . . . . . . . . . .
12.2 Having Fun while Programming . . . . .
12.3 do-while Loops . . . . . . . . . . . . . .
12.4 Event-Based Loops vs Count-Based Loops
12.5 for work or for play . . . . . . . . . . .
12.6 Picking a Loop . . . . . . . . . . . . . . . .
12.7 Nested Loops . . . . . . . . . . . . . . . . .
12.8 Infinite Loops . . . . . . . . . . . . . . . .
12.9 Review estions . . . . . . . . . . . . . .
12.10 Review Answers . . . . . . . . . . . . . . .
12.11 Further Reading . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
61
61
61
63
65
66
67
68
68
69
70
70
13 Arrays
13.1 Multi-dimensional Arrays .
13.2 Review estions . . . . .
13.3 Review Answers . . . . . .
13.4 Further Reading . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
71
73
74
74
75
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
14 Blocks, Functions, and Scope
77
14.1 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . 77
14.2 Basic Functions in C++ . . . . . . . . . . . . . . . . . 78
14.2.1 What are functions and why do we use them? 78
14.2.2 e parts of a basic function . . . . . . . . . . 78
14.3 void Functions . . . . . . . . . . . . . . . . . . . . . 81
14.4 Overloading Function Names . . . . . . . . . . . . . . 82
14.5
14.6
14.7
14.8
Scope . . . . . . .
Review estions
Review Answers .
Further Reading .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
83
85
86
88
15 Problem Solving & Troubleshooting
15.1 e Compilation Error . . . . . .
15.2 e Logic Error . . . . . . . . . .
15.3 e Infinite Loop . . . . . . . . .
15.4 Review estions . . . . . . . . .
15.5 Review Answers . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
89
89
90
90
91
92
16 e Preprocessor
16.1 Review estions . . . . . . . . . . . . . . . . . . . .
16.2 Review Answers . . . . . . . . . . . . . . . . . . . . .
93
94
95
17 Advanced Arithmetic
17.1 Examples . . . . .
17.1.1 pow() .
17.1.2 sqrt()
17.1.3 Modulo .
17.2 Review estions
17.3 Review Answers .
17.4 Further Reading .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
97
98
98
99
100
101
101
102
18 File I/O
18.1 I/O Streams . . . . . . . . . . . . . .
18.2 File I/O . . . . . . . . . . . . . . . .
18.3 Opening and closing a File . . . . .
18.4 Reading from a File . . . . . . . . .
18.5 Writing data to a File . . . . . . . .
18.6 Introduction to Classes and Objects
18.7 Other functions . . . . . . . . . . .
18.8 Review estions . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
103
103
104
104
105
106
107
108
109
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
18.9 Review Answers . . . . . . . . . . . . . . . . . . . . . 110
18.10 Further Reading . . . . . . . . . . . . . . . . . . . . . 110
19 Pointers
111
19.1 Review estions . . . . . . . . . . . . . . . . . . . . 114
19.2 Review Answers . . . . . . . . . . . . . . . . . . . . . 115
20 Dynamic Data
117
20.1 Review estions . . . . . . . . . . . . . . . . . . . . 121
20.2 Review Answers . . . . . . . . . . . . . . . . . . . . . 121
20.3 Further Reading . . . . . . . . . . . . . . . . . . . . . 121
21 Classes and Abstraction
21.1 structs . . . . . . . . . . . . . . . . . . . . . .
21.2 Assigning values to member variables . . . . . . .
21.3 Classes . . . . . . . . . . . . . . . . . . . . . . . .
21.4 public and private variables and functions
21.5 Defining member functions . . . . . . . . . . . . .
21.6 Using member functions . . . . . . . . . . . . . .
21.7 classes and structs together . . . . . . . . .
21.8 Constructors . . . . . . . . . . . . . . . . . . . . .
21.9 Overloading Member Functions . . . . . . . . . .
21.10 Review estions . . . . . . . . . . . . . . . . . .
21.11 Review Answers . . . . . . . . . . . . . . . . . . .
21.12 Further Reading . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
123
123
124
124
125
126
126
127
128
129
130
131
132
22 Separate Compilation
133
22.1 Review estions . . . . . . . . . . . . . . . . . . . . 135
22.2 Review Answers . . . . . . . . . . . . . . . . . . . . . 136
22.3 Further Reading . . . . . . . . . . . . . . . . . . . . . 136
23 STL
137
23.1 #include
#include (C++11) . . . . . . . . . . . 137
23.2 #include . . . . . . .
23.2.1 Forward iterators . . . .
23.2.2 Bidirectional iterators .
23.2.3 Random access iterators
23.3 #include . . . . . . . .
23.4 #include
Source Exif Data:
File Type : PDF
File Type Extension : pdf
MIME Type : application/pdf
PDF Version : 1.4
Linearized : Yes
Author : Jeremy A. Hansen, Ph.D.
Create Date : 2013:11:26 18:14:34-05:00
Modify Date : 2013:12:02 15:40:38-05:00
XMP Toolkit : Adobe XMP Core 5.2-c001 63.139439, 2010/09/27-13:37:26
Format : application/pdf
Creator : Jeremy A. Hansen, Ph.D.
Title : The Rook's Guide to C++
Creator Tool : LaTeX with hyperref package
Metadata Date : 2013:12:02 15:40:38-05:00
Producer : MiKTeX-xdvipdfmx (0.7.7 svn texlive 15080)
Document ID : uuid:25b53868-587c-46a9-aa33-985cc402ed15
Instance ID : uuid:cdf5aad6-6f0d-46d7-b99a-f9eb77b04966
Page Mode : UseOutlines
Page Count : 160
EXIF Metadata provided by EXIF.tools