C++ From The Ground Up Third Edition Book

Computer Programming and IT Books ITBooksHub.com
Click Here To Download Book
Pages 625
Views 1185
Size 2.3 MB
Download 305
Table Of Contents

1 The Story of C++

The Origins of C++

The Creation of C

Understanding the Need for C++

C++ Is Born

The Evolution of C++

What Is Object-Oriented Programming?




C++ Implements OOP

How C++ Relates to Java and C#

2 An Overview of C++

Your First C++ Program

Entering the Program

Compiling the Program

Run the Program

A Line-by-Line Explanation

Handling Syntax Errors

A Second Simple Program

A More Practical Example

A New Data Type

A Quick Review


A Program with Two Functions

Function Arguments

Functions Returning Values

The main( ) Function

The General Form of C++ Functions

Some Output Options

Two Simple Commands

The if Statement

The for Loop

Blocks of Code

Semicolons and Positioning

Indentation Practices

C++ Keywords

Identifiers in C++

The Standard C++ Library

3 The Basic Data Types

Declaration of Variables

Local Variables

Formal Parameters

Global Variables

Some Type Modifiers


Hexadecimal and Octal Literals

String Literals

Character Escape Sequences

Variable Initializations


Arithmetic Operators

Increment and Decrement

How C++ Got Its Name

Relational and Logical Operators


Type Conversion in Expressions

Converting to and from bool


Spacing and Parentheses

vi C++ from the Ground

4 Program Control Statements

The if Statement

The Conditional Expression

Nested ifs

The if-else-if Ladder

The for Loop

Some Variations on the for Loop

Missing Pieces

The Infinite Loop

Time Delay Loops

The switch Statement

Nested switch Statements

The while Loop

The do-while Loop

Using continue

Using break to Exit Loops

Nested Loops

Using the goto Statement

Putting Together the Pieces

5 Arrays and Strings

One-Dimensional Arrays

No Bounds Checking

Sorting an Array


Reading a String from the Keyboard

Some String Library Functions





Using the Null Terminator

Two-Dimensional Arrays

Multidimensional Arrays

Array Initialization

Unsized Array Initializations

Arrays of Strings

An Example Using String Arrays

6 Pointers

What Are Pointers?

The Pointer Operators

The Base Type Is Important

Assigning Values Through a Pointer


Pointer Expressions

Pointer Arithmetic

Pointer Comparisons

Pointers and Arrays

Indexing a Pointer

Are Pointers and Arrays Interchangeable?

Pointers and String Literals

A Comparison Example

Arrays of Pointers

The Null Pointer Convention

Multiple Indirection

Pointers and 16-bit Environments

Problems with Pointers

Uninitialized Pointers

Invalid Pointer Comparisons

Forgetting to Reset a Pointer

7 Functions, Part One: The Fundamentals

Scope Rules of Functions

Local Variables

Formal Parameters

Global Variables

Passing Pointers and Arrays

Calling Functions with Pointers

Calling Functions with Arrays

Passing Strings

argc and argv: Arguments to main( )

Passing Numeric Command Line Arguments

Converting Numeric Strings to Numbers

The return Statement

Returning from a Function

Returning Values

void Functions

Functions That Return Pointers

Function Prototypes

Headers: A Closer Look

Old-Style versus Modern Function Parame



8 Functions, Part Two: References, Overloadi

and Default Arguments

Two Approaches to Argument Passing

How C++ Passes Arguments

Using a Pointer to Create a Call-by-Reference

viii C++ from the Ground

Reference Parameters

Declaring Reference Parameters

Returning References

Creating a Bounded Array

Independent References

A Few Restrictions When Using References

Function Overloading

The overload Anachronism

Default Function Arguments

Default Arguments versus Overloading

Using Default Arguments Correctly

Function Overloading and Ambiguity

9 More Data Types and Operators

The const and volatile Qualifiers



Storage Class Specifiers



static Variables

Register Variables

The Origins of the register Modifier



More Operators

Bitwise Operators


The Shift Operators

The ? Operator

Compound Assignment

The Comma Operator

Multiple Assignments

Using sizeof

Dynamic Allocation Using new and delete

Initializing Dynamically Allocated Memory

Allocating Arrays

C’s Approach to Dynamic Allocation: malloc

and free( )

Precedence Summary

10 Structures and Unions


Accessing Structure Members

Arrays of Structures


A Simple Inventory Example

Passing Structures to Functions

Assigning Structures

Pointers to Structures and the Arrow Operator

References to Structures

Arrays and Structures Within Structures

C Structure Versus C++ Structures



Anonymous Unions

Using sizeof to Ensure Portability

Moving On to Object-Oriented Programming

11 Introducing the Class

Class Fundamentals

The General Form of a class

A Closer Look at Class Member Access

Constructors and Destructors

Parameterized Constructors

An Initialization Alternative

Classes and Structures Are Related

Structures versus Classes

Unions and Classes Are Related

Inline Functions

Creating Inline Functions Inside a Class

Arrays of Objects

Initializing Object Arrays

Pointers to Objects

Object References

12 A Closer Look at Classes

Friend Functions

Overloading Constructors

Dynamic Initialization

Applying Dynamic Initialization to Constructors

Assigning Objects

Passing Objects to Functions

Constructors, Destructors, and Passing Objects

A Potential Problem When Passing Objects

Returning Objects

A Potential Problem When Returning Objects

Creating and Using a Copy Constructor

Copy Constructors and Parameters

Copy Constructors and Initializations

x C++ from the Ground

Using Copy Constructors When an Object Is Returned

Copy Constructors—Is There a Simpler Way?

The this Keyword

13 Operator Overloading

Operator Overloading Using Member Functions

Using Member Functions to Overload Unary Operators

Operator Overloading Tips and Restrictions

Nonmember Operator Functions

Order Matters

Using a Friend to Overload a Unary Operator

Overloading the Relational and Logical Operators

A Closer Look at the Assignment Operator

Overloading [ ]

Overloading ( )

Overloading Other Operators

Another Example of Operator Overloading

14 Inheritance

Introducing Inheritance

Base Class Access Control

Using protected Members

Using protected for Inheritance of a Base Class

Reviewing public, protected, and private

Inheriting Multiple Base Classes

Constructors, Destructors, and Inheritance

When Constructors and Destructors Are Executed

Passing Parameters to Base Class Constructors

Granting Access

Reading C++ Inheritance Graphs

Virtual Base Classes

15 Virtual Functions and Polymorphism

Pointers to Derived Types

References to Derived Types

Virtual Functions

Virtual Functions Are Inherited

Why Virtual Functions?

A Simple Application of Virtual Functions

Pure Virtual Functions and Abstract Classes

Early versus Late Binding

Polymorphism and the Purist

16 Templates

Generic Functions

A Function with Two Generic Types

Explicitly Overloading a Generic Function

Overloading a Function Template

Using Standard Parameters with Template Functions

Generic Function Restrictions

Creating a Generic abs( ) Function

Generic Classes

An Example with Two Generic Data Types

Creating a Generic Array Class

Using Non-Type Arguments with Generic Classes

Using Default Arguments with Template Classes

Explicit Class Specializations

17 Exception Handling

Exception Handling Fundamentals

exit( ) and abort( )

Catching Class Types

Using Multiple catch Statements

Options for Exception Handling

Catching All Exceptions

Restricting Exceptions Thrown by a Function

Rethrowing an Exception

Handling Exceptions Thrown by new

The nothrow Alternative

Overloading new and delete

Overloading the nothrow Version of new

18 The C++ I/O System

Old VS Modern C++ I/O

C++ Streams

The C++ Predefined Streams

The C++ Stream Classes

Overloading the I/O Operators

Creating Inserters

Using Friend Functions to Overload Inserters

Overloading Extractors

C I/O Versus C++ I/O

Formatted I/O

Formatting with the ios Member Functions

Using I/O Manipulators

Creating Your Own Manipulator Functions

File I/O

Opening and Closing a File

Reading and Writing Text Files

Unformatted Binary I/O

Reading and Writing Blocks of Data

Detecting EOF

A File Comparison Example

xii C++ from the Ground

More Binary I/O Functions

Random Access

Checking I/O Status

Customized I/O and Files

19 Run-Time Type ID and the Casting Operators

Run-Time Type Identification (RTTI)

A Simple Application of Run-Time Type ID

typeid Can Be Applied to Template Classes

The Casting Operators





The Traditional Cast Versus the Four Casting Operators

20 Namespaces and Other Advanced Topics


Namespace Fundamentals

Unnamed Namespaces

The std Namespace

Pointers to Functions

Finding the Address of an Overloaded Function

Static Class Members

const Member Functions and mutable

Explicit Constructors

An Interesting Benefit from Impli

Constructor Conversion

The Member Initialization Syntax

Using the asm Keyword

Linkage Specification

The * and –>* Pointer-to-Member Operators

Creating Conversion Functions

21 Introducing the Standard Template Library

An Overview of the STL

The Container Classes

Accessing a Vector Through an Iterator

Inserting and Deleting Elements in a Vector

Storing Class Objects in a Vector

The Power of Iterators

Sort a List

Merging One List with Another

Storing Class Objects in a List


Storing Class Objects in a Map



Removing and Replacing Elements

Reversing a Sequence

Transforming a Sequence

Exploring the Algorithms

The string Class

Some string Member Functions

Putting Strings into Other Containers

Final Thoughts on the STL

22 The C++ Preprocessor


Function-Like Macros



Conditional Compilation Directives

#if, #else, #elif, and #endif

#ifdef and #ifndef


Using defined

The Diminishing Role of the Preprocessor



The # and ## Preprocessor Operators

Predefined Macro Names

Final Thoughts

A C-Based I/O

C I/O Uses Streams

Understanding printf( ) and scanf( )

printf( )

scanf( )

The C File System

fopen( )

fputc( )

fgetc( )

feof( )

fclose( )

Using fopen( ), fgetc( ), fputc( ), and fclose( )

ferror( ) and rewind( )

fread( ) and fwrite( )

xiv C++ from the Ground

fseek( ) and Random-Access I/O

fprintf( ) and fscanf( )

Erasing Files

B Working with an Older C++ Compiler

Two Simple Changes

C The NET Managed Extensions to C++

The NET Keyword Extensions

_ _abstract

_ _box

_ _delegate

_ _event

_ _finally

_ _gc

_ _identifier

_ _interface

_ _nogc

_ _pin

_ _property

_ _sealed

_ _try_cast

_ _typeof

_ _value

Preprocessor Extensions

The attribute Attribute

Compiling Managed C++