WELCOME ro Beginning ANSI C++: The Complete Language, Third Edition. This is a tuto- rial guide to Standard C++. During the course of the book you'll cover all the funda- mentals of syntax, grammar, object-oriented capability, and the principal features of the standard library. You'll soon gain enough programming know-how to write your own C++ applications. Why C++? C++ is arguably the most widely used programming language in existence. It's used in professional application development because of its immense flexibility, power, and efficiency. For high-performance code across a vast range of programming contexts, C++ is unrivalled. It's also much more accessible than many people assume. With the right guidance, getting a grip on C++ is easier than you might imagine. By developing your C++ skills, you'll learn a language already used by millions, and you'll acquire a new tool in your programming toolbox that is likely to be more powerful than any of the others. The Standard for C++ In 1998, the International Standard for C++, ISO/IEC 14882, was finally approved and adopted by the American National Standards Institute (ANSI) and the International Committee for Information Technology Standards (INCITS). This was the culmination of nine years of work by a joint ANSI/ISO committee whose objective was to establish a single definition of the C++ programming language that would be accepted worldwide.
Author(s): Ivor Horton
Edition: 3
Publisher: Apress
Year: 2004
Language: English
Pages: 1091
Ivor Horton s Beginning ANSI C++: The Complete Language, Third Edition
Contents at a Glance
Contents
About the Author
About the Technical Reviewer
Acknowledgments
Introduction
Why C++?
The Standard for C++
Using the Book
Basic Ideas
Programming Languages
A Potted History
Interpreted vs. Compiled Program Execution
Libraries
Why Is C++ Such a Great Language?
The ANSI/ISO Standard for C++
A Simple C++ Program
Names
Namespaces
Keywords
C++ Statements and Statement Blocks
Code Presentation Style
Program Structure
Program Functions and Execution
Creating an Executable from Your Source Files
Compiling
Linking
C++ Source Characters
The Universal Character Set
Trigraph Sequences
Escape Sequences
Whitespace in Statements
Documenting Your Programs
The Standard Library
Programming in C++
Procedural and Object-Oriented Programming
Summary
Exercises
Basic Data Types and Calculations
Data and Data Types
Performing Simple Calculations
Introducing Literals
Integer Literals
Integer Arithmetic
Operator Precedence and Associativity
Using Variables
Variable Names
Integer Variables
Integer Variable Types
Integer Ranges
The Type of an Integer Literal
The Assignment Operator
Multiple Assignments
Modifying the Value of a Variable
Incrementing and Decrementing Integers
Postfix Increment and Decrement Operations
The const Keyword
Numerical Functions for Integers
Generating Random Numbers
Floating-Point Operations
Floating-Point Data Types
Floating-Point Operations
Working with Floating-Point Values
Mathematical Functions
Working with Characters
Character Literals
Initializing char Variables
Working with Extended Character Sets
Functional Notation for Initial Values
Summary
Exercises
More on Handling Basic Data Types
Mixed Expressions
Assignments and Different Types
Explicit Casts
Old-Style Casts
Finding Out About Types
Finding the Limits
Bitwise Operators
The Bitwise Shift Operators
Logical Operations on Bit Patterns
Enumerated Data Types
Anonymous Enumerations
Casting Between Integer and Enumeration Types
Synonyms for Data Types
The Lifetime of a Variable
Automatic Variables
Positioning Variable Declarations
Global Variables
Static Variables
The volatile Type Modifier
Declaring External Variables
Precedence and Associativity
Summary
Exercises
Choices and Decisions in Your Programs
Comparing Data Values
Applying the Comparison Operators
Comparing Floating Point Values
The if Statement
Nested if Statements
The if-else Statement
Nested if-else Statements
Logical Operators
Logical AND
Logical OR
Logical Negation
The Conditional Operator
The switch Statement
Unconditional Branching
Decision Statement Blocks and Variable Scope
Summary
Exercises
Loops: Repeating One or More Statements
Understanding Loops
The while Loop
The do-while Loop
More Complex while Loop Conditions
The for Loop
Loops and Variable Scope
Controlling a for Loop with Floating-Point Values
Using More Complex Loop Control Expressions
Nested Loops
Skipping Loop Iterations
Breaking Out of a Loop
Indefinite Loops
Summary
Exercises
Arrays and Strings
Data Arrays
Using an Array
Initializing Arrays
Arrays of Characters
Multidimensional Arrays
Initializing Multidimensional Arrays
Multidimensional Character Arrays
A Better Class of String
Declaring string Objects
Operations with String Objects
Accessing Characters in a String
Accessing Substrings
Comparing Strings
Searching a String
Modifying a String
Arrays of Type string
Wide-Character Strings
Summary
Exercises
Pointers
What Is a Pointer?
Declaring a Pointer
Using Pointers
Initializing Pointers
Initializing Pointers to Type char
Constant Pointers and Pointers to Constants
Pointers and Arrays
Pointer Arithmetic
Using Pointer Notation with an Array Name
Using Pointers with Multidimensional Arrays
Operations on C-Style Strings
Dynamic Memory Allocation
The Free Store (aka the Heap)
The Operators new and delete
Dynamic Memory Allocation for Arrays
Hazards of Dynamic Memory Allocation
Converting Pointers
Summary
Exercises
Programming with Functions
Segmenting Your Programs
Why You Should Segment Your Programs into Functions
Understanding Functions
Defining a Function
Function Declarations
Passing Arguments to a Function
The Pass-by-Value Mechanism
The Pass-by-Reference Mechanism
Arguments to main()
Default Argument Values
Multiple Default Parameter Values
Returning Values from a Function
Returning a Pointer
Returning a Reference
Returning a New Variable from a Function
Inline Functions
Static Variables
Summary
Exercises
More on Functions
Function Overloading
The Signature of a Function
Overloading and Pointer Parameters
Overloading and Reference Parameters
Overloading and const Parameters
Overloading and Default Argument Values
A Sausage Machine for Functions
Creating Instances of a Function Template
Explicitly Specifying a Template Parameter
Specialization of Templates
Function Templates and Overloading
Templates with Multiple Parameters
Pointers to Functions
Declaring Pointers to Functions
Passing a Function as an Argument
Arrays of Pointers to Functions
Recursion
Using Recursion
Summary
Exercises
Program Files and Preprocessing Directives
Working with Program Files
The Scope of a Name
The “One Definition” Rule
Program Files and Linkage
External Names
Namespaces
The Global Namespace
Defining a Namespace
Functions and Namespaces
Function Templates and Namespaces
Extension Namespaces
Unnamed Namespaces
Namespace Aliases
Nested Namespaces
Preprocessing Your Source Code
Including Header Files in Your Programs
Substitutions in Your Program
Macro Substitutions
Preprocessing Directives on Multiple Lines
Strings As Macro Arguments
Joining the Arguments in a Macro Expansion
Logical Preprocessing Directives
The Logical #if Directive
Directives Testing for Specific Values
Multiple Choice Code Selection
Standard Preprocessing Macros
The #error and #pragma Directives
Debugging Methods
Integrated Debuggers
Preprocessing Directives in Debugging
Using the assert() Macro
Summary
Exercises
Creating Your Own Data Types
Introducing Objects
The Structure in C++
Understanding Structures
Defining a Structure Type
Creating Objects of a Structure Type
Accessing the Members of a Structure Object
Using Pointers with a Structure
Understanding Unions
Declaring Unions
Anonymous Unions
More Complex Structures
Structures with Structures As Members
Summary
Exercises
Classes: Defining Your Own Data Types
Classes and Object-Oriented Programming
Encapsulation
Inheritance
Polymorphism
Terminology
Defining a Class
Constructors
Placing Constructor Definitions Outside the Class
The Default Constructor
Default Initialization Values
Using an Initializer List in a Constructor
Use of the explicit Keyword
Private Members of a Class
Accessing Private Class Members
The Default Copy Constructor
Friends
The Friend Functions of a Class
Friend Classes
The Pointer Named this
Objects That Are const and const Member Functions
Mutable Data Members of a Class
Casting Away const
Arrays of Objects of a Class
The Size of a Class Object
Static Members of a Class
Static Data Members of a Class
Static Member Functions of a Class
Summary
Exercises
Class Operations
Pointers and References to Class Objects
Pointers As Data Members
Defining the Package Class
Defining the TruckLoad Class
Implementing the TruckLoad Class
Controlling Access to a Class
Nested Classes
The Importance of the Copy Constructor
Implementing the Copy Constructor
Dynamic Memory Allocation Within an Object
What Is a Destructor?
Defining the Destructor
The Default Destructor
Implementing a Destructor
References in Classes
References As Members of a Class
Summary
Exercises
Operator Overloading
Implementing Operators for Your Classes
Operator Overloading
Operators That Can Be Overloaded
Implementing an Overloaded Operator
Global Operator Functions
Implementing Full Support for an Operator
Operator Function Idioms
Overloading the Assignment Operator
Overloading the Arithmetic Operators
Overloading the Subscript Operator
Overloading Type Conversions
Overloading the Increment and Decrement Operators
Smart Pointers
Overloading Operators new and delete
Summary
Exercises
Inheritance
Classes and Object-Oriented Programming
Hierarchies
Inheritance in Classes
Inheritance vs. Aggregation
Deriving Classes from a Base Class
Access Control Under Inheritance
Declaring Members of a Class As protected
The Access Level of Inherited Class Members
Using Access Specifiers in Class Hierarchies
Changing the Access Specification of Inherited Members
Constructor Operation in a Derived Class
The Copy Constructor in a Derived Class
Destructors Under Inheritance
The Order in Which Destructors Are Called
Duplicate Member Names
Duplicate Function Member Names
Multiple Inheritance
Multiple Base Classes
Inherited Member Ambiguity
Repeated Inheritance
Virtual Base Classes
Converting Between Related Class Types
Summary
Exercises
Virtual Functions and Polymorphism
Understanding Polymorphism
Using a Base Class Pointer
Calling Inherited Functions
Virtual Functions
Default Argument Values in Virtual Functions
Using References to Call Virtual Functions
Calling the Base Class Version of a Virtual Function
Converting Between Pointers to Class Objects
Dynamic Casts
The Cost of Polymorphism
Pure Virtual Functions
Abstract Classes
Indirect Abstract Base Classes
Destroying Objects Through a Pointer
Virtual Destructors
Identifying Types at Runtime
Pointers to Class Members
Pointers to Data Members
Pointers to Member Functions
Summary
Exercises
Program Errors and Exception Handling
Handling Errors
Understanding Exceptions
Throwing an Exception
Code That Causes an Exception to Be Thrown
Nested try Blocks
Class Objects As Exceptions
Matching a Catch Handler to an Exception
Catching Derived Class Exceptions with a Base Class Handler
Rethrowing an Exception
Catching All Exceptions
Functions That Throw Exceptions
Function try Blocks
Throwing Exceptions in Constructors
Exceptions and Destructors
Standard Library Exceptions
Standard Library Exception Classes
Using Standard Exceptions
Summary
Exercises
Using Class Templates To Create Families of Classes
Understanding Class Templates
Applications of Class Templates
Defining Class Templates
Template Parameters
A Simple Class Template
Instantiating a Class Template
Static Members of a Class Template
Non-Type Class Template Parameters
Non-Type Parameter Example
Default Template Argument Values
Explicit Template Instantiation
Friends of Class Templates
Special Cases
Partial Template Specialization
Class Templates with Nested Classes
Defining Function Templates for Members
More Advanced Class Templates
Summary
Exercises
Input and Output Operations
Input and Output in C++
Understanding Streams
Advantages of Using Streams
Stream Classes
Standard Streams
Stream Insertion and Extraction Operations
Stream Manipulators
File Streams
Writing to a File
Reading from a File
Setting the File Open Mode
Unformatted Stream Operations
Unformatted Stream Input
Unformatted Stream Output
Errors in Stream Input/Output
Input/Output Errors and Exceptions
Using Binary Mode Stream Operations
Writing Numeric Data in Binary Form
Read/Write Operations on a Stream
Random Access to a Stream
String Streams
Objects and Streams
Overloading the Insertion Operator for Class Objects
Overloading the Extraction Operator for Class Objects
More Complex Objects in Streams
Summary
Exercises
Introducing the Standard Template Library
An Introduction to the STL Architecture
STL Components
STL Headers
Using the vector Container
Creating a vector Container
Accessing Elements in a vector Container
Basic Operations on a vector Container
Using vector Containers with Array Operations
Using an Input Stream Iterator
Creating Your Own Iterators
Passing Iterators to an Algorithm
STL Iterator Type Requirements
The STL Iterator Member Function Requirements
Insert Iterators
The list Container
Creating list Containers
Accessing Elements in a List
Operations on Lists
The Associative Containers
Using a map Container
Performance and Specialization
Summary
Exercises
ASCII Codes
C++ Keywords
Standard Library Headers
Language Support
Input/Output
Diagnostics
General Utilities
Strings
Containers
Iterator Support
General Purpose Algorithms
Numerical Operations
Localization
Operator Precedence and Associativity
Understanding Binary and Hexadecimal Numbers
Binary Numbers
Hexadecimal Numbers
Negative Binary Numbers
Big-Endian and Little-Endian Systems
Example Project
Outline
Improving the Project Specification
Developer’s Notes
The Person Class
The
Classes
The Container
Saving and Restoring Data
Index