1. Trang chủ
  2. » Công Nghệ Thông Tin

Problem solving with c++ 10th global edition

1,1K 170 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.117
Dung lượng 26,28 MB

Nội dung

GLOBAL EDITION Problem Solving with C++    TENTH EDITION    Walter Savitch Digital Resources for Students Your new textbook provides 12-month access to digital resources that may include VideoNotes (step-by-step video tutorials on programming concepts), source code, web chapters, quizzes, and more Refer to the preface in the textbook for a detailed list of resources Follow the instructions below to register for the Companion Website for Walter Savitch’s Problem Solving with C++, Tenth Edition, Global Edition Go to www.pearsonglobaleditions.com/savitch Enter the title of your textbook or browse by author name Click Companion Website Click Register and follow the on-screen instructions to create a login name and password Use a coin to scratch off the coating and reveal your access code Do not use a sharp knife or other sharp object as it may damage the code Use the login name and password you created during registration to start using the online resources that accompany your textbook IMPORTANT: This prepaid subscription does not include access to MyProgrammingLab, which is available at www.myprogramminglab.com for purchase This access code can only be used once This subscription is valid for 12 months upon activation and is not transferable If the access code has already been revealed it may no longer be valid For technical support go to https://support.pearson.com/getsupport/ This page intentionally left blank PROBLEM SOLVING with C++ This page intentionally left blank Tenth Edition Global Edition PROBLEM SOLVING with C++ Walter J Savitch UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR Kenrick Mock UNIVERSITY OF ALASKA, ANCHORAGE 330 Hudson Street, New York, NY 10013 S enior Vice President Courseware Portfolio Management: Director, Portfolio Management: Engineering, Computer Science & Global Editions: Portfolio Manager: Assistant Acquisitions Editor, Global Edition: Portfolio Management Assistant: Field Marketing Manager: Product Marketing Manager: Managing Producer, ECS and Math: Content Producer: Project Editor, Global Edition: Senior Manufacturing Controller, Global Edition: Manager, Media Production, Global Edition: Cover Designer: Cover Photo: Marcia J Horton Julian Partridge Matt Goldstein Aditee Agarwal Kristy Alaura Demetrius Hall Yvonne Vannatta Scott Disanno Sandra L Rodriguez K.K Neelakantan Angela Hawksbee Vikram Kumar Lumina Datamatics, Inc Iana Chyrva/Shutterstock The author and publisher of this book have used their best efforts in preparing this book These efforts include the development, research, and testing of theories and programs to determine their effectiveness The author and publisher make no warranty of any kind, expressed or implied, with regard to these programs or the documentation contained in this book The author and publisher shall not be liable in any event for incidental or consequential damages with, or arising out of, the furnishing, performance, or use of these programs Pearson Education Limited KAO Two KAO Park Harlow CM17 9NA United Kingdom and Associated Companies throughout the world Visit us on the World Wide Web at: www.pearsonglobaleditions.com © Pearson Education Limited 2018 The rights of Walter Savitch to be identified as the author of this work has been asserted by him in accordance with the Copyright, Designs and Patents Act 1988 Authorized adaptation from the United States edition, entitled Problem Solving with C++, 10th Edition, ISBN 978-0-13-444828-2 by Walter Savitch published by Pearson Education © 2018 All rights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without either the prior written permission of the publisher or a license permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, Saffron House, 6–10 Kirby Street, London EC1N 8TS All trademarks used herein are the property of their respective owners The use of any trademark in this text does not vest in the author or publisher any trademark ownership rights in such trademarks, nor does the use of such trademarks imply any affiliation with or endorsement of this book by such owners British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library 10 9 8 7 6 5 4 3 2 1 ISBN 10:   1-292-22282-4 ISBN 13: 978-1-292-22282-0 Typeset by iEnergizer Aptara®, Ltd Printed and bound in Malaysia 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 tenth 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 Tenth Edition This tenth edition presents the same programming philosophy as the ninth edition All of the material from the ninth edition remains, but with the following enhancements: ■ Consistent use of camelCase notation rather than underscore_case throughout the text ■ Discussion in Chapter 10 of shallow vs deep copy ■ Additional material in Chapter 12 and 17 on compiling templates with header files ■ Additional material in Chapter 18 on the std::array class, regular expressions, threads, and smart pointers in C++11 ■ Correction of errata and edits for clarity such as indicating preferred methods for file I/O, naming of terminology, better definition of encapsulation, and removing material that is now standard in C++11 and higher ■ Ten new Programming Projects ■ Five new VideoNotes for a total of sixty nine 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 ninth 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 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++14 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, the STL (Standard Template Library), threads, regular expressions, and smart pointers 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 1100 INDEX Functions (continued) digit_to_int implementation, 669–670 driver programs for, 314–316 equal, 654–660 factorial (n!), 262–263 flags and, 359–361 formatting output using, 357–372 friend, 654–676 graph, 441 indexed variables as arguments, 423–425 inheritance and, 884, 887–890 inline, 1078 input/output (I/O), 357–383, 372–383, 1072–1073 local variables and, 250–261, 302–303 manipulators, 363 member, 346–348, 372–383 member functions accessor, 585–586 mutator, 601–602 names, 253–256, 264–270 nonmember, 658–662 not inherited, 884, 893–894 overloading names, 264–270 overriding, 903 parameters, 229–233, 239–240, 256–258, 291–298, 425–431 polymorphism and, 898–910 predefined, 215–224, 491–494, 501–502 procedural abstraction and, 236–249, 305–313 programmer-defined, 225–235 random number generator, 220–221, 1076 recursive, 825–841 redefining functions, 887–890 return statements, 228–229, 234, 287–291 returning an array, 431–432 scale, 436–441 signature, 891 stream I/O, 357–372 string, 1075 stub, 316–318 subtasks, 283–335, 433–434 tasks, recursion for, 825–837 testing, 246–249, 313–319 throwing exceptions in, 943–945 top-down design for, 214–215, 432–443 trigonometric, 1077 type casting, 222–224 value returned, 213–282, 838–841 virtual, 898–910 void, 284–291 G Generic algorithms, 1025–1038 big-O notation, 1028–1029 container access running times, 1029–1030 container modifying, 1035–1036 nonmodifying sequence, 1031–1035 running times, 1025–1030 set, 1037–1038 sorting, 1038 templates for, 1025–1038 get function, 372–375 getline function, 499–500, 509–510, 512–513 Global named constants, 253–256 Global scope, 258–259 Global variables, 255–256, 552 graph function, 441 Greater than comparison operator (>), 110 Greater than or equal to comparison operator (>=), 110, 112 H Handling exceptions, 928 Hard disks, 38 Hardware computer systems and, 34–39, 64 input/output devices, 35 main memory, 35–37 processor (CPU), 35, 38–39 secondary memory, 38 Header files (< >), predefined functions, 216–218 Hierarchy of structures, 583 High-level languages, 40–41 I Identifiers, variables, 74–76 if-else statements, 107–114, 152–160 Boolean expressions for, 109–111 braces { } used with, 114, 153–155 branching mechanisms, 107–114, 152–160 comparison operators for, 109–114 compound statements and, 114 dangling else problem, 153–155 indenting, 152–153, 155–157 multiway branches, 155–160 nested, 152–155 #ifndef directive, 750–752 ifstream, 342–343, 352 Implementation files, ADT, 626–631, 742–749, 752–753 Implementation phase, 47 #include directive, 53, 57–58, 84–85, 216–218, 343, 352, 750–752 C++ programming and, 53, 57–58 directive notation (#) for, 57 file I/O, 343, 352, 363 header files and, 216–218 #ifndef directive and, 750–752 manipulator functions and, 363 output and, 84–85 predefined functions and, 218–219 preprocessors for, 218 separate compilation and, 750–752 Increment operators (++), 119–123, 173–176, 994–995, 1001–1003, 1005 Indentation, C++ programming and, 125 Indenting branching statements, 152–153, 155–157 Index (subscript) of arrays, 413, 417–418 Indexed variables, 413–420, 423–425, 460, 465 arguments to functions, as, 423–425 arrays and, 413–420 commas (,) between, 465 declaration of, 413–418 functions and, 423–425 illegal range of, 417–418 initializing, 420 multidimensional arrays, 460, 465 square brackets [ ] used for, 412–413, 465 Infinite loop statements, 119–123, 184 Infinite recursion, 833 Information hiding, 237, 631 See also Procedural abstraction Inheritance, 49, 632–637, 867–926 ancestor class, 878 INDEX 1101 assignment (=) operators used for, 894–895 base class, 868, 870–871, 882–884, 892 child class, 634, 868, 878 class hierarchy, 633–634 colon (:) used for, 634–635 constructors used in, 879–882 copy constructors used in, 894–895 derived classes and, 632–637, 868–870, 871–879, 888–890, 894–895 descendants, 878 destructors and, 894–895 function signature, 891 functions not inherited, 884, 893–894 member functions, 879, 884–886, 887–890 parent class, 634–635, 868, 878 polymorphism and, 896–910 private members and, 882–884 protected qualifier, 884–886 redefining functions, 887–890 Initialization, 80–81, 177–178, 420, 488–489, 585, 610–617 arrays, 420, 488–489 C strings, 488–489 constructors for, 610–617 declaration and, 80–81 objects, 610–617 structures, 585 variables, 80–81, 177–178, 420, 488–489 Inline functions, 1078 Input, 35, 53–55, 82, 88–90, 189–192, 340–346, 377–383 character data, 377–383 cin statements for, 53–55, 88–89 computer hardware devices, 35 echoing, 90 extraction operator (>>) for, 343 get function, 372–375 loops, design for ending, 189–192 member functions for, 377–382 new_line( ), 377–379, 381–382 new-line character (\n) and, 379–380 put function, 375–376 putback function, 376–377 reading files, 342–343 streams, 82, 340–346 1102 INDEX Input iterators, 1006 Input/output (I/O), 82–91, 339–410, 498–500, 509–511, 1072–1073 arguments (parameters) and, 366, 382–383 C++ programming and, 82–91 C strings, 498–500 character, 372–394 cin (input) statements, 88–89 cout (output) statements, 82–84 decimal points for formatting numbers, 87–88 designing, 90 double statements, 87–88 end of files (eof), 366–369, 387–388 escape sequences, 85–87 files, 340–357, 366–371 flags, 359–361 formatting, 357–372 functions, 357–383, 387–388, 1072–1073 getline function, 509–510 #include directive, 84–85 manipulators, 363 namespaces, 84–85, 369–370 new_line function, 377–378, 380–382 new-line instruction (\n), 86–87, 90, 374–375, 379–380 predefined character functions, 390–394 streams, 82, 339–410 string class for, 509–511 using directive, 84, 369–370 Insertion operator (

Ngày đăng: 02/03/2019, 10:35

TỪ KHÓA LIÊN QUAN