Home > Store

C++ Primer Plus, 5th Edition

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

C++ Primer Plus, 5th Edition


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


  • Copyright 2005
  • Edition: 5th
  • Book
  • ISBN-10: 0-672-32697-3
  • ISBN-13: 978-0-672-32697-4

If you are new to C++ programming, C++ Primer Plus, Fifth Edition is a friendly and easy-to-use self-study guide. You will cover the latest and most useful language enhancements, the Standard Template Library and ways to streamline object-oriented programming with C++. This guide also illustrates how to handle input and output, make programs perform repetitive tasks, manipulate data, hide information, use functions and build flexible, easily modifiable programs. With the help of this book, you will:

  • Learn C++ programming from the ground up.
  • Learn through real-world, hands-on examples.
  • Experiment with concepts, including classes, inheritance, templates and exceptions.
  • Reinforce knowledge gained through end-of-chapter review questions and practice programming exercises.

C++ Primer Plus, Fifth Edition makes learning and using important object-oriented programming concepts understandable. Choose this classic to learn the fundamentals and more of C++ programming.




Solutions to a selected subset of Programming Exercises (zipped Word and text document): 76 KB - cpp5sol.zip

All the code developed for the book in one convenient download - 144 KB -- cpp5ex.zip

Sample Content

Online Sample Chapter

Dealing with Data

Table of Contents


1. Getting Started.

    Learning C++: What Lies Before You.

    The Origins of C++: A Little History.

      The C Language.

      C Programming Philosophy.

      The C++ Shift: Object-Oriented Programming.

      C++ and Generic Programming.

      The Genesis of C++.

    Portability and Standards.

    The Mechanics of Creating a Program.

      Creating the Source Code File.

      Compilation and Linking.


2. Setting Out to C++.

    C++ Initiation.

      The main() Function.

      C++ Comments.

      The C++ Preprocessor and the iostream File.

      Header Filenames.


      C++ Output with cout.

      C++ Source Code Formatting.

    C++ Statements.

      Declaration Statements and Variables.

      Assignment Statements.

      A New Trick for cout.

    More C++ Statements.

       Using cin.

      Concatenating with cout.

      cin and cout: A Touch of Class.


      Using a Function That Has a Return Value.

      Function Variations.

      User-Defined Functions.

      Using a User-Defined Function That Has a Return Value.

      Placing the using Directive in Multifunction Programs.


    Review Questions.

    Programming Exercises.

3. Dealing with Data.

    Simple Variables.

      Names for Variables.

      Integer Types.

      The short, int, and long Integer Types.

      Unsigned Types.

      Choosing an Integer Type.

      Integer Constants.

      How C++ Decides What Type a Constant Is.

      The char Type: Characters and Small Integers.

      The bool Type.

    The const Qualifier.

    Floating-Point Numbers.

      Writing Floating-Point Numbers.

      Floating-Point Types.

      Floating-Point Constants.

      Advantages and Disadvantages of Floating-Point Numbers.

    C++ Arithmetic Operators.

      Order of Operation: Operator Precedence and Associativity.

      Division Diversions.

      The Modulus Operator.

      Type Conversions.


    Review Questions.

    Programming Exercises.

4. Compound Types.

    Introducing Arrays.

      Program Notes.

      Initialization Rules for Arrays.


      Concatenating String Constants.

      Using Strings in an Array.

      Adventures in String Input.

      Reading String Input a Line at a Time.

      Mixing String and Numeric Input.

    Introducing the string Class.

      Assignment, Concatenation, and Appending.

      More string Class Operations.

      More on string Class I/O.

    Introducing Structures.

      Using a Structure in a Program.

      Can a Structure Use a string Class Member?

      Other Structure Properties.

      Arrays of Structures.

      Bit Fields in Structures.



      Setting Enumerator Values.

      Value Ranges for Enumerations.

    Pointers and the Free Store.

      Declaring and Initializing Pointers.

      Pointer Danger.

      Pointers and Numbers.

      Allocating Memory with new.

      Freeing Memory with delete.

      Using new to Create Dynamic Arrays.

    Pointers, Arrays, and Pointer Arithmetic.

      Program Notes.

      Pointers and Strings.

      Using new to Create Dynamic Structures.

      Automatic Storage, Static Storage, and Dynamic Storage.


    Review Questions.

    Programming Exercises.

5. Loops and Relational Expressions.

    Introducing for Loops.

      for Loop Parts.

      Back to the for Loop.

      Changing the Step Size.

      Inside Strings with the for Loop.

      The Increment (++) and Decrement (--) Operators.

      Side Effects and Sequence Points.

      Prefixing Versus Postfixing.

      The Increment/Decrement Operators and Pointers.

      Combination Assignment Operators.

      Compound Statements, or Blocks.

      The Comma Operator (or More Syntax Tricks).

    Relational Expressions.

      A Mistake You'll Probably Make.

      Comparing C-Style Strings.

      Comparing string Class Strings.

    The while Loop.

      Program Notes.

      for Versus while.

      Just a Moment-Building a Time-Delay Loop.

    The do while Loop.

    Loops and Text Input.

      Using Unadorned cin for Input.

      cin.get(char) to the Rescue.

      Which cin.get()?

      The End-of-File Condition.

      Yet Another Version of cin.get().

    Nested Loops and Two-Dimensional Arrays.

      Initializing a Two-Dimensional Array.


    Review Questions.

     Programming Exercises.

6. Branching Statements and Logical Operators.

    The if Statement.

      The if else Statement.

      Formatting if else Statements.

      The if else if else Construction.

    Logical Expressions.

      The Logical OR Operator: ||

      The Logical AND Operator: &&

      The Logical NOT Operator: !

      Logical Operator Facts.

      Alternative Representations.

    The cctype Library of Character Functions.

    The ?: Operator.

    The switch Statement.

      Using Enumerators as Labels.

      switch and if else.

    The break and continue Statements.

      Program Notes.

    Number-Reading Loops.

      Program Notes.

    Simple File Input/Output.

      Text I/O and Text Files.

      Writing to a Text File.

       Reading from a Text File.


    Review Questions.

    Programming Exercises.

7. Functions: C++'s Programming Modules.

    Function Review.

      Defining a Function.

      Prototyping and Calling a Function.

    Function Arguments and Passing by Value.

      Multiple Arguments.

      Another Two-Argument Function.

    Functions and Arrays.

      How Pointers Enable Array-Processing Functions.

      The Implications of Using Arrays as Arguments.

      More Array Function Examples.

      Functions Using Array Ranges.

      Pointers and const.

    Functions and Two-Dimensional Arrays.

    Functions and C-Style Strings.

      Functions with C-Style String Arguments.

      Functions That Return C-Style Strings.

    Functions and Structures.

      Passing and Returning Structures.

      Another Example of Using Functions with Structures.

      Passing Structure Addresses.

    Functions and string Class Objects.


      Recursion with a Single Recursive Call.

      Recursion with Multiple Recursive Calls.

    Pointers to Functions.

      Function Pointer Basics.

      A Function Pointer Example.


    Review Questions.

    Programming Exercises.

8. Adventures in Functions.

    C++ Inline Functions.

    Reference Variables.

       Creating a Reference Variable.

      References as Function Parameters.

      Reference Properties and Oddities.

      Using References with a Structure.

      Using References with a Class Object.

      Another Object Lesson: Objects, Inheritance, and References.

      When to Use Reference Arguments.

    Default Arguments.

      Program Notes.

    Function Overloading.

      An Overloading Example.

      When to Use Function Overloading.

    Function Templates.

      Overloaded Templates.

      Explicit Specializations.

      Instantiations and Specializations.

      Which Function Version Does the Compiler Pick?


    Review Questions.

    Programming Exercises.

9. Memory Models and Namespaces.

    Separate Compilation.

    Storage Duration, Scope, and Linkage.

      Scope and Linkage.

      Automatic Storage Duration.

      Static Duration Variables.

      Specifiers and Qualifiers.

      Functions and Linkage.

      Language Linking.

      Storage Schemes and Dynamic Allocation.

    The Placement new Operator.

      Program Notes.


      Traditional C++ Namespaces.

      New Namespace Features.

      A Namespace Example.

      Namespaces and the Future.


    Review Questions.

    Programming Exercises.

10. Objects and Classes.

    Procedural and Object-Oriented Programming.

    Abstraction and Classes.

      What Is a Type?

      Classes in C++.

      Implementing Class Member Functions.

      Using Classes.

      Reviewing Our Story to Date.

    Class Constructors and Destructors.

      Declaring and Defining Constructors.

      Using Constructors.

      Default Constructors.


      Improving the Stock Class.

      Constructors and Destructors in Review.

    Knowing Your Objects: The this Pointer.

    An Array of Objects.

    The Interface and Implementation Revisited.

    Class Scope.

      Class Scope Constants.

    Abstract Data Types.


    Review Questions.

    Programming Exercises.

11. Working with Classes.

    Operator Overloading.

    Time on Our Hands: Developing an Operator Overloading Example.

      Adding an Addition Operator.

      Overloading Restrictions.

      More Overloaded Operators.

    Introducing Friends.

      Creating Friends.

      A Common Kind of Friend: Overloading the << Operator.

    Overloaded Operators: Member Versus Nonmember Functions.

    More Overloading: A Vector Class.

      Using a State Member.

      Overloading Arithmetic Operators for the Vector Class.

      An Implementation Comment.

      Taking the Vector Class on a Random Walk.

    Automatic Conversions and Type Casts for Classes.

      Program Notes.

      Conversion Functions.

      Conversions and Friends.


    Review Questions.

    Programming Exercises.

12. Classes and Dynamic Memory Allocation.

    Dynamic Memory and Classes.

      A Review Example and Static Class Members.

      Implicit Member Functions.

      The New, Improved String Class.

      Things to Remember When Using new in Constructors.

      Observations About Returning Objects.

      Using Pointers to Objects.

      Reviewing Techniques.

    A Queue Simulation.

      A Queue Class.

      The Customer Class.

      The Simulation.


    Review Questions.

    Programming Exercises.

13. Class Inheritance.

    Beginning with a Simple Base Class.

      Deriving a Class.

      Constructors: Access Considerations.

      Using a Derived Class.

      Special Relationships Between Derived and Base Classes.

    Inheritance: An Is-a Relationship.

    Polymorphic Public Inheritance.

      Developing the Brass and BrassPlus Classes.

      Static and Dynamic Binding.

      Pointer and Reference Type Compatibility.

      Virtual Member Functions and Dynamic Binding.

      Things to Know About Virtual Methods.

    Access Control: protected.

    Abstract Base Classes.

      Applying the ABC Concept.

      ABC Philosophy.

    Inheritance and Dynamic Memory Allocation.

      Case 1: Derived Class Doesn't Use new.

      Case 2: Derived Class Does Use new.

      An Inheritance Example with Dynamic Memory Allocation and Friends.

    Class Design Review.

      Member Functions That the Compiler Generates for You.

      Other Class Method Considerations.

      Public Inheritance Considerations.

      Class Function Summary.


    Review Questions.

    Programming Exercises.

14. Reusing Code in C++.

    Classes with Object Members.

      The valarray Class: A Quick Look.

      The Student Class Design.

      The Student Class Example.

    Private Inheritance.

      The Student Class Example (New Version).

    Multiple Inheritance.

      How Many Workers?

      Which Method?

      MI Synopsis.

    Class Templates.

      Defining a Class Template.

      Using a Template Class.

      A Closer Look at the Template Class.

      An Array Template Example and Non-Type Arguments.

      Template Versatility.

      Template Specializations.

      Member Templates.

      Templates as Parameters.

      Template Classes and Friends.


    Review Questions.

    Programming Exercises.

15. Friends, Exceptions, and More.


      Friend Classes.

      Friend Member Functions.

      Other Friendly Relationships.

    Nested Classes.

      Nested Classes and Access.

      Nesting in a Template.


      Calling abort().

      Returning an Error Code.

      The Exception Mechanism.

      Using Objects as Exceptions.

      Unwinding the Stack.

      More Exception Features.

      The exception Class.

      Exceptions, Classes, and Inheritance.

      When Exceptions Go Astray.

      Exception Cautions.


      What Is RTTI For?

      How Does RTTI Work?

    Type Cast Operators.


    Review Questions.

    Programming Exercises.

16. The string Class and the Standard Template Library.

    The string Class.

      Constructing a String.

      string Class Input.

      Working with Strings.

      What Else Does the string Class Offer?

    The auto_ptr Class.

      Using auto_ptr.

      auto_ptr Considerations.

    The STL.

      The vector Template Class.

      Things to Do to Vectors.

      More Things to Do to Vectors.

    Generic Programming.

      Why Iterators?

      Kinds of Iterators.

      Iterator Hierarchy.

      Concepts, Refinements, and Models.

      Kinds of Containers.

      Associative Containers.

    Function Objects (aka Functors).

      Functor Concepts.

      Predefined Functors.

      Adaptable Functors and Function Adapters.


       Algorithm Groups.

      General Properties of Algorithms.

      The STL and the string Class.

      Functions Versus Container Methods.

      Using the STL.

    Other Libraries.

      vector and valarray.


    Review Questions.

    Programming Exercises.

17. Input, Output, and Files.

    An Overview of C++ Input and Output.

      Streams and Buffers.

      Streams, Buffers, and the iostream File.


    Output with cout.

      The Overloaded << Operator.

      The Other ostream Methods.

      Flushing the Output Buffer.

      Formatting with cout.

    Input with cin.

      How cin >> Views Input.

      Stream States.

      Other istream Class Methods.

      Other istream Methods.

    File Input and Output.

      Simple File I/O.

      Stream Checking and is_open().

      Opening Multiple Files.

      Command-Line Processing.

      File Modes.

      Random Access.

    Incore Formatting.

    What Now?


    Review Questions.

    Programming Exercises.

Appendix A. Number Bases.

    Decimal Numbers (Base 10).

    Octal Integers (Base 8).

    Hexadecimal Numbers (Base 16).

    Binary Numbers (Base 2).

    Binary and Hex.

Appendix B. C++ Reserved Words.

    C++ Keywords.

    Alternative Tokens.

    C++ Library Reserved Names.

Appendix C. The ASCII Character Set.

Appendix D. Operator Precedence.

Appendix E. Other Operators.

    Bitwise Operators.

      The Shift Operators.

      The Logical Bitwise Operators.

      Alternative Representations of Bitwise Operators.

      A Few Common Bitwise Operator Techniques.

    Member Dereferencing Operators.

Appendix F. The string Template Class.

    Thirteen Types and a Constant.

    Data Information, Constructors, and Odds and Ends.

      Default Constructors.

      Constructors That Use Arrays.

      Constructors That Use Part of an Array.

      Copy Constructors.

      Constructors That Use n Copies of a Character.

      Constructors That Use a Range.

      Memory Miscellany.

    String Access.

    Basic Assignment.

    String Searching.

      The find() Family.

      The rfind() Family.

      The find_first_of() Family.

      The find_last_of() Family.

      The find_first_not_of() Family.

      The find_last_not_of() Family.

    Comparison Methods and Functions.

     String Modifiers.

      Methods for Appending and Adding.

      More Assignment Methods.

      Insertion Methods.

      Erase Methods.

      Replacement Methods.

      Other Modifying Methods: copy() and swap().

    Output and Input.

Appendix G. The STL Methods and Functions.

    Members Common to All Containers.

    Additional Members for Vectors, Lists, and Deques.

    Additional Members for Sets and Maps.

    STL Functions.

      Nonmodifying Sequence Operations.

      Mutating Sequence Operations.

       Sorting and Related Operations.

      Numeric Operations.

Appendix H. Selected Readings and Internet Resources.

    Selected Readings.

    Internet Resources.

Appendix I. Converting to ANSI/ISO Standard C++.

    Use Alternatives for Some Preprocessor Directives.

      Use const Instead of #define to Define Constants.

      Use inline Instead of #define to Define Short Functions.

    Use Function Prototypes.

    Use Type Casts.

    Become Familiar with C++ Features.

     Use the New Header Organization.

    Use Namespaces.

    Use the autoptr Template.

    Use the string Class.

    Use the STL.

Appendix J. Answers to the Review Questions.

    Answers to Review Questions for Chapter 2.

    Answers to Review Questions for Chapter 3.

    Answers to Review Questions for Chapter 4.

    Answers to Review Questions for Chapter 5.

    Answers to Review Questions for Chapter 6.

    Answers to Review Questions for Chapter 7.

    Answers to Review Questions for Chapter 8.

    Answers to Review Questions for Chapter 9.

    Answers to Review Questions for Chapter 10.

    Answers to Review Questions for Chapter 11.

    Answers to Review Questions for Chapter 12.

    Answers to Review Questions for Chapter 13.

    Answers to Review Questions for Chapter 14.

    Answers to Review Questions for Chapter 15.

    Answers to Review Questions for Chapter 16.

    Answers to Review Questions for Chapter 17.



Submit Errata

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.


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.


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.


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.


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


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


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.


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.


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