C++ Primer, 4th Edition
Read an excerpt:
Chapter 1: Getting Started
Excerpt provided courtesy of Addison-Wesley Professional. Copyright © Addison-Wesley. Written permission from the publisher is required for any use of this material.
|
Customer Reviews: 2 Average Customer Rating:      Write a Review and tell the world about this title! People who purchase this book frequently purchase: - Effective C++: 55 Specific Ways to Improve Your Programs and Designs, 3rd Edition; Scott Meyers, $38.95, 22% Off!
- C++ Common Knowledge: Essential Intermediate Programming; Stephen C. Dewhurst, $27.50, 21% Off!
- The C++ Standard Library: A Tutorial and Reference; Nicolai M. Josuttis, $57.95, 23% Off!
- C++ Coding Standards: 101 Rules, Guidelines, and Best Practices; Herb Sutter, et al, $34.95, 22% Off!
Books on similar topics, in best-seller order:Books from the same publisher, in best-seller order:
C++ Primer, Fourth Edition has been completely revised and rewritten to conform
to today's C++ usage. Students new to C++ will find a clear and practically organized
introduction to the language enhanced by numerous pedagogical aids. The fourth
edition represents a complete restructuring and revision of the Primer. The authors
motivation in this edition is to teach effective styles of modern C++ programming.
To this end, they introduce the Standard Library and generic programming much
earlier in the text. Many examples have been reformulated to take advantage of
library facilities. Their focus is to show how to use the standard library abstractions
rather than the low-level facilities built into the language. They've also streamlined
and reordered the presentation of language topics.
Each chapter also contains a Summary section outlining the important topics
covered in that chapter. Extensive forward and backward cross-references are
provided to make it easier to follow the interrelationships among various features
and concepts. Occasional sidebar discussions are also provided to highlight
important concepts, general advice or cautions. The authors have also incorporated
a series of short sections that denote particularly important points, warn about
common pitfalls, suggest good programming practices, and provide general usage
tips.
Preface
C++ is about efficient programming with abstractions.
Over the years, the focus of the language--and of users of the language--has
gravitated from emphasizing efficiency to placing more emphasis on using abstractions.
This revision of the C++ Primer reflects this change in approach: The book has
been completely reorganized and rewritten to highlight modern styles of C++
programming, emphasizing use of the library and generic programming while deemphasizing
low-level programming.
Earlier editions of C++ Primer were motivated by changes in the language itself.
The Second Edition covered multiple inheritance and templates, features added
after the initial releases of C++ but before standardization. The third edition
covered the myriad of features refined and/or added as part of international
standardization process.
This edition has a very different motivation: applying what's been learned
about how to effectively teach C++, concentrating on modern programming styles
that most effectively use the abstraction powers of the language. The language
has matured greatly since the days of the first edition of this book. In particular,
with the advent of the standard library, it is possible to use C++, and to learn
C++ in ways that emphasize abstraction much more than previously had been feasible.
Just as the authorship of the Third Edition was expanded to improve coverage
of the ISO Standardization, we've expanded authorship of this edition as well.
This time, we've added Barbara Moo, who brings an understanding of how to teach
modern C++ based on years of teaching and writing about C++. In particular,
Barbara co-authored (along with Andy Koenig) Accelerated C++, which pioneered
the approach of presenting the language by starting with the library. We have
adopted many of these ideas in restructuring the Primer.
In addition to restructuring the text, this revision incorporates several new
elements intended to enhance the reader's learning. Each chapter concludes with
a Chapter Summary and glossary of Defined Terms that recap the chapter's most
important points. Readers should use these sections as a personal checklist:
If an item is unclear go back and restudy the related material until the meaning
of the item is obvious. We've also incorporated hints, warnings and observations
about important topics throughout the text. We hope that these will help readers
more quickly digest important concepts and avoid common pitfalls.
What hasn't changed from earlier versions is that the book remains a comprehensive,
tutorial introduction to C++. Our intent is to provide a clear, complete and
correct guide to the language. Knowledge of C is not assumed, although familiarity
with some modern, block structured language is. This book is intended as a first
book on C++; it is not intended as a first book on programming.
Structure of This Book
C++ Primer provides a comprehensive introduction to the International Standard
on C++, covering both the language proper and the extensive Library that is
part of that Standard. Much of the power of C++ comes from its support for abstractions.
Learning to program in C++ effectively requires more than learning a new set
of syntax and semantics. Our focus will be on how to use the features of C++
to write programs that are safe, that can be built quickly and yet offer performance
comparable to the sorts of low-level programs often written in C.
C++ is a large language and can be daunting to new users. Modern C++ can be
thought of as comprising three parts:
* The low-level language, largely inherited from C
* More advanced language features that allow us to build our own abstractions
and to organize large-scale programs and systems
* The standard library that uses these advanced features to provide a set of
useful data structures and algorithms.
Most texts tend to present the topics in this order: concentrating first on
the low-level details, then introducing the more advanced language features
and only after having covered the entire language is the standard library explained.
The result, all too often, is that readers get bogged down in issues of low-level
programming and never really master programming with abstractions let alone
how to build their own abstractions.
In this edition we take a completely different tack. We start by covering enough
of the library and basic language constructs to allow readers to write significant
programs. Our intent is to ignore some of the complexities that may hide in
the details of the language so that we can concentrate on how to use the abstractions
in the library or those that we could write ourselves. Once we've presented
enough mechanics to write real programs we'll move on to concentrate on use
of the library. Only after a thorough grounding in using the library will we
move onto those features of C++ that enable us to write our own abstractions.
Finally, we'll cover various advanced features, which are of most use in structuring
large, complex systems.
Chapter 1 introduces the essential elements of a C++ program and the details
of how to get a C++ program compiled and executed. Having read and worked through
the exercises in this chapter the reader should have enough familiarity with
the basics of the language to write real, albeit simple programs. Along the
way, we'll cover the fundamentals of compiling and executing a program so that
you can make the examples work.
Parts I and II cover the basic language and library facilities. The focus of
these parts is to learn how to write C++ programs and how to use the abstractions
from the library. All readers will need to know essentially everything covered
in this portion of the book.
Part I covers The Basics: Data types, expressions, statements and functions.
Chapter 2 covers the so-called "built-in" types defined by the language.
The following chapter, Chapter 3, introduces the library string, and vector
types. In Chapter 4 we look at arrays and pointers. These low-level facilities
are built-into the language and are analogous to library vectors and strings.
Prior to the advent of the C++ standard and the availability of the library,
C++ programs often relied heavily on arrays and pointers. Modern C++ programs
in many cases can avoid pointers and arrays using vectors and strings in their
place. The types described in these first three chapters form the basic building
blocks of all our programs.
Chapter 5 provides a detailed discussion of the expressions supported by the
language, such as the arithmetic, relational, and assignment expressions. Statements
are the topic of Chapter 6. Chapter 7 shows how functions are defined in C++.
Functions encapsulate a set of operations that generally form a single task.
Part I closes with a review of the fundamentals of the IO library.
Part II covers The Containers and Algorithms Library. Chapter 9 presents the
sequential container types and Chapter 10 the associative containers. The generic
algorithms are the topic of Chapter 11.
In addition to teaching the basics of C++, the material in Parts I and II serves
another important purpose. The library classes, string, vector and the IO classes,
are examples of abstract data types. These types are defined not by the language
itself. Instead, they are defined using the same facilities that are available
to any C++ programmer. These classes, useful in their own right, are also useful
in demonstrating how higher-level, more abstract programs can be written and
what they look like. Our experience in teaching C++ is that by first using abstract
types, readers find it easier to understand how to build their own types. Parts
III through V focus on how we can write our own types.
Part III covers the parts of C++ that let us build our own abstractions. The
material covered in Part III is the heart of C++: its support for classes. The
class mechanism provides the basis writing our own abstractions. It is also
the foundation for object oriented and generic programming.
Chapter 12 presents the basic mechanisms for writing our own types. In C++,
a class author controls all aspects of behavior. In particular, the class author
specifies what happens when objects of the class type are created, copied, assigned
and destroyed. These actions are covered in chapter 13. This part closes with
Chapter 14, which covers operator overloading and conversion operators. These
operations are important in building types that behave as naturally as the built-in
types.
Parts IV and V cover more advanced parts of the language. Part IV covers the
C++ features that support two different styles of high-level programming based
on abstractions. First is Chapter 15, which presents C++'s support for object-oriented
programming. This chapter covers inherited classes and dynamic binding. These
facilities let us write programs that can use hierarchies of types without need
to care about precise differences among the types in the inheritance.
Chapter 16 covers C++ support for building our own generic functions and classes.
Class templates and function templates are the foundation of the standard library.
This chapter covers the language features that allow us to write our own type-independent
functions and container classes.
The Primer closes with Part V, which covers advanced features. Chapter 17 covers
features that are of interest primarily to builders of large systems. Chapter
18 introduces features that are useful for specialized problems.
C++ is intended for use in building large (10s of million lines and more) commercial
systems. It incorporates features to support the high-performance, high reliability
needs of such systems as well as facilities to enable large groups of programmers
to work together on such large bodies of code. Chapter 17 covers exception handling,
namespaces and multiple inheritance. These features, while useful on systems
of any size, are particularly important on large systems.
Chapter 18 introduces several features that have application to specific rather
than general problems. Among the topics covered in this chapter are tools and
technique for controlling memory allocation, run-time type identification (RTTI),
nested classes, and pointers to class members.
Finally, whenever one writes a book, what one chooses to leave out is often
as important as what one covers. Certain aspects of the language--such as a
detailed discussion of how constructors work, under what conditions internal
temporary objects are created by the compiler, or detailed concerns about efficiency--do
not fit well into a tutorial introduction to the language, although they are
of general importance to programming real-world applications. Certain portions
of the C++ standard library have been intentionally left out, such as the support
for locales and the numerical library. The C++ standard library is very extensive,
and presenting all its aspects is beyond the scope of this primer.
Changes to the Fourth Edition
The fourth edition represents a complete restructuring and revision of the
Primer. Our motivation in this edition is to teach effective styles of modern
C++ programming. To this end, we introduce the standard library and generic
programming much earlier in the text. The examples have been reformulated to
take advantage of library facilities. Our focus is to show how to use the standard
library abstractions rather than the low-level facilities built into the language.
We've also streamlined and reordered the presentation of language topics.
We've incorporated a number of aids to enhance learning. When first introduced,
important terms are highlighted in the text in bold. Important terms that we
assume are already familiar to the reader are highlighted as bold italics. If
a term in bold italics is not already familiar, we suggest that readers consider
seeking additional help in understanding that concept. Terms first introduced
in a chapter are summarized at the end of each Chapter.
Each Chapter also contains a Summary section outlining the important topics
covered in that chapter. Extensive forward and backward cross-references are
provided to make it easier to follow the interrelationships among various features
and concepts. Occasional sidebar discussions are also provided to highlight
important concepts, general advice or cautions. We've also incorporated a series
of short sections that denote particularly important points, warn about common
pitfalls, suggest good programming practices, and provide general usage tips.
Table of Contents
Preface.
1. Getting Started.
Writing a Simple C++ Program.
A First Look at Input/Output.
A Word About Comments.
Control Structures.
Introducing Classes.
The C++ Program.
Summary.
Defined Terms.
I. THE BASICS.
2. Variables and Basic Types.
Primitive Built-in Types.
Literal Constants.
Variables.
const Qualifier.
References.
Typedef Names.
Enumerations.
Class Types.
Writing Our Own Header Files.
Summary.
Defined Terms
3. Library Types.
Namespace using Declarations.
Library string Type.
Library vector Type.
Introducing Iterators.
Library bitset Type.
Summary.
Defined Terms.
4. Arrays and Pointers.
Arrays.
Introducing Pointers.
C-Style Character Strings.
Multidimensional Arrays.
Summary.
Defined Terms
5. Expressions.
Arithmetic Operators.
Relational and Logical Operators.
The Bitwise Operators.
Assignment Operators.
Increment and Decrement Operators.
The Arrow Operator.
The Conditional Operator.
The size of Operator.
Comma Operator.
Evaluating Compound Expressions.
The new and delete Expressions.
Type Conversions.
Summary.
Defined Terms
6. Statements.
Simple Statements.
Declaration Statements.
Compound Statements (aka Blocks).
Statement Scope.
The if Statement.
The switch Statement.
The while Statement.
The for Loop Statement.
The do while Statement.
The break Statement.
The continue Statement.
The goto Statement.
try Blocks and Exception Handling.
Summary.
Defined Terms.
7. Functions.
Defining a Function.
Argument Passing.
Returninga Value.
Function Declarations.
Local Objects.
Inline Functions.
Class Member Functions.
Overloaded Functions.
Pointers to Functions.
Summary.
Defined Terms.
8. The IO Library.
An Object-Oriented Library.
No Copyor Assign for IO Objects.
Condition States.
File Input and Output.
StringStreams.
Summary.
Defined Terms.
II. CONTAINERS AND ALGORITHMS.
9. Sequential Containers.
Defining a Sequential Container.
Iterators and Iterator Ranges.
Sequence Container Operations.
How a vector Grows.
Deciding Which Container to Use.
strings Revisited.
Container Adaptors.
Summary.
Defined Terms.
10. Associative Containers.
Preliminaries: the pair Type.
Associative Containers.
The map Type.
The set Type.
The multimap and multiset Types.
Using Containers: Text Query Program.
Summary.
Defined Terms.
11. Generic Algorithms.
Overview.
A First Look at the Algorithms.
Revisiting Iterators.
Structure of Generic Algorithms.
Container Specific Algorithms.
Summary.
Defined Terms.
III. CLASSES AND DATA ABSTRACTION.
12. Classes.
Class Definitions and Declarations.
The Implicit this Pointer.
ClassScope.
Constructors.
Friends.
static Class Members.
Summary.
Defined Terms.
13. Copy Control.
The Copy Constructor.
The Assignment Operator.
The Destructor.
A Message Handling Example.
Managing Pointer Members.
Summary.
Defined Terms.
14. Overloaded Operations and Conversions.
Definingan Overloaded Operator.
Input and Output Operators.
Arithmetic and Relational Operators.
Operator =.
Operator[ ].
Operator * and ->.
Increment and Decrement Operators.
Operator () and Function Objects.
Conversions and Class Types.
Summary.
Defined Terms.
IV. OBJECT-ORIENTED AND GENERIC PROGRAMMING.
15. Object-Oriented Programming.
OOP: An Overview.
Defining Base and Derived Classes.
Conversions and Inheritance.
Constructors and Copy Control.
Class Scope under Inheritance.
Pure virtual Functions.
Containers and Inheritance.
Handle Classes and Inheritance.
Text Queries Revisited.
Summary.
Defined Terms.
16. Templates and Generic Programming.
Template Definitions.
Instantiation.
Template Compilation Models.
Class Template Members.
A Generic Handle Class.
Template Specializations.
Overloading and Function Templates.
Summary.
Defined Terms.
V. ADVANCED TOPICS.
17. Tools for Large Programs.
Exception Handling.
Namespaces.
Multiple and Virtual Inheritance.
Summary.
Defined Terms
18. Specialized Tools and Techniques.
Optimizing Memory Allocation.
Run-Time Type Identification.
Pointer to Class Member.
Nested Classes.
Union: A Space-Saving Class.
Local Classes.
Inherently NonPortable Features.
Summary.
Defined Terms.
A. The Library.
Library Names and Headers.
Brief Tour of the Algorithms.
The IO Library Revisited.
Index.
Customer Reviews
Customer Reviews: 2 Average Customer Rating:      Jun 25, 2005     wang2008@gmail.com Just too many typos!!! Just too many typos!!!
May 24, 2005     G. Wade Johnson from Houston, TX A great first book on C++ I am often asked to recommend an introductory book on C++. Until recently, I did not have an answer. Now I do.
The fourth edition of the C++ Primer uses the teaching style I first saw in Accelerated C++. It teaches C++ without assuming knowledge of C. The authors also focus on the use of objects long before they show how to create them. Based on some of the mistakes I've seen junior OO programmers make, I think this is an excellent idea. I think a programmer should have a thorough understanding of the use of objects before they begin building their own.
The book covers most of the C++ language and does a good job of explaining how to make good use of the language. Amazingly, there is even information here for the experienced C++ programmer. Unfortunately, the book does have a significant number of typos and formatting issues that might cause problems for a novice.
I plan to recommend this book the next time someone asks for an introductory C++ book.
|