Home > Store

C++ Primer, 4th Edition

Register your product to gain access to bonus material or receive a coupon.

C++ Primer, 4th Edition

Book

  • Sorry, this book is no longer in print.
Not for Sale

About

Features

A best-seller completely revised and rewritten to conform to today's C++ usage.

° A favorite choice for a first C++ book--greatly improved and updated for modern C++ programming.

° Reorganized to better address the novice audience, making it more approachable for non-C programmers.

° Features many new examples that are less advanced and more accessible to novices as well as experienced programmers--folks who already use C++ need this book too.

Description

  • Copyright 2005
  • Edition: 4th
  • Book
  • ISBN-10: 0-201-72148-1
  • ISBN-13: 978-0-201-72148-5

"C++ Primer is well known as one of the best books for learning C++ and is useful for C++ programmers of all skill levels. This Fourth Edition not only keeps this tradition alive, it actually improves on it."
--Steve Vinoski, Chief Engineer, Product Innovation, IONA Technologies

"The Primer really brings this large and complex language down to size."
--Justin Shaw, Senior Member of Technical Staff, Electronic Programs Division, The Aerospace Corporation

"It not only gets novices up and running early, but gets them to do so using good programming practices."
--Nevin ":-)" Liber, Senior Principal Engineer (C++ developer since 1988)

This popular tutorial introduction to standard C++ has been completely updated, reorganized, and rewritten to help programmers learn the language faster and use it in a more modern, effective way.

Just as C++ has evolved since the last edition, so has the authors' approach to teaching it. They now introduce the C++ standard library from the beginning, giving readers the means to write useful programs without first having to master every language detail. Highlighting today's best practices, they show how to write programs that are safe, can be built quickly, and yet offer outstanding performance. Examples that take advantage of the library, and explain the features of C++, also show how to make the best use of the language. As in its previous editions, the book's authoritative discussion of fundamental C++ concepts and techniques makes it a valuable resource even for more experienced programmers.

Program Faster and More Effectively with This Rewritten Classic
  • Restructured for quicker learning, using the C++ standard library
  • Updated to teach the most current programming styles and program design techniques
  • Filled with new learning aids that emphasize important points, warn about common pitfalls, suggest good programming practices, and provide general usage tips
  • Complete with exercises that reinforce skills learned
  • Authoritative and comprehensive in its coverage

The source code for the book's extended examples is available on the Web at the address below.

www.awprofessional.com/cpp_primer

Downloads

Source Code

Untitled Document

Download the source code files related to this book.

Source code (packaged for Windows systems using Microsoft Visual Studio.NET). Please note, the code requires Microsoft compiler Version 7.0 or later.

Source code (packaged for Unix and similar systems, tar file compressed with gzip). Please note the code was compiled using g++ version 3.2 or later.

Sample Content

Online Sample Chapter

Getting Started: The Basic Elements of C++

Downloadable Sample Chapter

Download the Sample Chapter related to this title.

Sample Pages

Download the sample pages (includes Chapter 1 and Index)

Table of Contents

Preface     xix

Chapter 1. Getting Started      1

1.1 Writing a Simple C++ Program    2
1.2 A First Look at Input/Output     5
1.3 A Word About Comments     10
1.4 Control Structures     11
1.5 Introducing Classes    20
1.6 The C++ Program    25
Chapter Summary    28
Defined Terms    28

Part I. The Basics    31

Chapter 2. Variables and Basic Types    33

2.1 Primitive Built-in Types    34
2.2 Literal Constants     37
2.3 Variables     43
2.4 const Qualifier    56
2.5 References    58
2.6 Typedef Names    61
2.7 Enumerations    62
2.8 Class Types     63
2.9 Writing Our Own Header Files    67
Chapter Summary    73
Defined Terms     73

Chapter 3. Library Types      77

3.1 Namespace using Declarations      78
3.2 Library string Type     80
3.3 Library vector Type      90
3.4 Introducing Iterators      95
3.4.1 Iterator Arithmetic      100
3.5 Library bitset Type      101
Chapter Summary      107
Defined Terms      107

Chapter 4. Arrays and Pointers 109

4.1 Arrays      110
4.2 Introducing Pointers      114
4.3 C-Style Character Strings      130
4.4 Multidimensioned Arrays      141
Chapter Summary      145
Defined Terms      145

Chapter 5. Expressions      147

5.1 Arithmetic Operators      149
5.2 Relational and Logical Operators      152
5.3 The Bitwise Operators      154
5.4 Assignment Operators      159
5.5 Increment and Decrement Operators      162
5.6 The Arrow Operator      164
5.7 The Conditional Operator      165
5.8 The size of Operator      167
5.9 Comma Operator      168
5.10 Evaluating Compound Expressions      168
5.11 The new and delete Expressions      174
5.12 Type Conversions      178
Chapter Summary      188
Defined Terms      188

Chapter 6. Statements      191

6.1 Simple Statements      192
6.2 Declaration Statements      193
6.3 Compound Statements (Blocks)      193
6.4 Statement Scope      194
6.5 The if Statement      195
6.5.1 The if Statement else Branch      197
6.6 The switch Statement      199
6.7 The whileStatement      204
6.8 The for Loop Statement      207
6.9 The do while Statement      210
6.10 The break Statement      212
6.11 The continue Statement      214
6.12 The goto Statement      214
6.13 try Blocks and Exception Handling      215
6.13.1 A throw Expression      216
6.13.2 The try Block      217
6.13.3 Standard Exceptions      219
6.14 Using the Preprocessor for Debugging      220
Chapter Summary      223
Defined Terms      223

Chapter 7. Functions      225

7.1 Defining a Function      226
7.2 Argument Passing      229
7.3 The return Statement      245
7.4 Function Declarations      251
7.5 Local Objects      254
7.6 Inline Functions      256
7.7 ClassMemberFunctions      258
7.8 Overloaded Functions      265
7.9 Pointers to Functions      276
Chapter Summary      280
Defined Terms      280

Chapter 8. The IO Library      283

8.1 An Object-Oriented Library      284
8.2 Condition States      287
8.3 Managing the Output Buffer      290
8.4 File Input and Output      293
8.5 String Streams      299
Chapter Summary      302
Defined Terms      302

Part II. Containers and Algorithms      303

Chapter 9. Sequential Containers      305

9.1 Defining a Sequential Container      307
9.2 Iterators andIteratorRanges      311
9.3 Sequence Container Operations      316
9.4 How a vector Grows      330
9.5 Deciding Which Container to Use      333
9.6 strings Revisited      335
9.7 Container Adaptors      348
Chapter Summary      353
Defined Terms      353

Chapter 10. Associative Containers      355

10.1 Preliminaries: the pair Type      356
10.2 Associative Containers      358
10.3 The map Type      360
10.4 The set Type      372
10.5 The multimap and multiset Types      375
10.6 Using Containers: Text-Query Program      379
Chapter Summary      388
Defined Terms      388

Chapter 11. Generic Algorithms      391

11.1 Overview      392
11.2 A First Look at the Algorithms      395
11.3 Revisiting Iterators      405
11.4 Structure of Generic Algorithms      419
11.5 Container-Specific Algorithms      421
Chapter Summary      424
Defined Terms      424

Part III. Classes and Data Abstraction      427

Chapter 12. Classes      429

12.1 Class Definitions and Declarations         430
12.2 The Implicit this Pointer         440
12.3 Class Scope         444
12.4 Constructors         451
12.5 Friends         465
12.6 static Class Members         467
Chapter Summary         473
Defined Terms         473

Chapter 13 Copy Control      475

13.1 The Copy Constructor         476
13.2 The Assignment Operator         482
13.3 The Destructor           484
13.4 A Message-Handling Example       486
13.5 Managing Pointer Members         492
Chapter Summary         502
Defined Terms         502

Chapter 14. Overloaded Operations and Conversions      505

14.1 Defining an Overloaded Operator         506
14.2 Input andOutputOperators         513
14.3 Arithmetic and Relational Operators         517
14.4 Assignment Operators        520
14.5 Subscript Operator         522
14.6 Member Access Operators         523
14.7 Increment and Decrement Operators        526
14.8 Call Operator and Function Objects         530
14.9 Conversions and Class Types         535
Chapter Summary         . 552
Defined Terms         552

Part IV. Object-Oriented and Generic Programming      555

Chapter 15. Object-Oriented Programming      557

15.1 OOP: An Overview         558
15.2 Defining Base and Derived Classes         560
15.3 Conversions and Inheritance         577
15.4 Constructors and Copy Control         580
15.5 Class Scope under Inheritance         590
15.6 Pure Virtual Functions         595
15.7 Containers and Inheritance         597
15.8 Handle Classes and Inheritance         598
15.9 Text Queries Revisited         607
Chapter Summary         621
Defined Terms         621

Chapter 16. Templates and Generic Programming      623

16.1 Template Definitions         624
16.2 Instantiation           636
16.3 Template Compilation Models       643
16.4 Class Template Members         647
16.5 A Generic Handle Class         666
16.6 Template Specializations         671
16.7 Overloading and Function Templates         679
Chapter Summary        683
Defined Terms         683

Part V. Advanced Topics      685

Chapter 17. Tools for Large Programs      687

17.1 Exception Handling         688
17.2 Namespaces           712
17.3 Multiple and Virtual Inheritance       731
Chapter Summary         748
Defined Terms         748

Chapter 18. Specialized Tools and Techniques      753

18.1 Optimizing Memory Allocation         754
18.2 Run-Time Type Identification         772
18.3 Pointer to Class Member         780
18.4 Nested Classes         786
18.5 Union: A Space-Saving Class         792
18.6 Local Classes         796
18.7 Inherently Nonportable Features         797
Chapter Summary         805
Defined Terms         805

Appendix A. The Library      809

A.1 Library Names and Headers         810
A.2 A Brief Tour of the Algorithms         811
A.3 The IO Library Revisited         825

Index      843

Preface

Untitled Document

C++ Primer, Fourth Edition, provides a comprehensive introduction to the C++ language. As a primer, it provides a clear tutorial approach to the language, enhanced by numerous examples and other learning aids. Unlike most primers, it also provides a detailed description of the language, with particular emphasis on current and effective programming techniques.

Countless programmers have used previous editions of C++ Primer to learn C++. In that time C++ has matured greatly. Over the years, the focus of the language--and of C++ programmers--has grown beyond a concentration on runtime efficiency to focus on ways of making programmers more efficient. With the widespread availability of the standard library, it is possible to use and learn C++ more effectively than in the past. This revision of the C++ Primer reflects these new possiblities.

Changes to the Fourth Edition

In this edition, we have completely reorganized and rewritten the C++ Primer to highlight modern styles of C++ programming. This edition gives center stage to using the standard library while deemphasizing techniques for low-level programming. We introduce the standard library much earlier in the text and have reformulated the examples to take advantage of library facilities. We have also streamlined and reordered the presentation of language topics.

In addition to restructuring the text, we have incorporated several new elements to enhance the reader's understanding. Each chapter concludes with a Chapter Summary and glossary of Defined Terms, which recap the chapter's most important points. Readers should use these sections as a personal checklist: If you do not understand a term, restudy the corresponding part of the chapter.

We've also incorporated a number of other learning aids in the body of the text:

  • Important terms are indicated in bold; important terms that we assume are already familiar to the reader are indicated in bold italics. Each term appears in the chapter's Defined Terms section.
  • Throughout the book, we highlight parts of the text to call attention to important aspects of the language, warn about common pitfalls, suggest good programming practices, and provide general usage tips. We hope that these notes will help readers more quickly digest important concepts and avoid common pitfalls.
  • To make it easier to follow the relationships among features and concepts, we provide extensive forward and backward cross-references.
  • We have provided sidebar discussions that focus on important concepts and supply additional explanations for topics that programmers new to C++ often find most difficult.
  • Learning any programming language requires writing programs. To that end, the primer provides extensive examples throughout the text. Source code for the extended examples is available in the "More Information" box on this web site.

What hasn't changed from earlier versions is that the book remains a compre-hensive tutorial introduction to C++. Our intent is to provide a clear, complete and correct guide to the language. We teach the language by presenting a series of examples, which, in addition to explaining language features, show how to make the best use of C++. Although knowledge of C (the language on which C++ was originally based) is not assumed, we do assume the reader has programmed in a modern block-structured language.

Structure of This Book

C++ Primer provides an 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 programming with abstractions. Learning to program effectively in C++ requires more than learning new syntax and semantics. Our focus is 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 define our own data types and to organize large-scale programs and systems
  • The standard library, which uses these advanced features to provide a set of useful data structures and algorithms

Most texts present C++ in this same order: They start by covering the low-level details and then introduce the more advanced language features. They explain the standard library only after having covered the entire language. The result, all too often, is that readers get bogged down in issues of low-level programming or the complexities of writing type definitions and never really understand the power of programming in a more abstract way. Needless to say, readers also often do not learn enough to build their own abstractions.

In this edition we take a completely different tack. We start by covering the basics of the language and the library together. Doing so allows you, the reader, to write significant programs. Only after a thorough grounding in using the library-- and writing the kinds of abstract programs that the library allows--do we move on to those features of C++ that will enable you to write your own abstractions.

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. Most C++ programmers need to know essentially everything covered in this portion of the book.

In addition to teaching the basics of C++, the material in Parts I and II serves another important purpose. The library facilities are themselves abstract data types written in C++. The library can be defined using the same class-construction features that are available to any C++ programmer. Our experience in teaching C++ is that by first using well-designed 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 introduces the heart of C++: its support for classes. The class mechanism provides the basis for writing our own abstractions. Classes are also the foundation for object-oriented and generic programming, which we cover in Part IV. The Primer concludes with Part V, which covers advanced features that are of most use in structuring large, complex systems.

Index

Download the Index file related to this title.

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020