1. Trang chủ
  2. » Tất cả

Problem solving with c++ (ninth edition) walter savitch

1,1K 23 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 1.092
Dung lượng 5,95 MB

Nội dung

Ninth Edition Problem Solving with C++ This page intentionally left blank Ninth Edition Problem Solving with C++ Walter Savitch University of California, San Diego Contributor Kenrick Mock University of Alaska, Anchorage 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 Editorial Director: Marcia Horton Acquisitions Editor: Matt Goldstein Program Manager: Kayla Smith-Tarbox Editorial Assistant: Kelsey Loanes Marketing Coordinator: Kathryn Ferranti Production Director: Erin Gregg Managing Editor: Scott Disanno Senior Operations Supervisor: Vincent Scelta Operations Specialist: Linda Sager Cover Designer: Joyce Wells Permissions Manager: Timothy Nicholls Image Permissions Manager: Karen Sanatar Media Producer: Renata Butera Media Project Manager: Wanda Rockwell Full-Service Vendor: Hardik Popli, Cenveo® Publisher Services Composition: Cenveo Publisher Services Printer/Binder: Courier/Westford Cover Printer: Lehigh-Phoenix Color/Hagerstown Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on appropriate page within text Microsoft® and Windows® are registered trademarks of the Microsoft Corporation in the U.S.A and other countries Screen shots and icons reprinted with permission from the Microsoft Corporation This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation Copyright © 2015, 2012, 2009, 2007, 2005, 2003 Pearson Education, Inc All rights reserved Manufactured in the United States of America This publication is protected by Copyright, and permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise To obtain permission(s) to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, 501 Boylston Street, Suite 900, Boston, Massachusetts 02116 Many of the designations by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed in initial caps or all caps     Library of Congress Cataloging-in-Publication Data Savitch, Walter J., 1943Problem solving with C++ / Walter Savitch ; contributor, Kenrick Mock Ninth edition pages cm Includes index ISBN-13: 978-0-13-359174-3 (alkaline paper) ISBN-10: 0-13-359174-3 (alkaline paper) C++ (Computer program language) Problem solving I Mock, Kenrick II Title QA76.73.C153S29 2014 005.13'3 dc23 2013048487 10 1—CW—15 14 13 12 11 www.pearsonhighered.com ISBN 10: 0-13-359174-3 ISBN 13: 978-0-13-359174-3 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 v 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 through 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 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 and Pointers and dynamic arrays: Chapter 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 This material covers all control structures, function definitions, and the basic file I/O Preface Arrays and strings: Chapter 7, Sections 8.1 and 8.2 of Chapter 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 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 vii viii Preface Dependency Chart The dependency chart on the next page shows possible orderings of chapters and subsections A line joining two boxes means that the upper box must be covered before the lower box Any ordering that is consistent with this partial ordering can be read without loss of continuity If a box contains a section number or numbers, then the box refers only to those sections and not to the entire chapter Summary Boxes Each major point is summarized in a boxed section These boxed sections are spread throughout each chapter Self-Test Exercises Each chapter contains numerous Self-Test Exercises at strategic points Complete answers for all the Self-Test Exercises are given at the end of each chapter VideoNotes VideoNote VideoNotes are designed for teaching students key programming concepts and techniques These short step-by-step videos demonstrate how to solve ­problems from design through coding VideoNotes ­allow for self-paced ­instruction with easy navigation including the ability to select, play, rewind, fast-forward, and stop within each VideoNote exercise Online Practice and Assessment with MyProgrammingLab MyProgrammingLab helps students fully grasp the logic, semantics, and syntax of programming Through practice exercises and immediate, personalized feedback, MyProgrammingLab improves the programming competence of beginning students who often struggle with the basic concepts and paradigms of popular high-level programming languages A self-study and homework tool, a MyProgrammingLab course consists of hundreds of small practice problems organized around the structure of this textbook For students, the system automatically detects errors in the logic and syntax of their code submissions and offers targeted hints that enable students to figure out what went wrong—and why For instructors, a comprehensive gradebook tracks correct and incorrect answers and stores the code inputted by students for review MyProgrammingLab is offered to users of this book in partnership with Turing’s Craft, the makers of the CodeLab interactive programming exercise system For a full demonstration, to see feedback from instructors and students, or to get started using MyProgrammingLab in your course, visit www.myprogramminglab.com Preface DISPLAY P.1   Dependency Chart Chapter Introduction Chapter C++ Basics Chapter Functions Chapter Functions Chapter More Flow of Control Chapter Arrays 7.1–7.3 Chapter I/O Streams Chapter 14 Recursion Chapter 10 Classes Chapter 7.4 MultiDimensional Arrays Chapter 11 Classes 11.1–11.2 Chapter Strings and Vectors *Chapter 16 Exception Handling Chapter 12 Separate ­Compilation & Namespaces Chapter 11 11.3 Classes & Arrays Chapter Pointers and Dynamic Arrays Chapter 11 11.4 Classes & Dynamic Arrays Chapter 13 Pointers and Linked Lists Chapter 15 Inheritance Chapter 17 Templates *Chapter 16 contains occasional references to derived classes, but those references can be omitted Chapter 18 STL ix products obtained using, 156–157 semicolons (;) and, 149–150 sentinel value, 158 sums obtained using, 155–156 uninitialized variables and, 152 while statements, 84–91, 139–144, 153–154 zero times body execution, 87, 141 Low-level language, M Machine language, 8–9 main() function, 25 Main memory, 3–5 Mainframe computer systems, Manipulator functions, 329 map class, 983–990 Member functions, 312–314, 338–354, 465–466, 480–483, 554–558, 570–574, 576–588, 818–821, 845, 848–856 at, 480–481 accessor functions and, 567–568 BankAccount class examples of, 570–574 blank spaces and, 338–339 C strings, 465–466 character I/O and, 338–354 classes and, 312–314, 554–558, 570–574, 576–588 constructors, 576–588 definition of, 554–558 dot (.) operator used for, 313, 557–558 eof, 353–354 exit, 315 fail, 314 get, 338–341 getline, 465–466 inheritance and, 845, 850–852 Index length, 480–481 mutator functions and, 567–568 new_line( ), 343–345, 347–348 new–line character (\n) and, 338–360, 345–346 objects and, 312–314 private, 559–568, 848–850 protected, 850–852 public, 559–568 put, 341–342 putback function, 342–343 recursion and, 818–821 redefinition of, 853–856 scope resolution (::) operator used for, 557–558 stream I/O using, 312–314 string class use of, 480–483 Member names, structures, 543, 545–546 Member values, structures, 543, 546 Member variables, structures, 543, 545–547, 550 Memory, 3–6, 40–42, 262–264, 382–383, 393–394, 516–518 addresses, 4–5 array declaration and, 382–383 array parameters, 393–394 bits (binary digits), bytes, 4–5 call-by-reference parameters and, 262–264 computer hardware components, 3–6 delete operator for, 517–518 dynamic variables, 516–518 files, freestore, 516–517 locations, 4–5, 41–42, 262–264 main, 3–5 1047 management, 516–518 pointers for, 516–518 random access (RAM), secondary (auxiliary), 3, 6–7 sequential access of, storage as, variables and, 40–42 Menus, 133–134 program choices using, 133–134 switch statements for, 133–134 Messages, errors, 30–31 Monitor, computer output device, Multidimensional arrays, 425–431, 530–532 commas (,) between indexes, 431 declarations for, 426–427 delete [ ] operator and, 530–531 dynamic, 530–532 indexed variables and, 426, 431 parameters, 426–427 size of, 426–427 square brackets [ ] used for, 427, 431 two–dimensional example of, 427, 531–532 Multiplication operator (*), 70 Mutable iterators, 970 Mutator functions, 567–568 N Names, 42–45, 49, 95–97, 207–208, 221–224, 232–238, 308–310, 318, 518–520 constants, 95–97, 221–224 data types, 44–45 external file, 310 files, 308–310, 318 formal parameters, 207–208 functions and, 221–224, 232–238 1048 Index Names (continued) global constants, 221–224 identifiers, 42–44 overloading functions, 232–238 pointer types, 518–520 procedural abstractions, 207–208 streams, 308–310, 318 typedef function, 518–520 variables, 42–45, 49, 308 Namespaces, 52–53, 186, 227–229, 335–336, 719–732 classes and, 719–732 creating, 721–723 file I/O and, 335–336 global, 732 local variables and, 227–229 names for, 724–726, 731 output and, 52–53 qualifying names, 724–726 stream I/O and, 335–336 unnamed, 726–732 using directives for, 52–53, 186, 228–229, 335–336, 719–721, 724–726 Nesting, 120–123, 137, 154, 160–161, 208–211, 916 blocks, 137, 916 braces { } used for, 121–123, 137–138 break statement in, 154 dangling else problem, 121–122 function calls and, 208 if-else statements, 120–123 indenting statements, 120–121 loops, 154, 160–161, 208–211 multiway branches, 120–123, 137 procedural abstraction and, 208–211 scope of the block for, 137 statements, 120–123 try-catch blocks, 916 Network computer systems, new operator, 513–515, 524–527 dynamic arrays and, 524–527 pointers using, 513–515 new_line( ) function, 343–345, 347–348 New–line instruction (\n), 23, 53, 54–55, 58, 338–360, 345–346 C++ programming and, 23 endl used in place of, 54–55 input and, 345–346 member functions and, 338–360 output and, 54–55 Node class, 762–765 Nodes, 740–742, 747–750, 755–765 arrow (->) operator used with, 742, 744 binary trees and, 761–762 doubly linked lists, 760–761 head (front) of lists, inserting at, 747–749 inserting to lists, 747–749, 755–757 leaf, 762 linked lists and, 740–742, 747–750, 755–757 lost, 750–751 middle of lists, inserting and removing, 755–757 NULL constant used in, 742–744 pointer variables and, 741–742 removing from lists, 755–757 root, 762 searching linked lists using, 751–754 structures, 740–742 trees and, 761–762 Nonmember functions, 624–628 Nonmodifying sequence algorithms, 997–1001 Not equal to comparison operator (!=), 78–79 Not operator (!), 79, 113, 118 Null (/0) character, 453–454, 456 NULL constant, 742–744 Null statements, 150 nullptr, in C++ 11 programming, 745 Number formatting, decimal points for, 55–56 Number–to–C string conversions, 466–470 Numeric calculations, 143–146, 155–157 See also Arithmetic operators for loop statements for, 143–146 loop design for, 155–157 products, 156–157 sums, 155–156 Numeric data values, 44, 60–64 O Object code, 9–11, 26–27 Object-oriented programming (OOP), 16–17 classes, 17 encapsulation, 17 inheritance, 17 polymorphism, 17 program design using, 16–17 Objects, 312–315, 554, 566, 569, 576–588 assignment operator (=) used with, 569 classes and, 312–315, 554, 566 constructors for, 576–588 file I/O and, 312–315 initialization of, 576–583 member functions, 312, 576–588 public and private ­specification, 566 streams and, 312–315 Off–by–one error, 162 ofstream, 308–309, 318 Op operator, 74 open function, 309–310, 318 Operating systems, computer software for, Operators, 69–74, 77–82, 87–91, 112–120, 309, 316–318, 464, 643–658, 1016–1017, 1032–1033 arithmetic, 69–74, 112–116 Boolean expressions, 77–79, 112–120 comparison, 77–82 decrement ( ), 87–91 extraction (>>), 309, 316–318, 464, 650–658 increment (++), 87–91 insertion (

Ngày đăng: 23/03/2023, 22:41