A new edition of this title is available, ISBN-10: 0672329417 ISBN-13: 9780672329418
Join the leagues of thousands of programmers and learn C++ from some of the best. The fifth edition of the best seller Sams Teach Yourself C++ in 21 Days, written by Jesse Liberty, a well-known C++ and C# programming manual author and Bradley L. Jones, manager for a number of high profiler developer websites, has been updated to the new ANSI/ISO C++ Standard. This is an excellent hands-on guide for the beginning programmer. Packed with examples of syntax and detailed analysis of code, fundamentals such as managing I/O, loops, arrays and creating C++ applications are all covered in the 21 easy-to-follow lessons. You will also be given access to a website that will provide you will all the source code examples developed in the book as a practice tool. C++ is the preferred language for millions of developers-make Sams Teach Yourself the preferred way to learn it!
Jesse Liberty is the author of numerous books on software development, including best-selling titles in C++ and .NET. He is the president of Liberty Associates, Inc. (http://www.LibertyAssociates.com) where he provides custom programming, consulting, and training.
Bradley Jones, Microsoft MVP, Visual C++, can be referred to as a webmaster, manager, coding grunt, executive editor, and various other things. His time and focus are on a number of software development sites and channels, including Developer.com, CodeGuru.com, DevX, VBForums, Gamelan, and other Jupitermedia-owned sites. This influence expands over sites delivering content to over 2.5 million unique developers a month.
His expertise is in the area of the big "C"s-C, C++, and C#-however, his experience includes development in PowerBuilder, VB, some Java, ASP, COBOL I/II, and various other technologies too old to even mention now. He has also been a consultant, analyst, project lead, associate publisher for major technical publishers, and author. His recent authoring credits include Sams Teach Yourself the C# Language in 21 Days, a 6th edition of Sams Teach Yourself C in 21 Days, and now this edition of Sams Teach Yourself C++ in 21 Days. He is also the cofounder and president of the Indianapolis .NET Developers Association, which is a charter INETA group with membership of over 700. You can often hear his ramblings on the CodeGuru.com or VBForums.com discussion forums, and he also does the weekly CodeGuru newsletter that goes out to tens of thousands of developers.
(c) Copyright Pearson Education. All rights reserved.
Who Should Read This Book
Conventions Used in This Book
Sample Code for This Book
Week 1 At a Glance
A Note to C Programmers
Where You Are Going
1. Getting Started.
A Brief History of C++
The Need for Solving Problems
Procedural, Structured, and Object-Oriented Programming
Object-Oriented Programming (OOP)
C++ and Object-Oriented Programming
How C++ Evolved
Should I Learn C First?
C++, Java, and C#
Microsoft's Managed Extensions to C++
The ANSI Standard
Preparing to Program
Your Development Environment
The Process of Creating the Program
Creating an Object File with the Compiler
Creating an Executable File with the Linker
The Development Cycle
HELLO.cpp-Your First C++ Program
Getting Started with Your Compiler
Building the Hello World Project
2. The Anatomy of a C++ Program.
A Simple Program
A Brief Look at cout
Using the Standard Namespace
Commenting Your Programs
Types of Comments
A Final Word of Caution About Comments
Methods Versus Functions
3. Working with Variables and Constants.
What Is a Variable?
Storing Data in Memory
Setting Aside Memory
Size of Integers
signed and unsigned
Fundamental Variable Types
Defining a Variable
Creating More Than One Variable at a Time
Assigning Values to Your Variables
Creating Aliases with typedef
When to Use short and When to Use long
Wrapping Around an unsigned Integer
Wrapping Around a signed Integer
Working with Characters
Characters and Numbers
Special Printing Characters
4. Creating Expressions and Statements.
Starting with Statements
Blocks and Compound Statements
Working with Operators
Combining the Assignment and Mathematical Operators
Incrementing and Decrementing
Prefixing Versus Postfixing
Understanding Operator Precedence
The Nature of Truth
Evaluating with the Relational Operators
The if Statement
The else Statement
Advanced if Statements
Using Braces in Nested if Statements
Using the Logical Operators
The Logical AND Operator
The Logical OR Operator
The Logical NOT Operator
Short Circuit Evaluation
More About Truth and Falsehood
The Conditional (Ternary) Operator
5. Organizing into Functions.
What Is a Function?
Return Values, Parameters, and Arguments
Declaring and Defining Functions
Defining the Function
Execution of Functions
Determining Variable Scope
Local Variables Within Blocks
Parameters Are Local Variables
Global Variables: A Word of Caution
Considerations for Creating Function Statements
More About Function Arguments
More About Return Values
Special Topics About Functions
How Functions Work-A Peek Under the Hood
Levels of Abstraction
6. Understanding Object-Oriented Programming.
Is C++ Object-Oriented?
Creating New Types
Introducing Classes and Members
Declaring a Class
A Word on Naming Conventions
Defining an Object
Classes Versus Objects
Accessing Class Members
Assigning to Objects, Not to Classes
If You Don't Declare It, Your Class Won't Have It
Private Versus Public Access
Making Member Data Private
Implementing Class Methods
Adding Constructors and Destructors
Getting a Default Constructor and Destructor
Using the Default Constructor
Including const Member Functions
Interface Versus Implementation
Where to Put Class Declarations and Method Definitions
Classes with Other Classes as Member Data
7. More on Program Flow.
The Roots of Looping: goto
Why goto Is Shunned
Using while Loops
Exploring More Complicated while Statements
Introducing continue and break
Examining while (true) Loops
Implementing do...while Loops
Looping with the for Statement
Advanced for Loops
Empty for Loops
Scoping in for Loops
Summing Up Loops
Controlling Flow with switch Statements
Using a switch Statement with a Menu
Week 1 In Review
Week 2 At a Glance
Where You Are Going
8. Understanding Pointers.
What Is a Pointer?
A Bit About Memory
Getting a Variable's Memory Address
Storing a Variable's Address in a Pointer
Getting the Value from a Variable
Dereferencing with the Indirection Operator
Pointers, Addresses, and Variables
Manipulating Data by Using Pointers
Examining the Address
Why Would You Use Pointers?
The Stack and the Free Store (Heap)
Allocating Space with the new Keyword
Putting Memory Back: The delete Keyword
Another Look at Memory Leaks
Creating Objects on the Free Store
Deleting Objects from the Free Store
Accessing Data Members
Creating Member Data on the Free Store
The this Pointer
Stray, Wild, or Dangling Pointers
Using const Pointers
const Pointers and const Member Functions
Using a const this Pointers
9. Exploiting References.
What Is a Reference?
Using the Address-Of Operator (&) on References
Attempting to Reassign References (Not!)
Null Pointers and Null References
Passing Function Arguments by Reference
Making swap() Work with Pointers
Implementing swap() with References
Understanding Function Headers and Prototypes
Returning Multiple Values
Returning Values by Reference
Passing by Reference for Efficiency
Passing a const Pointer
References as an Alternative
Knowing When to Use References Versus Pointers
Mixing References and Pointers
Returning Out-of-Scope Object References
Returning a Reference to an Object on the Heap
Pointer, Pointer, Who Has the Pointer?
10. Working with Advanced Functions.
Overloaded Member Functions
Using Default Values
Choosing Between Default Values and Overloaded Functions
The Default Constructor
The Copy Constructor
Writing an Increment Function
Overloading the Prefix Operator
Returning Types in Overloaded Operator Functions
Returning Nameless Temporaries
Using the this Pointer
Overloading the Postfix Operator
Difference Between Prefix and Postfix
Overloading Binary Mathematical Operators
Issues in Operator Overloading
Limitations on Operator Overloading
What to Overload
The Assignment Operator
Handling Data Type Conversion
11. Object-Oriented Analysis and Design.
Software Design: The Modeling Language
Software Design: The Process
Waterfall Versus Iterative Development
The Process of Iterative Development
Step 1: The Conceptualization Phase: Starting with The Vision
Step 2: The Analysis Phase: Gathering Requirements
Step 3: The Design Phase
What Are the Classes?
Building the Static Model
Steps 4-6: Implementation, Testing, and Rollout?
12. Implementing Inheritance.
What Is Inheritance?
Inheritance and Derivation
The Animal Kingdom
The Syntax of Derivation
Private Versus Protected
Inheritance with Constructors and Destructors
Passing Arguments to Base Constructors
Overriding Base Class Functions
Hiding the Base Class Method
Calling the Base Method
How Virtual Functions Work
Trying to Access Methods from a Base Class
Creating Virtual Destructors
Virtual Copy Constructors
The Cost of Virtual Methods
13. Managing Arrays and Strings.
What Is an Array?
Accessing Array Elements
Writing Past the End of an Array
Fence Post Errors
Using Arrays of Objects
Declaring Multidimensional Arrays
Initializing Multidimensional Arrays
Building Arrays of Pointers
A Look at Pointer Arithmetic-An Advanced Topic
Declaring Arrays on the Free Store
A Pointer to an Array Versus an Array of Pointers
Pointers and Array Names
Deleting Arrays on the Free Store
Resizing Arrays at Runtime
char Arrays and Strings
Using the strcpy() and strncpy() Methods
Linked Lists and Other Structures
Creating Array Classes
Problems with Single Inheritance
Adding to Two Lists
The Parts of a Multiply Inherited Object
Constructors in Multiply Inherited Objects
Inheriting from Shared Base Class
Problems with Multiple Inheritance
Mixins and Capabilities Classes
Abstract Data Types
Pure Virtual Functions
Implementing Pure Virtual Functions
Complex Hierarchies of Abstraction
Which Classes Are Abstract?
Week 2 In Review
Week 3 At a Glance
Where You Are Going
15. Special Classes and Functions.
Sharing Data Among Objects of the Same Type: Static Member Data
Using Static Member Functions
Pointers to Functions
Why Use Function Pointers?
Arrays of Pointers to Functions
Passing Pointers to Functions to Other Functions
Using typedef with Pointers to Functions
Pointers to Member Functions
Arrays of Pointers to Member Functions
16. Advanced Inheritance.
Accessing Members of the Aggregated Class
Controlling Access to Aggregated Members
Cost of Aggregation
Copying by Value
Implementation in Terms of Inheritance Versus Aggregation/Delegation
Adding Friend Classes
Friend Functions and Operator Overloading
Overloading the Insertion Operator
17. Working with Streams.
Overview of Streams
Encapsulation of Data Flow
Streams and Buffers
Standard I/O Objects
Redirection of the Standard Streams
Input Using cin
The cin Return Value
Other Member Functions of cin
Single Character Input
Getting Strings from Standard Input
Peeking at and Returning Characters: peek() and putback()
Outputting with cout
Flushing the Output
Functions for Doing Output
Manipulators, Flags, and Formatting Instructions
Streams Versus the printf() Function
File Input and Output
Using the ofstream
Opening Files for Input and Output
Changing the Default Behavior of ofstream on Open
Binary Versus Text Files
18. Creating and Using Namespaces.
Resolving Functions and Classes by Name
Visibility of Variables
Static Global Variables
Creating a Namespace
Declaring and Defining Types
Defining Functions Outside a Namespace
Adding New Members
Using a Namespace
The using Keyword
The using Directive
The using Declaration
The Namespace Alias
The Unnamed Namespace
The Standard Namespace std
What Are Templates?
Building a Template Definition
Using the Name
Implementing the Template
Passing Instantiated Template Objects to Functions
Templates and Friends
Nontemplate Friend Classes and Functions
General Template Friend Class or Function
Using Template Items
Using Specialized Functions
Static Members and Templates
The Standard Template Library
Understanding Sequence Containers
Understanding Associative Containers
Working with the Algorithm Classes
20. Handling Errors and Exceptions.
Bugs, Errors, Mistakes, and Code Rot
The Idea Behind Exceptions
The Parts of Exception Handling
Causing Your Own Exceptions
Creating an Exception Class
Placing try Blocks and catch Blocks
How Catching Exceptions Work
Using More Than One catch Specification
Data in Exceptions and Naming Exception Objects
Exceptions and Templates
Exceptions Without Errors
A Word About Code Rot
Bugs and Debugging
21. What's Next.
The Preprocessor and the Compiler
The #define Preprocessor Directive
Using #define for Constants
Using #define for Tests
The #else Precompiler Command
Inclusion and Inclusion Guards
Why All the Parentheses?
The assert() Macro
Debugging with assert()
Using assert() Versus Exceptions
Printing Interim Values
Operator Exclusive OR
The Complement Operator
Long Lines and Function Length
Structuring switch Statements
Spelling and Capitalization of Names
Setting Up Access
Making Items Constant with const
Next Steps in Your C++ Development
Where to Get Help and Advice
Related C++ Topics: Managed C++, C#, and Microsoft's .NET
Staying in Touch
Week 3 In Review
A. Working with Numbers: Binary and Hexadecimal.
Using Other Bases
Converting to Different Bases
Why Base 2?
Bits, Bytes, and Nybbles
What's a KB?
B. C++ Keywords.
C. Operator Precedence.
E. A Look at Linked Lists.
The Component Parts of Your Linked List