ebook img

Starting Out with C++: From Control Structures through Objects PDF

1249 Pages·6·177.23 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Starting Out with C++: From Control Structures through Objects

NG OUT WITH START~ Fr,om Contr,ot Structures, through Objoects sixth edition c++ Quick Reference c++ Data Types Commonly Used Operators Data Type Description Assignment Operators char CbanK::ter = Assignment +.. Combined addition/assignment unsigned char Unsigned Character -". Combined subtraction/assignment int Integer *'" Combined multiplication/assignment short int Short integer 1- Combined division/assignment '''' Combined modulus/assignment short Same as short int Arithmetic Operators unsigned short int Unsigned shon integer + Addition unsigned short Same as unsigned short int Subtraction unsigned int Unsigned integer .. Multiplication 1 Division unsigned Same as unsigned int % Modulus (remainder) long i nt Long integer Relational Operators long Same as long int < Less than unsigned long int Unsigned long integer <- Less than or equal to > Greater than unsigned long Sarneas unsigned long int >'" Greater Ihall or equal 10 float Single precision floating point -- Equal to !;- Not equal to double double precision floating point Logical Operators long double Long double precision floating Ii Ii AND point II OR I NOT Increment/Decrement Forms of the 1f Statement ++ lncrement Decrement Simple if Example if (expression) if (x < y) statement; x++; Conditional Operator 7: For-m: if/else Example expression ? expression ; expression if ( expression) if (x < y) Example: ,statement; x++; x '" a < b ? a : b; else else The sratement above works like: statement; x--; if(3<b) g '" a; e18e if/else if Example x - b; if (expression) if (x < y) statement; x++; else if (expression) else if (x < z) The wbile Loop statement; x--; Form: Example: else else while (expression) while (x < 100) statement; cout « xo4o+ « end1; statement; y++; while (expression) while (x < 100) To conditionally-execute more than one { ( statement, enclose the statements in braces: .9tatement; cout « x « endl; statement; X++i Form Example ) ) if (expression) if (x < y) ( ( statement; x++; x, statement; cout « The do-wbile Loop I ) Form: Example: do do statement, cout « x++ « endl; while (expression): while (x < 100); Web Sites For tbe Sf1lrting Out with C++ Series do do { { aw.com/gaddisbooks statement; cout « x « end1; For Addison-Wesley Computing statement; x++; } while (expression) ; } while (x <: 100); aw.com/computing LOCATION OF VIDEO NOTES IN THE TEXT Chapter 1 Introduction to Flowcharting, p. '9 Designing a Program with Pseudocode, p. 19 Designing the Account Balance Program, p. 25 Predicting the Result of Problem 33, p. 25 Chapter 2 Using cout, p. 33 Variable Definitions, p. 39 Assignment Statements and Simple Math Expressions, p. 61 Solving the Restaurant Bill problem, p. 77 Chapter 3 Reading.lnput with cin, p. 83 Formatting Numbers with setprecision, p. 119 Writing Data to a File, p. 144 Reading Data from a File, p. 145 Solving the Stadium Seating Problem, p. 156 Chapter 4 The if Statement, p. 168 The if/else Statement, p. ' 81 The if/else if Statement, p. 191 Solving the Time Calculator Problem, p. 239 Chapter 5 The while Loop, p. 254 The for Loop, p. 270 Solving the Calories Burned Problem, p. 299 Chapter 6 Functions and Arguments, p. 319 Value-Returning Functions, p. 332 Solving the Markup Problem, p. 375 Chapter 7 Accessing Array Elements with a loop, p. 392 Passing an Array to a Function, p. 415 Solving the Chips and Salsa Problem, p. 457 ChapterS The Binary Search, p. 470 The Selection Sort, p. 485 Solving the Charge Account Validation Modification Problem, p. 503 Chapter 9 Dynamically Allocating an Array, p. 535 Solving the getString Function Problem, p. 554 Chapter 10 Writing a C-String-Handling Function, p. 582 Using the string Class, p. 588 VANWYLEN Solving the Backward String Problem, p. 602 (CO'Yiffi1J03 'mOWge) Hope College Holland, Michigan LOCATION OF VIDEO NOTES IN THE TEXT (continued) -:"';' ."", -- Chapter 11 Creating a Structure, p. 611 Passing a Structure to a Function, p. 630 Solving the Weather Statistics Problem, p. 664 Chapter 12 Passing File Stream Objects to Functions, p. 683 Working with Multiple Files, p_ 69S Solving the File Encryption Filter Problem, p_ 725 Chapter 13 Writing a Class, p_ 736 Defining an Instance of a Class, p. 741 Solving the Employee Class Problem, p_ 816 . Chapter 14 Operator Overloading, p. 843 Class Aggregation, p. 872 Solving the NumDays Problem, p_ 888 Chapter 15 Redefining a Base Class Function in a Derived Class, p. 917 Polymorphism, p. 927 Solving the Employee and ProductionWorker Classes Problem, p. 960 '. Chapter 16 Throwing an Exception, p. 972 • T '''.~ Handling an Exception, p. 973 Writing a Function Template, p_ 991 Storing Objects in a vector p. 1011 I Solving the Exception Project Problem, p_ 1025 Chapter 17 Appending a Node to a Linked List, p_ 1030 Inserting a Node in a Linked List, p. 1037 Deleting a Node from a Linked List, p_ 1042 Sulvill~ the Mernber InSt!rlion by Pu~ition Pruult!HI, p. 1064 Chapter 18 Storing Objects in an STL stack, p. 1083 Storing Objects in an STL queue, p_ 1098 Solving the File Compare Problem, p. 1104 Chapter 19 Reducing a Problem with Recursion, p_ 1112 Solving the Recursive Multiplication Problem, p. 1141 Chapter 20 Inserting a Node in a Binary Tree, p_ 1150 Deleting a Node from a Binary Tree, p_ 11 56 Solving the Node Counter Problem, p. 1167 From Control Structures through Objects 6th Edition Tony Gaddis , - PEARSON Addison Wesley BOSTon San Francisco New York London Toronto Sydney Tokyo Singapore Madrid Mexico City Munich Pans Cape "Iown Hong Kong Montrt"al Executive Editor Michael Hirsch Editorial Assistant Stephanie Sellinger Senior Production Supervisor Marilyn Lloyd Text Designer Joyce Cosent~no Wells Cover Designer Beth Paquin Cover Jmage iStockphoto Media Producer Bethany Tidd Senior Media Buyer Ginny Michaud Marketing Manager Christopher Kelly Senior Mapufacturiug Buyer Carol Melville Production Services Aptara, Inc. Access the latest information about Addison-Wesley titles from our World Wide Web site: http:www.aw.comJcomputing Many of the designations llsed by manufactnrers and sellers to distinguish their products are claimed as trade marks. Where those designations appear in this book, and Addison-Wesley was aware of a trademark claim, the designations have been printed in initial caps or all caps. 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 warranty or representation, nor does it accept any liabilities with respect to the programs or applications. .. Library of Congress Cataloging-ill-Publication Data Gaddis, Tony. Srarring out with C++ : from control structures through objects I Tony Gaddis. - 6th cd. p. em. Includes index. lSBN-13,978-0-321-54588-6 [SBN-l0, 0-321-54588-5 I. C++ (Computer program language) 1. Title. QA76.73.CI53G332008 005.J3'3--dc22 2008008001 . Copyright ~ 2009 Pearson Education, Inc. 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 the prior written permission of the publisher. l)rinted in the United States of America. For information on obtaining permission for use of material in this work, please submit a written request to Pearson Education, inc., Rights and Contracts Departinellt, 501 Boylston Street, Suite 900, Boston, MA 02116, fax your request to 617-671-3447, or e-mad at http://www.pearsoned.com/legaVpermissions.htm. ISBN n 978-0-321-54588-6 ISBN 10, 0-321-54588-5 23456789 IO-QWT-ll10 09 08 j o '1 Preface xiii CHAPTER 1 Introduction to Computers and Programming 1 CHAPTER 2 Introduction to c++ 29 CHAPTER 3 Expressions and Interactivity 83 CHAPTER 4 Making Decisions 163 CHAPTER 5 Looping 249 CHAPTER 6 Functions 307 CHAPTER 7 Arrays 387 CHAPTER 8 Searching and Sorting Arrays 467 CHAPTER 9 Pointers 507 CHAPTER 10 Characters, Strings, and the string Class 559 CHAPTER 11 Structured Data 609 CHAPTER 12 Advanced File Operations 673 CHAPTER 13 Introduction to Classes 729 CHAPTER 14 More About Classes 825 CHAPTER 15 Inheritance, Polymorphism, and Virtual Functions 895 CHAPTER 16 Exceptions, Templates, and the Standard Template Library (STL) 971 CHAPTER 17 Linked Lists 1027 CHAPTER 18 Stacks and Queues 1067 CHAPTER 19 Recursion 1107 CHAPTER 20 Binary Tree. 1145 Appendix A: Getting Started with Alice 1171 Appendix B: The ASCII Character Set 1199 Appendix C: Operator Precedence and Associativity 1203 Index 1205 v vi Contents at a Glance Student CD The following appendices arc on the accompanying Student CD. Appendix D: Introduction to Flowcharting Appendix E: Using UML in Class Design Appendix F: Namespaces Appendix G: Writing Managed C++ Code for the .NET Framework Appendix H: Passing Command Line Arguments Appendix I: Header File and Library Function Reference Appendix ,: Binary Numbers and Bitwise Operations Appendix K: Multi-Source File Programs Appendix L: Stream Member Functions for Formatting Appendix M: Introduction to Microsoft Visual c++ 2008 Express Edition Appendix N: Answers to Checkpoints Appendix 0 : Answers to Odd Numbered Review Questions Table of Location of Video Notes Preface xiii CHAPTER 1 Introduction to Computers and Programming 1 1.1 Why Program? 1 1.2 Compmer Systems: Hardware and Software 2 1.3 Programs and Programming Languages 6 1.4 What Is a Program Made of? ]2 1.5 Input, Processing, and Output ]6 1.6 The Programming Process 17 1.7 Procedural and Object-Oriented Programming 21 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 Literals 39 2.5 Identifiers 43 2.6 Integer Data Types 44 2.7 The char Data Type 49 2.8 Floating-Poim Data Types 54 2.9 The bool Data Type 57 2.10 Determining the Size of a Data Type 58 2.11 Variable Assignments and Initialization 59 2.12 Scope 60 2.13 Arithmetic Operators 61 2.14 Commems 67 2.15 Focus on Software Engineering: Programming Style 69 2.16 llYou Plan to Continue in Computer Science: Standard and Presrandard C++ 71 CHAPTER 3 Expressions and Interactlvity 83 3.1 The cin Object 83 3.2 Mathematical Expressions 91 3.3 When You Mix Apples and Oranges: Type Conversion 100 vii viii Contents 3.4 Overflow and Underflow 102 3.5 Type Casting 104 3.6 Named Constants 107 3.7 Multiple Assignment and Combined Assignment 1.12 3.8 Formatting Output 116 3.9 Fonnartcd Tnpur 125 3.10 Focus on Object-Oriented Programming: More About Member Functions 130 3.11 More Mathematical Library Functions 131 3.12 Focus on Debugging: Hand Tracing a Program 135 3.13 Focus on Problem Solving: A Case Study 137 3.14 Introduction to File Input and Output 140 CHAPTER 4 Making Decisions 163 4.1 Relational Operators 163 4.2 The if Statement 168 4.3 Flags 176 4.4 Expanding the if Statement 177 4.5 The if/else Statement 181 4.6 Nested if Statements 184 4.7 The if/else if Statement 191 4.8 Menus 195 4.9 Logical Operators 199 4.10 Checking Numeric Ranges with Logical Operators 206 4.11 Focus on Software Engineering: Validating User Input 207 4.12 More About Variable Definitions and Scope 209 4.13 Comparing Suings 213 4.14 The Conditional Operator 218 4.15 The switch Statement 222 4.16 Testing for File Open Errors 231 CHAPTER 5 Looping 249 5.1 The Increment and Decrement Operators 249 5.2 Introduction to Loops: The while Loop 254 5.3 Using the while Loop for input Validation 261 5.4 Counters 263 5.5 The do-while Loop 265 5.6 The for Loop 270 5.7 Keeping a Running Total 280 5.8 Sentinels 283 5.9 Using a Loop to Read Data from a File 284 5.10 Focus on Software Engineering: Deciding Which Loop to Use 287 5.11 Nested Loops 287 5.12 Breaking Out of a Loop 290 5.]3 The continue Statement 292 CHAPTER 6 Functions 307 6.1 Focus on Softvvare Engineering: Modular Programming 307 6.2 Defining and CaUing Functions 309 6.3 Function Prototypes 317

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.