Day 1                                                                              

Introduction

A Brief History of C++

Programs

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

HELLO.CPPYour First C++ Program

Listing 1.1. HELLO.CPP, the Hello World program.

Compile Errors

Listing 1.2. Demonstration of compiler error.

 

 

Day 2

Listing 2.1. HELLO.CPP demonstrates the parts of a C++ program.

A Brief Look at cout

Listing 2.2.

Using cout.

Comments

Using Comments

Listing 2.3. HELP.CPP demonstrates comments.

Comments at the Top of Each File

A Final Word of Caution About Comments

Functions

Listing 2.4. Demonstrating a call to a function.

Using Functions

Listing 2.5. FUNC.CPP demonstrates a simple function.

 

Day 3

Types of Comments

Variables and Constants

What Is a Variable?

Figure 3.1.

Setting Aside Memory

Size of Integers

Listing 3.1. Determining the size of variable types

on your computer.

signed and unsigned

Fundamental Variable Types

Defining a Variable

Case Sensitivity

Keywords

Creating More Than One Variable at a Time

Assigning Values to Your Variables

Listing 3.2. A demonstration of the use of variables.

typedef

Listing 3.3. A demonstration of typedef.

When to Use short and When to Use long

Wrapping Around an unsigned Integer

Listing 3.4.

A demonstration of putting too large a value in an unsigned integer.

Wrapping Around a signed Integer

Listing 3.5.

A demonstration of adding too large a number to a signed integer.

Characters

Characters and Numbers

Listing 3.6. Printing characters based on numbers.

Special Printing Characters

Constants

Literal Constants

Symbolic Constants

Enumerated Constants

Listing 3.7. A demonstration of enumerated constants

 

Day 4

Expressions and Statements

Statements

Whitespace

Blocks and Compound Statements

Expressions

Listing 4.1. Evaluating complex expressions.

Operators

Assignment Operator

Mathematical Operators

Listing 4.2. A demonstration of subtraction and integer overflow.

Integer Division and Modulus

Combining the Assignment and Mathematical Operators

Increment and Decrement

Prefix and Postfix

Listing 4.3. A demonstration of prefix and postfix operators.

Precedence

Nesting Parentheses

The Nature of Truth

Relational Operators

The if Statement

Listing 4.4. A demonstration of branching based on relational operators.

Indentation Styles

else

Listing 4.5. Demonstrating the else keyword.

The if Statement

Advanced if Statements

Listing 4.6. A complex, nested if statement.

Using Braces in Nested if Statements

Listing 4.7. A demonstration of why

braces help clarify which else statement goes with which if statement.

Listing 4.8. A demonstration of the proper use of braces with an if statement.

Logical Operators Logical AND  OR  NOT

Relational Precedence

More About Truth and Falsehood

Conditional (Ternary) Operator

Listing 4.9. A demonstration of the conditional operator.

 

Day 5

Functions

What Is a Function?

Declaring and Defining Functions

Declaring the Function

Function Prototypes

Listing 5.1. A function declaration

and the definition and use of that function.

Defining the Function

Functions

Execution of Functions

Local Variables

Listing 5.2. The use of local variables and parameters.

Global Variables

Listing 5.3. Demonstrating global and local variables.

Global Variables: A Word of Caution

More on Local Variables

Listing 5.4. Variables scoped within a block.

Function Statements

Function Arguments

Using Functions as Parameters to Functions

Parameters Are Local Variables

Listing 5.5. A demonstration of passing by value.

Return Values

Listing 5.6. A demonstration of multiple return statements.

Default Parameters

Listing 5.7. A demonstration of default parameter values.

Overloading Functions

Listing 5.8. A demonstration of function polymorphism.

Special Topics About Functions

Inline Functions

Listing 5.9. Demonstrates an inline function.

Recursion

Listing 5.10. Demonstrates recursion using the Fibonacci series.

How Functions WorkA Look Under the Hood

Levels of Abstraction

Partitioning RAM

The Stack and Functions

 

Day 6

Basic Classed

Creating New Types

Why Create a New Type?

Classes and Members

Declaring a Class

A Word on Naming Conventions

Defining an Object

Classes Versus Objects

Accessing Class Members

Assign to Objects, Not to Classes

If You Dont Declare It, Your Class Wont Have It

Private Versus Public

Listing 6.1. Accessing the public members of a simple class.

Make Member Data Private

Listing 6.2. A class with accessor methods.

Privacy Versus Security

The class keyword

Implementing Class Methods

Listing 6.3. Implementing the methods of a simple class.

Constructors and Destructors

Default Constructors and Destructors

Listing 6.4. Using constructors and destructors.

const Member Functions

Interface Versus Implementation

Listing 6.5. A demonstration of violations of the interface.

Why Use the Compiler to Catch Errors?

Where to Put Class Declarations and Method Definitions

Inline Implementation sting 6.6. Cat class declaration in CAT.HPP.

Listing 6.7. Cat implementation in CAT.CPP.

Classes with Other Classes as Member Data

Listing 6.8. Declaring a complete class.

Listing 6.9. RECT.CPP.

Structures

Why Two Keywords Do the Same Thing

 

 

Day 7

More Program Flow

Looping

The Roots of Looping goto

Listing 7.1. Looping with the keyword goto.

Why goto Is Shunned

The goto Statement

while Loops

Listing 7.2. while loops.

The while Statement

More Complicated while Statements

Listing 7.3. Complex while loops.

continue and break

Listing 7.4. break and continue.

The continue Statement

The break Statement

while (1) Loops

Listing 7.5. while (1) loops.

do...while Loops

Listing 7.6. Skipping the body of the while Loop.

do...while

Listing 7.7. Demonstrates do...while loop.

The do...while Statement

for Loops

Listing 7.8. While reexamined.

Listing 7.9. Demonstrating the for loop.

The for Statement

Advanced for Loops

Listing 7.10. Demonstrating multiple statements in for loops.

Listing 7.11. Null statements in for loops.

Listing 7.12. Illustrating empty for loop statement.

Empty for Loops

Listing 7.13. Illustrates the null statement in a for loop.

Nested Loops

Listing 7.14. Illustrates nested for loops.

Scoping in for Loops

Summing Up Loops

Listing 7.15. Solving the nth Fibonacci number

using iteration.

switch Statements

Listing 7.16. Demonstrating the switch statement.

The switch Statement

Using a switch Statement with a Menu

Listing 7.17. Demonstrating a forever loop.

 

 

Day 8

Pointers

What Is a Pointer?

Listing 8.1. Demonstrating address of variables

Storing the Address in a Pointer

Pointer Names

The Indirection Operator

Pointers, Addresses, and Variables

Manipulating Data by Using Pointers

Listing 8.2. Manipulating data by using pointers

Examining the Address

Listing 8.3. Finding out what is stored in pointers

Pointers

Why Would You Use Pointers?

The Stack and the Free Store

new

delete

Listing 8.4. Allocating, using, and deleting pointers.

Memory Leaks

Creating Objects on the Free Store

Deleting Objects

Listing 8.5. Creating and deleting objects on the free store

Accessing Data Members

Listing 8.6. Accessing member data of objects on the free store.

Member Data on the Free Store

Listing 8.7. Pointers as member data

The this Pointer

Listing 8.8. Using the this pointer

Stray or Dangling Pointers

Listing 8.9. Creating a stray pointer

const Pointers

const Pointers and const Member Functions

Listing 8.10. Using pointers to const objects

const this Pointers

 

Day 9

References

What Is a Reference?

Listing 9.1. Creating and using references.

Using the Address of Operator & on References

Listing 9.2. Taking the address of a reference

Listing 9.3. Assigning to a reference

What Can Be Referenced?

Listing 9.4. References to objects

References

Null Pointers and Null References

Passing Function Arguments by Reference

Listing 9.5. Demonstrating passing by value

Making swap() Work with Pointers

Listing 9.6. Passing by reference using pointers

Implementing swap() with References

Listing 9.7. swap() rewritten with references

Understanding Function Headers and Prototypes

Returning Multiple Values

Listing 9.8. Returning values with pointers

Returning Values by Reference

Listing 9.9.

Listing 9.8 rewritten using references.

Passing by Reference for Efficiency

Listing 9.10. Passing objects by reference

Passing a const Pointer

Listing 9.11. Passing const pointers

References as an Alternative

Listing 9.12. Passing references to objects

const References

When to Use References and When to Use Pointers

Mixing References and Pointers

Dont Return a Reference to an Object that Isnt in Scope!

Listing 9.13. Returning a reference to a non-existent object

Returning a Reference to an Object on the Hea

Listing 9.14. Memory leaks

Pointer, Pointer, Who Has the Pointer?

 

Day 10

Advanced Functions

Overloaded Member Functions

Listing 10.1. Overloading member functions.

Using Default Values

Listing 10.2. Using default values.

Choosing Between Default Values and Overloaded Functions

The Default Constructor

Overloading Constructors

Listing 10.3. Overloading the constructor.

Initializing Objects

Listing 10.4. A code snippet showing initialization of member variables.

The Copy Constructor

Listing 10.5. Copy constructors.

Operator Overloading

Listing 10.6. The Counter class.

Writing an Increment Function

Listing 10.7. Adding an increment operator.

Overloading the Prefix Operator

Listing 10.8. Overloading operator++.

Returning Types in Overloaded Operator Functions

Listing 10.9. Returning a temporary object.

Returning Nameless Temporaries

Listing 10.10. Returning a nameless temporary object.

Using the this Pointer

Listing 10.11. Returning the this pointer.

Overloading the Postfix Operator

Difference Between Prefix and Postfix

Listing 10.12. Prefix and postfix operators.

Operator Overloading Unary Operators

The Addition Operator

Listing 10.13. The Add() function.

Overloading operator+

Listing 10.14. operator+.

Operator Overloading: Binary Operators

Issues in Operator Overloading

Limitations on Operator Overloading

What to Overload

The Assignment Operator

Listing 10.15. An assignment operator.

Conversion Operators

Listing 10.16. Attempting to assign a Counter to a USHORT.

Listing 10.17. Converting USHORT to Counter.

Conversion Operators

Listing 10.18. Converting from Counter to unsigned short().

 

Day 11

Arrays

What Is an Array?

Array Elements

Listing 11.1. Using an integer array.

Writing Past the End of an Array

Listing 11.2. Writing past the end of an array.

Fence Post Errors

Initializing Arrays

Declaring Arrays

Listing 11.3. Using consts and enums in arrays.

Arrays

Arrays of Objects

Listing 11.4. Creating an array of objects.

Multidimensional Arrays

Initializing Multidimensional Arrays

Listing 11.5. Creating a multidimensional array.

A Word About Memory

Arrays of Pointers

Listing 11.6. Storing an array on the free store.

Declaring Arrays on the Free Store

A Pointer to an Array Versus an Array of Pointers

Pointers and Array Names

Listing 11.7. Creating an array by using new.

Deleting Arrays on the Free Store

char Arrays

Listing 11.8. Filling an array.

Listing 11.9. Filling an array.

strcpy() and strncpy()

Listing 11.10. Using strcpy().

Listing 11.11. Using strncpy().

String Classes

Listing 11.12. Using a String class.

Linked Lists and Other Structures

Listing 11.13. Implementing a linked list.

Array Classes

 

 

Day 12

Inheritance

What Is Inheritance?

Inheritance and Derivation

Figure 12.1.

The Animal Kingdom

The Syntax of Derivation

Listing 12.1. Simple inheritance.

Private Versus Protected

Listing 12.2. Using a derived object.

Constructors and Destructors

Listing 12.3. Constructors and destructors called.

Passing Arguments to Base Constructors

Listing 12.4. Overloading constructors in derived classes.

Overriding Functions

Listing 12.5. Overriding a base class method

in a derived class.

Overloading Versus Overriding

Hiding the Base Class Method

Listing 12.6. Hiding methods.

Overriding Versus Hiding

Calling the Base Method

Listing 12.7. Calling base method from overridden method.

Virtual Methods

Listing 12.8. Using virtual methods.

Listing 12.9. Multiple virtual functions called in turn.

How Virtual Functions Work

You Cant Get There from Here

Slicing

Listing 12.10. Data slicing when passing by value.

Virtual Destructors

Virtual Copy Constructors

Listing 12.11. Virtual copy constructor.

The Cost of Virtual Methods

 

Day 13

Polymorphism

Problems with Single Inheritance

Listing 13.1. If horses could fly...

Percolating Upward

Casting Down

Listing 13.2. Casting down.

Adding to Two Lists

Multiple Inheritance

Listing 13.3. Multiple inheritance.

Declaring Multiple Inheritance

The Parts of a Multiply Inherited Object

Constructors in Multiply Inherited Objects

Listing 13.4. Calling multiple constructors.

Ambiguity Resolution

Inheriting from Shared Base Class

Listing 13.5. Common base classes.

Virtual Inheritance

Listing 13.6. Illustration of the use of virtual inheritance.

Declaring Classes for Virtual Inheritance

Problems with Multiple Inheritance

Mixins and Capabilities Classes

Abstract Data Types

Listing 13.7. Shape classes.

Pure Virtual Functions

Listing 13.8. Abstract Data Types.

Abstract Data Types

Implementing Pure Virtual Functions

Listing 13.9. Implementing pure virtual functions.

Complex Hierarchies of Abstraction

Listing 13.10. Deriving ADTs from other ADTs.

Which Types Are Abstract?

The Observer Pattern

A Word About Multiple Inheritance, Abstract Data Types, and Java

 

Day 14

Special Classes and Functions

Static Member Data

Listing 14.1. Static member data.

Listing 14.2. Accessing static members without an object.

Listing 14.3. Accessing static members using non-static member functions.

Static Member Functions

Listing 14.4. Static member functions.

Static Member Functions

Pointers to Functions

Listing 14.5. Pointers to functions.

Pointer to Function

Why Use Function Pointers?

Listing 14.6. Rewriting Listing 14.5 without the pointer to function.

Shorthand Invocation

Arrays of Pointers to Functions

Listing 14.7. Demonstrates use of an array of pointers to functions.

Passing Pointers to Functions to Other Functions

Listing 14.8. Passing pointers to functions

as function arguments.

Using typedef with Pointers to Functions

Listing 14.9. Using typedef to make pointers to functions more readable.

Pointers to Member Functions

Listing 14.10. Pointers to member functions.

Arrays of Pointers to Member Functions

Listing 14.11. Array of pointers to member functions.

 

 

Day 15

Advanced Inheritance

Containment

Listing 15.1. The String class.

Listing 15.2. The Employee class and driver program.

Accessing Members of the Contained Class

Filtering Access to Contained Members

Cost of Containment

Listing 15.3. Contained class constructors.

Copying by Value

Listing 15.4. Passing by value.

Implementation in Terms of Inheritance/Containment Versus Delegation

Delegation

Listing 15.5. Delegating to a contained LinkedList.

Private Inheritance

Listing 15.6. Private

inheritance.

Friend Classes

Listing 15.7. Friend class illustrated.

Friend Class

Friend Functions

Friend Functions and Operator Overloading

Listing 15.8. Friendly operator+.

Friend Functions

Overloading the Insertion Operator

Listing 15.9. Overloading operator<<().

 

 

Day 16

Streams

Overview of Streams

Encapsulation

Buffering

Streams and Buffers

Standard I/O Objects

Redirection

Input Using cin

Listing 16.1. cin handles different data types.

Strings

String Problems

Listing 16.2. Trying to write more than one word to cin.

Listing 16.3. Multiple input.

operator>> Returns a Reference to an istream Object

Other Member Functions of cin

Single Character Input

Listing 16.4. Using get() with no parameters.

Listing 16.5 Using get() with parameters.

Getting Strings from Standard Input

Listing 16.6. Using get() with a character array

Listing 16.7. Using getline().

Using cin.ignore()

Listing 16.8. Using ignore().

peek() and putback()

Listing 16.9. Using peek() and putback().

Output with cout

Flushing the Output

Related Functions

Listing 16.10. Using put().

Listing 16.11. Using write().

Manipulators, Flags, and Formatting Instructions

Using cout.width()

Listing 16.12. Adjusting the width of output.

Setting the Fill Characters

Listing 16.13. Using fill().

Set Flags

Listing 16.14. Using setf.

Streams Versus the printf() Function

Listing 16.15. Printing with printf().

File Input and Output

ofstream

Condition States

Opening Files for Input and Output

Listing 16.16. Opening files for read and write.

Changing the Default Behavior of ofstream on Open

Listing 16.17. Appending to the end of a file

Binary Versus Text Files

Listing 16.18. Writing a class to a file.

Command-Line Processing

Listing 16.19. Using command-line arguments.

Listing 16.20. Using command-line arguments.

 

Day 17

The Preprocessor

The Preprocessor and the Compiler

Seeing the Intermediate Form

Using #define

Using #define for Constants

Using #define for Tests

The #else Precompiler Command

Listing 17.1. Using #define.

Inclusion and Inclusion Guards

Defining on the Command Line

Undefining

Listing 17.2. Using #undef.

Conditional Compilation

Macro Functions

Why All the Parentheses?

Listing 17.3. Using parentheses in macros.

Macros Versus Functions and Templates

Inline Functions

Listing 17.4. Using

inline rather than a macro.

String Manipulation

Stringizing

Concatenation

Predefined Macros

assert()

Listing 17.5. A simple assert() macro.

Debugging with assert()

assert() Versus Exceptions

Side Effects

Class Invariants

Listing 17.6. Using Invariants().

Printing Interim Values

Listing 17.7. Printing values in DEBUG mode.

Debugging Levels

Listing 17.8. Levels of debugging.

 

 

Day 18

Object-Oriented Analysis and Design

The Development Cycle

Simulating an Alarm System

Preliminary Design

What Are the Objects?

Other Objects

What Are the Classes?

How Are Alarms Reported?

Event Loops

Listing 18.1. A simple event loop.

PostMaster

Measure Twice, Cut Once

Divide and Conquer

Message Format

Initial Class Design

Rooted Hierarchies Versus Non-Rooted Hierarchies

Designing the Interfaces

Building a Prototype

The 80/80 Rule

Designing the PostMasterMessage Class

Application Program Interface

Listing 18.2. PostMasterMessages interface.

Programming in Large Groups

Ongoing Design Considerations

Design Decisions

Decisions, Decisions

Working with Driver Programs

Listing 18.3. A driver program for PostMasterMessage.

 

Day 19

Templates

What Are Templates?

Parameterized Types

Template Definition

Listing 19.1. A template of an Array class.

Using the Name

Implementing the Template

Listing 19.2. The implementation of the template array.

Template Functions

Templates and Friends

Non-Template Friend Classes and Functions

Listing 19.3. Non-template friend function.

General Template Friend Class or Function

Listing 19.4. Using operator ostream.

A Type-Specific Template Friend Class or Function

Using Template Items

Listing 19.5. Passing template objects

to and from functions.

Specialized Functions

Listing 19.6. Specializing template implementations.

Static Members and Templates

Listing 19.7. Using static member data and functions

with templates.

The Standard Template Library

 

 

 

 

 

 

 

 

 

 

 

 

Appendix

Operator Precedence.

KeyWords

Binary

 

Day 20

Exceptions and Error Handling

Bugs, Errors, Mistakes, and Code Rot

Exceptions

A Word About Code Rot

Exceptions

How Exceptions Are Used

Listing 20.1. Raising an exception.

try Blocks

catch Blocks

Using try Blocks and catch Blocks

Catching Exceptions

More Than One catch Specification

Listing 20.2. Multiple exceptions.

Exception Hierarchies

Listing 20.3. Class hierarchies and exceptions.

Data in Exceptions and Naming Exception Objects

Listing 20.4. Getting data out of an exception object.

Listing 20.5.

Passing by reference and using virtual functions in exceptions.

Exceptions and Templates

Listing 20.6. Using exceptions with templates.

Exceptions Without Errors

Bugs and Debugging

Breakpoints

Watch Points

Examining Memory

Assembler

 

 

 

 

 

 

 

 

 

 

 

 

Facit

Day 21

Whats Next

The Standard Libraries

String

Listing 21.1. strlen().

strcpy() and strncpy()

Listing 21.2. Using strcpy.

Listing 21.3. Using strncpy().

strcat() and strncat()

Listing 21.4. Using strcat() and strncat().

Other String Functions

Time and Date

Listing 21.5. Using ctime().

stdlib

Listing 21.6. Using atoi() and related functions.

qsort()

Listing 21.7. Using qsort().

Other Libraries

Bit Twiddling

Operator AND

Operator OR

Operator Exclusive OR

The Complement Operator

Setting Bits

Clearing Bits

Flipping Bits

Bit Fields

Listing 21.8. Using bit fields.

Style

Indenting

Braces

Long Lines

switch Statements

Program Text

Identifier Names

Spelling and Capitalization of Names

Comments

Access

Class Definitions

include Files

assert()

const

Next Steps

Where to Get Help and Advice

Required Reading

Magazines

Staying in Touch