Statistics
3
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-04-01
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
(This page has no text content)
Starting Out with C++ Early Objects Tenth Edition Tony Gaddis Judy Walters Godfrey Muganda
Senior Vice President Courseware Portfolio Management: Marcia Horton Vice President Courseware Engineering, Computer Science & Global Editions: Julian Partridge Executive Portfolio Manager: Matt Goldstein Portfolio Management Assistant: Meghan Jacoby Product Marketing Manager: Yvonne Vannatta Field Marketing Manager: Demetrius Hall Marketing Assistant: Jon Bryant Managing Producer: Scott Disanno Content Producer: Amanda Brands Manufacturing Buyer, Higher Ed, Lake Side Communications, Inc. (LSC): Maura Zaldivar-Garcia Cover Designer: Pearson CSC Cover: bluestocking/Getty Images Manager, Rights and Permissions: Ben Ferrini
Inventory Manager: Bruce Boundy Full-Service Project Management: Abhishan Sharma, Integra Software Services Pvt. Ltd. Composition: Integra Software Services Pvt. Ltd. Printer/Binder: Lake Side Communications, Inc. Copyright © 2020, 2017, 2014 Pearson Education, Inc., Hoboken, NJ 07030. 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. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions department, please visit www.pearson.com/permissions/. Microsoft and/or its respective suppliers make no representations about the suitability of the information contained in the documents and related graphics published as part of the services for any purpose. All such documents and related graphics are provided “as is” without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information, including all warranties and conditions of merchantability, whether express, implied or statutory, fitness for a particular purpose, title and
non-infringement. In no event shall Microsoft and/or its respective suppliers be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortious action, arising out of or in connection with the use or performance of information available from the services. The documents and related graphics contained herein could include technical inaccuracies or typographical errors. Changes are periodically added to the information herein. Microsoft and/or its respective suppliers may make improvements and/or changes in the product(s) and/or the program(s) described herein at any time. Partial screen shots may be viewed in full within the software version specified. Microsoft Windows , and Microsoft Office are registered trademarks of the Microsoft Corporation in the U.S.A. and other countries. This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation. The programs and applications presented in this book have been included for their instructional value. They have been tested with care, but are not guaranteed for any particular purpose. The publisher does not offer any warranties or representations, nor does it accept any liabilities with respect to the programs or applications. 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 Names: Gaddis, Tony, author. | Walters, Judy, author. | Muganda, Godfrey, author. Title: Starting out with C++. Early objects / Tony Gaddis, Judy Walters, Godfrey Muganda. Description: Tenth edition. | Hoboken : Pearson Education, Inc., [2020] | Includes index. Identifiers: LCCN 2018048221| ISBN 9780135235003 | ISBN 0135235006 Subjects: LCSH: C++ (Computer program language) Classification: LCC QA76.73.C153 G333 2020 | DDC 005.13/3— dc23 LC record available at https://lccn.loc.gov/2018048221 10 9 8 7 6 5 4 3 2 1 ISBN 10:  0-13-523500-6 ISBN 13: 978-0-13-523500-3
(This page has no text content)
Contents at a Glance Preface xv Chapter 1 Introduction to Computers and Programming 1 Chapter 2 Introduction to C++ 29 Chapter 3 Expressions and Interactivity 79 Chapter 4 Making Decisions 157 Chapter 5 Looping 247 Chapter 6 Functions 327 Chapter 7 Introduction to Classes and Objects 411 Chapter 8 Arrays and Vectors 513 Chapter 9 Searching, Sorting, and Algorithm Analysis 613 Chapter 10 Pointers 659 Chapter 11 More about Classes and Object-Oriented Programming 717 Chapter 12 More on C-Strings and the string Class 821 Chapter 13 Advanced File and I/O Operations 867 Chapter 14 Recursion 929
Chapter 15 Polymorphism and Virtual Functions 963 Chapter 16 Exceptions and Templates 1001 Chapter 17 The Standard Template Library 1035 Chapter 18 Linked Lists 1131 Chapter 19 Stacks and Queues 1183 Chapter 20 Binary Trees 1223 Appendix A: The ASCII Character Set 1253 Appendix B: Operator Precedence and Associativity 1257 Appendix C: Answers to Checkpoints 1259 Appendix D: Answers to Odd-Numbered Review Questions 1301 Index 1323
Contents Preface xv Chapter 1 Introduction to Computers and Programming 1 1.1 Why Program? 1 1.2 Computer Systems: Hardware and Software 3 1.3 Programs and Programming Languages 8 1.4 What Is a Program Made of? 13 1.5 Input, Processing, and Output 17 1.6 The Programming Process 18 1.7 Tying It All Together: Hi! It’s Me 23 Chapter 2 Introduction to C++ 29 2.1 The Parts of a C++ Program 29 2.2 The cout Object 33 2.3 The #include Directive 38 2.4 Variables and the Assignment Statement 39 2.5 Literals 41 2.6 Identifiers 43
2.7 Integer Data Types 45 2.8 Floating-Point Data Types 50 2.9 The char Data Type 54 2.10 The C++ string Class 58 2.11 The bool Data Type 59 2.12 Determining the Size of a Data Type 60 2.13 More on Variable Assignments and Initialization 61 2.14 Scope 63 2.15 Arithmetic Operators 64 2.16 Comments 68 2.17 Programming Style 69 2.18 Tying It All Together: Smile! 71 Chapter 3 Expressions and Interactivity 79 3.1 The cin Object 79 3.2 Mathematical Expressions 86 3.3 Data Type Conversion and Type Casting 94 3.4 Overflow and Underflow 100 3.5 Named Constants 101 3.6 Multiple and Combined Assignment 104
3.7 Formatting Output 108 3.8 Working with Characters and Strings 118 3.9 More Mathematical Library Functions 132 3.10 Random Numbers 134 3.11 Focus on Debugging: Hand Tracing a Program 138 3.12 Green Fields Landscaping Case Study—Part 1 140 3.13 Tying It All Together: Word Game 143 Chapter 4 Making Decisions 157 4.1 Relational Operators 157 4.2 The if Statement 163 4.3 The if/else Statement 172 4.4 The if/else if Statement 177 4.5 Menu-Driven Programs 185 4.6 Nested if Statements 187 4.7 Logical Operators 191 4.8 Validating User Input 200 4.9 More about Blocks and Scope 202 4.10 More about Characters and Strings 205 4.11 The Conditional Operator 211
4.12 The switch Statement 215 4.13 Enumerated Data Types 224 4.14 Focus on Testing and Debugging: Validating Output Results 227 4.15 Green Fields Landscaping Case Study—Part 2 229 4.16 Tying It All Together: Fortune Teller 234 Chapter 5 Looping 247 5.1 Introduction to Loops: The while Loop 247 5.2 Using the while Loop for Input Validation 254 5.3 The Increment and Decrement Operators 257 5.4 Counters 262 5.5 Keeping a Running Total 264 5.6 Sentinels 267 5.7 The do-while Loop 269 5.8 The for Loop 275 5.9 Deciding Which Loop to Use 281 5.10 Nested Loops 283 5.11 Breaking Out of a Loop 285 5.12 Using Files for Data Storage 288
5.13 Focus on Testing and Debugging: Creating Good Test Data 304 5.14 Central Mountain Credit Union Case Study 307 5.15 Tying It All Together: What a Colorful World 311 Chapter 6 Functions 327 6.1 Modular Programming 327 6.2 Defining and Calling Functions 328 6.3 Function Prototypes 336 6.4 Sending Data into a Function 337 6.5 Passing Data by Value 342 6.6 The return Statement 346 6.7 Returning a Value from a Function 347 6.8 Returning a Boolean Value 353 6.9 Using Functions in a Menu-Driven Program 355 6.10 Local and Global Variables 359 6.11 Static Local Variables 366 6.12 Default Arguments 368 6.13 Using Reference Variables as Parameters 372 6.14 Overloading Functions 382
6.15 The exit() Function 386 6.16 Stubs and Drivers 389 6.17 Little Lotto Case Study 391 6.18 Tying It All Together: Glowing Jack-o-lantern 396 Chapter 7 Introduction to Classes and Objects 411 7.1 Abstract Data Types 411 7.2 Object-Oriented Programming 413 7.3 Introduction to Classes 415 7.4 Creating and Using Objects 418 7.5 Defining Member Functions 420 7.6 Constructors 427 7.7 Destructors 435 7.8 Private Member Functions 438 7.9 Passing Objects to Functions 441 7.10 Object Composition 448 7.11 Separating Class Specification, Implementation, and Client Code 452 7.12 Structures 459 7.13 More about Enumerated Data Types 471
7.14 Home Software Company OOP Case Study 475 7.15 Introduction to Object-Oriented Analysis and Design 482 7.16 Screen Control 492 7.17 Tying It All Together: Yoyo Animation 497 Chapter 8 Arrays and Vectors 513 8.1 Arrays Hold Multiple Values 513 8.2 Accessing Array Elements 515 8.3 Inputting and Displaying Array Data 517 8.4 Array Initialization 524 8.5 The Range-Based for Loop 531 8.6 Processing Array Contents 534 8.7 Using Parallel Arrays 546 8.8 The typedef Statement 550 8.9 Arrays as Function Arguments 550 8.10 Two-Dimensional Arrays 560 8.11 Arrays with Three or More Dimensions 567 8.12 Introduction to the STL vector 571 8.13 Arrays of Objects 583
8.14 National Commerce Bank Case Study 593 8.15 Tying It All Together: Rock, Paper, Scissors 595 Chapter 9 Searching, Sorting, and Algorithm Analysis 613 9.1 Introduction to Search Algorithms 613 9.2 Searching an Array of Objects 620 9.3 Introduction to Sorting Algorithms 623 9.4 Sorting an Array of Objects 634 9.5 Sorting and Searching Vectors 637 9.6 Introduction to Analysis of Algorithms 641 9.7 Case Studies 649 9.8 Tying It All Together: Secret Messages 649 Chapter 10 Pointers 659 10.1 Pointers and the Address Operator 659 10.2 Pointer Variables 661 10.3 The Relationship Between Arrays and Pointers 665 10.4 Pointer Arithmetic 669 10.5 Initializing Pointers 670 10.6 Comparing Pointers 673 10.7 Pointers as Function Parameters 675
10.8 Pointers to Constants and Constant Pointers 679 10.9 Dynamic Memory Allocation 684 10.10 Returning Pointers from Functions 688 10.11 Pointers to Class Objects and Structures 694 10.12 Selecting Members of Objects 698 10.13 Smart Pointers 700 10.14 Tying It All Together: Pardon Me, Do You Have the Time? 708 Chapter 11 More about Classes and Object-Oriented Programming 717 11.1 The this Pointer and Constant Member Functions 717 11.2 Static Members 722 11.3 Friends of Classes 729 11.4 Memberwise Assignment 734 11.5 Copy Constructors 735 11.6 Operator Overloading 744 11.7 Rvalue References and Move Operations 765 11.8 Type Conversion Operators 775 11.9 Convert Constructors 778 11.10 Aggregation and Composition 782
11.11 Inheritance 787 11.12 Protected Members and Class Access 792 11.13 Constructors, Destructors, and Inheritance 798 11.14 Overriding Base Class Functions 803 11.15 Tying It All Together: Putting Data on the World Wide Web 806 Chapter 12 More on C-Strings and the string Class 821 12.1 Character Testing 821 12.2 Character Case Conversion 825 12.3 C-Strings 828 12.4 Library Functions for Working with C-Strings 833 12.5 Conversions Between Numbers and Strings 842 12.6 Writing Your Own C-String Handling Functions 846 12.7 More about the C++ string Class 852 12.8 Advanced Software Enterprises Case Study 855 12.9 Tying It All Together: Program Execution Environments 857 Chapter 13 Advanced File and I/O Operations 867 13.1 Input and Output Streams 867 13.2 More Detailed Error Testing 875