LOCATION OF VIDEONOTES IN THE TEXT Chapter 10 Class Scope,Public and Private Members,p.565 Default Initialization of Member Variables,p.587 Separate Interface and Implementation,p.592 Solution to Practice Program 10.1,p.611 Chapter 11 const Confusion,p.639 Arrays of Classes using Dynamic Arrays,p.671 Overloading and =for a Class,p.680 Solution to Programming Project 11.12,p.701 Chapter 12 Avoiding Multiple Definitions,p.715 Solution to Practice Program 12.3,p.736 Chapter 13 Walkthrough of Linked Lists of Classes,p.762 Solution to Programming Project 13.6,p.783 Solution to Programming Project 13.9,p.785 Chapter 14 Recursion and the Stack,p.801 Solution to Practice Program 14.4,p.827 Solution to Practice Program 14.4,p.828 Chapter 15 Inheritance Example,p.858 Solution to Practice Program 15.3,p.882 Solution to Programming Project 15.1,p.884 Solution to Programming Project 15.10,p.889 Chapter 16 The STL Exception Class,p.917 Solution to Practice Program 16.1,p.920 Solution to Programming Project 16.3,p.922 Chapter 17 Issues Compiling Programs with Templates,p.931 Solution to Programming Project 17.7,p.955 Chapter 18 C++11 and Containers,p.990 Solution to Practice Program 18.2,p.1007 Solution to Programming Project 18.6,p.1010 (Continued from Inside Front Cover)
Location of VideoNotes in the Text Chapter 10 Class Scope, Public and Private Members, p. 565 Default Initialization of Member Variables, p. 587 Separate Interface and Implementation, p. 592 Solution to Practice Program 10.1, p. 611 Chapter 11 const Confusion, p. 639 Arrays of Classes using Dynamic Arrays, p. 671 Overloading = and == for a Class, p. 680 Solution to Programming Project 11.12, p. 701 Chapter 12 Avoiding Multiple Definitions, p. 715 Solution to Practice Program 12.3, p. 736 Chapter 13 Walkthrough of Linked Lists of Classes, p. 762 Solution to Programming Project 13.6, p. 783 Solution to Programming Project 13.9, p. 785 Chapter 14 Recursion and the Stack, p. 801 Solution to Practice Program 14.4, p. 827 Solution to Practice Program 14.4 , p. 828 Chapter 15 Inheritance Example, p. 858 Solution to Practice Program 15.3, p. 882 Solution to Programming Project 15.1, p. 884 Solution to Programming Project 15.10, p. 889 Chapter 16 The STL Exception Class, p. 917 Solution to Practice Program 16.1, p. 920 Solution to Programming Project 16.3, p. 922 Chapter 17 Issues Compiling Programs with Templates, p. 931 Solution to Programming Project 17.7, p. 955 Chapter 18 C++11 and Containers, p. 990 Solution to Practice Program 18.2, p. 1007 Solution to Programming Project 18.6, p. 1010 (Continued from Inside Front Cover)
Ninth Edition PROBLEM SOLVING with ○++ Walter Savitch UNIVERSITY OF CALIFORNIA,SAN DIEGO CONTRIBUTOR Kenrick Mock UNIVERSITY OF ALASKA,ANCHORAGE PEARSON Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City São Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo P ro b l em So l v i ng with C++ Walter Savitch University of California, San Diego Contributor Kenrick Mock University of Alaska, Anchorage Ninth Edition
Preface This book is meant to be used in a first course in programming and computer science using the C++language.It assumes no previous programming experi- ence and no mathematics beyond high school algebra. If you have used the previous edition of this book,you should read the following section that explains the changes to this ninth edition and then you can skip the rest of this preface.If you are new to this book,the rest of this preface will give you an overview of the book. Changes to the Ninth Edition This ninth edition presents the same programming philosophy as the eighth edition.All of the material from the eighth edition remains,but with the fol- lowing enhancements: End-of-chapter programs are now split into Practice Programs and Program- ming Projects.Practice Programs require a direct application of concepts presented in the chapter and solutions are usually short.Practice Programs are appropriate for laboratory exercises.Programming Projects require ad- ditional problem solving and solutions are generally longer than Practice Programs.Programming Projects are appropriate for homework problems. Introduction to C++11 in the context of C++98.Examples of C++11 content includes new integer types,the auto type,raw string literals,strong enumera- tions,nullptr,ranged for loop,conversion between strings and integers, member initializers,and constructor delegation. Additional material on sorting,secure programming (e.g.,overflow,array out of bounds),and inheritance. Correction of errata. Twenty-one new Practice Programs and ten new Programming Projects. Ten new VideoNotes for a total of sixty-four VideoNotes.These VideoNotes walk students through the process of both problem solving and coding to help reinforce key programming concepts.An icon appears in the margin of the book when a VideoNote is available regarding the topic covered in the text. If you are an instructor already using the eighth edition,you can continue to teach your course almost without change. Flexibility in Topic Ordering This book was written to allow instructors wide latitude in reordering the material.To illustrate this flexibility,we suggest two alternative ways to order
v Preface This book is meant to be used in a first course in programming and computer science using the C++ language. It assumes no previous programming experience and no mathematics beyond high school algebra. If you have used the previous edition of this book, you should read the following section that explains the changes to this ninth edition and then you can skip the rest of this preface. If you are new to this book, the rest of this preface will give you an overview of the book. Changes to the Ninth Edition This ninth edition presents the same programming philosophy as the eighth edition. All of the material from the eighth edition remains, but with the following enhancements: ■ End-of-chapter programs are now split into Practice Programs and Programming Projects. Practice Programs require a direct application of concepts presented in the chapter and solutions are usually short. Practice Programs are appropriate for laboratory exercises. Programming Projects require additional problem solving and solutions are generally longer than Practice Programs. Programming Projects are appropriate for homework problems. ■ Introduction to C++11 in the context of C++98. Examples of C++11 content includes new integer types, the auto type, raw string literals, strong enumerations, nullptr, ranged for loop, conversion between strings and integers, member initializers, and constructor delegation. ■ Additional material on sorting, secure programming (e.g., overflow, array out of bounds), and inheritance. ■ Correction of errata. ■ Twenty-one new Practice Programs and ten new Programming Projects. ■ Ten new VideoNotes for a total of sixty-four VideoNotes. These VideoNotes walk students through the process of both problem solving and coding to help reinforce key programming concepts. An icon appears in the margin of the book when a VideoNote is available regarding the topic covered in the text. If you are an instructor already using the eighth edition, you can continue to teach your course almost without change. Flexibility in Topic Ordering This book was written to allow instructors wide latitude in reordering the material. To illustrate this flexibility, we suggest two alternative ways to order
vi PREFACE the topics.There is no loss of continuity when the book is read in either of these ways.To ensure this continuity when you rearrange material,you may need to move sections rather than entire chapters.However,only large sec- tions in convenient locations are moved.To help customize a particular order for any class's needs,the end of this preface contains a dependency chart,and each chapter has a "Prerequisites"section that explains what material needs to be covered before each section in that chapter. Reordering 1:Earlier Classes To effectively design classes,a student needs some basic tools such as control structures and function definitions.This basic material is covered in Chapters 1 through 6.After completing Chapter 6,students can begin to write their own classes.One possible reordering of chapters that allows for such early coverage of classes is the following: Basics:Chapters 1,2,3,4,5,and 6.This material covers all control struc- tures,function definitions,and basic file I/O.Chapter 3,which covers ad- ditional control structures,could be deferred if you wish to cover classes as early as possible. Classes and namespaces:Chapter 10,Sections 11.1 and 11.2 of Chapter 11, and Chapter 12.This material covers defining classes,friends,overloaded operators,and namespaces. Arrays,strings and vectors:Chapters 7 and 8 Pointers and dynamic arrays:Chapter 9 Arrays in classes:Sections 11.3 and 11.4 of Chapter 11 Inheritance:Chapter 15 Recursion:Chapter 14 (Alternately,recursion may be moved to later in the course.) Pointers and linked lists:Chapter 13 Any subset of the following chapters may also be used: Exception handling:Chapter 16 Templates:Chapter 17 Standard Template Library:Chapter 18 Reordering 2:Classes Slightly Later but Still Early This version covers all control structures and the basic material on arrays be- fore doing classes,but classes are covered later than the previous ordering and slightly earlier than the default ordering. Basics:Chapters 1,2,3,4,5,and 6.This material covers all control struc- tures,function definitions,and the basic file I/O
vi Preface the topics. There is no loss of continuity when the book is read in either of these ways. To ensure this continuity when you rearrange material, you may need to move sections rather than entire chapters. However, only large sections in convenient locations are moved. To help customize a particular order for any class’s needs, the end of this preface contains a dependency chart, and each chapter has a “Prerequisites” section that explains what material needs to be covered before each section in that chapter. Reordering 1: Earlier Classes To effectively design classes, a student needs some basic tools such as control structures and function definitions. This basic material is covered in Chapters 1 through 6. After completing Chapter 6, students can begin to write their own classes. One possible reordering of chapters that allows for such early coverage of classes is the following: Basics: Chapters 1, 2, 3, 4, 5, and 6. This material covers all control structures, function definitions, and basic file I/O. Chapter 3, which covers additional control structures, could be deferred if you wish to cover classes as early as possible. Classes and namespaces: Chapter 10, Sections 11.1 and 11.2 of Chapter 11, and Chapter 12. This material covers defining classes, friends, overloaded operators, and namespaces. Arrays, strings and vectors: Chapters 7 and 8 Pointers and dynamic arrays: Chapter 9 Arrays in classes: Sections 11.3 and 11.4 of Chapter 11 Inheritance: Chapter 15 Recursion: Chapter 14 (Alternately, recursion may be moved to later in the course.) Pointers and linked lists: Chapter 13 Any subset of the following chapters may also be used: Exception handling: Chapter 16 Templates: Chapter 17 Standard Template Library: Chapter 18 Reordering 2: Classes Slightly Later but Still Early This version covers all control structures and the basic material on arrays before doing classes, but classes are covered later than the previous ordering and slightly earlier than the default ordering. Basics: Chapters 1, 2, 3, 4, 5, and 6. This material covers all control structures, function definitions, and the basic file I/O
PREFACE vii Arrays and strings:Chapter 7,Sections 8.1 and 8.2 of Chapter 8 Classes and namespaces:Chapter 10,Sections 11.1 and 11.2 of Chapter 11, and Chapter 12.This material covers defining classes,friends,overloaded operators,and namespaces. Pointers and dynamic arrays:Chapter 9 Arrays in classes:Sections 11.3 and 11.4 of Chapter 11 Inheritance:Chapter 15 Recursion:Chapter 14.(Alternately,recursion may be moved to later in the course.) Vectors:Chapter 8.3 Pointers and linked lists:Chapter 13 Any subset of the following chapters may also be used: Exception handling:Chapter 16 Templates:Chapter 17 Standard Template Library:Chapter 18 Accessibility to Students It is not enough for a book to present the right topics in the right order.It is not even enough for it to be clear and correct when read by an instructor or other experienced programmer.The material needs to be presented in a way that is accessible to beginning students.In this introductory textbook,I have endeav- ored to write in a way that students find clear and friendly.Reports from the many students who have used the earlier editions of this book confirm that this style makes the material clear and often even enjoyable to students. ANSI/ISO C++Standard This edition is fully compatible with compilers that meet the latest ANSI/ISO C++standard.At the time of this writing the latest standard is C++11. Advanced Topics Many "advanced topics"are becoming part of a standard CS1 course.Even if they are not part of a course,it is good to have them available in the text as enrichment material.This book offers a number of advanced topics that can be integrated into a course or left as enrichment topics.It gives thorough cov- erage of C++templates,inheritance (including virtual functions),exception handling,and the STL(Standard Template Library).Although this book uses libraries and teaches students the importance of libraries,it does not require any nonstandard libraries.This book uses only libraries that are provided with essentially all C++implementations
Preface vii Arrays and strings: Chapter 7, Sections 8.1 and 8.2 of Chapter 8 Classes and namespaces: Chapter 10, Sections 11.1 and 11.2 of Chapter 11, and Chapter 12. This material covers defining classes, friends, overloaded operators, and namespaces. Pointers and dynamic arrays: Chapter 9 Arrays in classes: Sections 11.3 and 11.4 of Chapter 11 Inheritance: Chapter 15 Recursion: Chapter 14. (Alternately, recursion may be moved to later in the course.) Vectors: Chapter 8.3 Pointers and linked lists: Chapter 13 Any subset of the following chapters may also be used: Exception handling: Chapter 16 Templates: Chapter 17 Standard Template Library: Chapter 18 Accessibility to Students It is not enough for a book to present the right topics in the right order. It is not even enough for it to be clear and correct when read by an instructor or other experienced programmer. The material needs to be presented in a way that is accessible to beginning students. In this introductory textbook, I have endeavored to write in a way that students find clear and friendly. Reports from the many students who have used the earlier editions of this book confirm that this style makes the material clear and often even enjoyable to students. ANSI/ISO C++ Standard This edition is fully compatible with compilers that meet the latest ANSI/ISO C++ standard. At the time of this writing the latest standard is C++11. Advanced Topics Many “advanced topics” are becoming part of a standard CS1 course. Even if they are not part of a course, it is good to have them available in the text as enrichment material. This book offers a number of advanced topics that can be integrated into a course or left as enrichment topics. It gives thorough coverage of C++ templates, inheritance (including virtual functions), exception handling, and the STL (Standard Template Library). Although this book uses libraries and teaches students the importance of libraries, it does not require any nonstandard libraries. This book uses only libraries that are provided with essentially all C++ implementations