Teach Yourself C++ in 21 Days,
Week 1 at a Glance
Day 1 Getting Started
Day 2 The Parts of a C++ Program
Day 3 Variables and Constants
Day 4 Expressions and Statements
Day 5 Functions
Day 6 Basic Classes
Day 7 More Program Flow
Week 1 in Review
Week 2 at a Glance
Day 8 Pointers
Day 9 References
Day 10 Advanced Functions
Day 11 Arrays
Day 12 Inheritance
Day 13 Polymorphism
Day 14 Special Classes and Functions
Week 2 in Review
Week 3 at a Glance
Day 15 Advanced Inheritance
Day 16 Streams
Day 17 The Preprocessor
Day 18 Object-Oriented Analysis and Design
Day 19 Templates
Day 20 Exceptions and Error Handling
Day 21 Whats Next
Week 3 in Review
A Operator Precedence
B C++ Keywords
C Binary and Hexadecimal
C++ in 21 Days,
This book is dedicated to the living memory of David Levine.
A second edition is a second chance to acknowledge and to thank those folks without whose support and
help this book literally would have been impossible. First among them are Stacey, Robin, and Rachel
I must also thank everyone associated with my books, both at Sams and at Wrox press, for being
professionals of the highest quality. The editors at Sams did a fantastic job, and I must especially
acknowledge and thank Fran Hatton, Mary Ann Abramson, Greg Guntle, and Chris Denny.
I have taught an online course based on this book for a couple years, and many folks there contributed to
finding and eradicating bugs and errors. A very large debt is owed to these folks, and I must especially
thank Greg Newman, Corrinne Thompson, and also Katherine Prouty and Jennifer Goldman.
I would also like to acknowledge the folks who taught me how to program: Skip Gilbrech and David
McCune, and those who taught me C++, including Steve Rogers and Stephen Zagieboylo. I want
particularly to thank Mike Kraley, Ed Belove, Patrick Johnson, Mike Rothman, and Sangam Pant, all of
whom taught me how to manage a project and ship a product.
Others who contributed directly or indirectly to this book include: Scott Boag, David Bogartz, Gene
Broadway, Drew and Al Carlson, Frank Childs, Jim Culbert, Thomas Dobbing, James Efstratiou, David
Heath, Eric Helliwell, Gisele and Ed Herlihy, Mushtaq Khalique, Matt Kingman, Steve Leland, Michael
Smith, Frank Tino, Donovan White, Mark Woodbury, Wayne Wylupski, and Alan Zeitchek.
Programming is as much a business and creative experience as it is a technical one, and I must therefore
acknowledge Tom Hottenstein, Jay Leve, David Rollert, David Shnaider, and Robert Spielvogel.
Finally, I'd like to thank Mrs. Kalish, who taught my sixth-grade class how to do binary arithmetic in
1965, when neither she nor we knew why.
Tell Us What You Think!
As a reader, you are the most important critic and commentator of our books. We value your opinion and
want to know what we're doing right, what we could do better, what areas you'd like to see us publish in,
and any other words of wisdom you're willing to pass our way. You can help us make strong books that
meet your needs and give you the computer guidance you require.
Do you have access to CompuServe or the World Wide Web? Then check out our CompuServe forum by
typing GO SAMS at any prompt. If you prefer the World Wide Web, check out our site at
NOTE: If you have a technical question about this book, call the technical support line at
As the publishing manager of the group that created this book, I welcome your comments. You can fax, e-
mail, or write me directly to let me know what you did or didn't like about this book--as well as what we
can do to make our books stronger. Here's the information:
E-mail: [email protected]
Mail: Greg Wiegand Sams Publishing 201 W. 103rd Street Indianapolis, IN 46290
This book is designed to help you teach yourself how to program with C++. In just 21 days, you'll learn
about such fundamentals as managing I/O, loops and arrays, object-oriented programming, templates, and
creating C++ applications--all in well-structured and easy-to-follow lessons. Lessons provide sample
listings--complete with sample output and an analysis of the code--to illustrate the topics of the day.
Syntax examples are clearly marked for handy reference.
To help you become more proficient, each lesson ends with a set of common questions and answers,
exercises, and a quiz. You can check your progress by examining the quiz and exercise answers provided
in the book's appendix.
Who Should Read This Book
You don't need any previous experience in programming to learn C++ with this book. This book starts
you from the beginning and teaches you both the language and the concepts involved with programming
C++. You'll find the numerous examples of syntax and detailed analysis of code an excellent guide as you
begin your journey into this rewarding environment. Whether you are just beginning or already have
some experience programming, you will find that this book's clear organization makes learning C++ fast
NOTE: These boxes highlight information that can make your C++ programming more
efficient and effective.
WARNING: These focus your attention on problems or side effects that can occur in
These boxes provide clear definitions of essential terms.
DO use the "Do/Don't" boxes to find a quick summary of a fundamental principle in a
lesson. DON'T overlook the useful information offered in these boxes.
This book uses various typefaces to help you distinguish C++ code from regular English. Actual C++
code is typeset in a special monospace font. Placeholders--words or characters temporarily used to
represent the real words or characters you would type in code--are typeset in italic monospace. New or
important terms are typeset in italic.
In the listings in this book, each real code line is numbered. If you see an unnumbered line in a listing,
you'll know that the unnumbered line is really a continuation of the preceding numbered code line (some
code lines are too long for the width of the book). In this case, you should type the two lines as one; do
not divide them.
● Day 1
❍ Getting Started
■ A Brief History of C++
■ Solving Problems
■ Procedural, Structured, and Object-Oriented Programming
■ C++ and Object-Oriented Programming
■ How C++ Evolved
■ The ANSI Standard
■ Should I Learn C First?
■ Preparing to Program
■ Your Development Environment
■ Compiling the Source Code
■ Creating an Executable File with the Linker
■ The Development Cycle
■ Figure 1.1.
■ HELLO.CPPYour First C++ Program
■ Listing 1.1. HELLO.CPP, the Hello World program.
■ Compile Errors
■ Listing 1.2. Demonstration of
■ compiler error.
Welcome to Teach Yourself C++ in 21 Days! Today you will get started on your way to becoming a proficient C++
programmer. You'll learn
● Why C++ is the emerging standard in software development.
● The steps to develop a C++ program.
● How to enter, compile, and link your first working C++ program.
A Brief History of C++
Computer languages have undergone dramatic evolution since the first electronic computers were built to assist in
telemetry calculations during World War II. Early on, programmers worked with the most primitive computer
instructions: machine language. These instructions were represented by long strings of ones and zeroes. Soon,
assemblers were invented to map machine instructions to human-readable and -manageable mnemonics, such as ADD
In time, higher-level languages evolved, such as BASIC and COBOL. These languages let people work with
something approximating words and sentences, such as Let I = 100. These instructions were translated back into
machine language by interpreters and compilers. An interpreter translates a program as it reads it, turning the program
instructions, or code, directly into actions. A compiler translates the code into an intermediary form. This step is
called compiling, and produces an object file. The compiler then invokes a linker, which turns the object file into an
Because interpreters read the code as it