C++ Quick Reference - C++ Help · PDF fileC++ Quick Reference C++DataTypes Data Type...

987

Transcript of C++ Quick Reference - C++ Help · PDF fileC++ Quick Reference C++DataTypes Data Type...

  • C++ Quick Reference

    C++ Data TypesDData Type Descrip t ionchar Characterunsigned char Unsigned Characterint Integershort int Short integershort Same as short intunsigned short int Unsigned short integerunsigned short Same as unsigned short intunsigned int Unsigned integerunsigned Same as unsigned intlong int Long integerlong Same as long intunsigned long int Unsigned long integerunsigned long Same as unsigned long intfloat Single precision floating pointdouble double precision floating pointlong double Long double precision floating

    point

    Common ly Used OperatorsAAss ign ment Operators= Assignment+= Combined addition/assignment-= Combined subtraction/assignment*= Combined multiplication/assignment/= Combined division/assignment%= Combined modulus/assignmentAAri th met ic Operators+ Addition- Subtraction* Multiplication/ Division% Modulus (remainder)RRela t iona l Operators< Less than Greater than>= Greater than or equal to== Equal to!= Not equal toLLogical Operators&& AND|| OR! NOTIIncrement /Decrement++ Increment-- DecrementForms of the if SStatement

    Simp le iif Exampleif (expression) if (x < y)

    statement; x++;

    iif /e l se Exampleif (expression) if (x < y)

    statement; x++;else else

    statement; x--;

    iif /e l se if Exampleif (expression) if (x < y)

    statement; x++;else if (expression) else if (x < z)

    statement; x--;else else

    statement; y++;

    TTo condi t iona l ly--execute more than onessta tement, enclose the sta tement s in braces:Form EExampleif (expression) if (x < y){ {

    statement; x++;statement; cout

  • LOCATION OF VIDEONOTES IN THE TEXT

    Chapter 1 Introduction to Flowcharting, p. 19Designing a Program with Pseudocode, p. 19Designing the Account Balance Program, p. 25Predicting the Result of Problem 33, p. 25

    Chapter 2 Using cout, p. 31Variable Definitions, p. 37Assignment Statements and Simple Math Expressions, p. 59Solving the Restaurant Bill problem, p. 75

    Chapter 3 Reading Input with cin, p. 79Formatting Numbers with setprecision, p. 115Writing Data to a File, p. 140Reading Data from a File, p. 141Solving the Stadium Seating Problem, p. 152

    Chapter 4 The if Statement, p. 164The if/else Statement, p. 177The if/else if Statement, p. 187Solving the Time Calculator Problem, p. 235

    Chapter 5 The while Loop, p. 246The for Loop, p. 262Solving the Calories Burned Problem, p. 291

    Chapter 6 Functions and Arguments, p. 309Value-Returning Functions, p. 322Solving the Markup Problem, p. 365

    Chapter 7 Accessing Array Elements with a Loop, p. 378Passing an Array to a Function, p. 401Solving the Chips and Salsa Problem, p. 443

    Chapter 8 The Binary Search, p. 454The Selection Sort, p. 469Solving the Charge Account Validation Modification Problem, p. 487

    Chapter 9 Dynamically Allocating an Array, p. 519Solving the getString Function Problem, p. 538

    Chapter 10 Writing a C-String-Handling Function, p. 564Using the string Class, p. 570Solving the Backward String Problem, p. 584

    (continued on next page)

  • (continued) LOCATION OF VIDEONOTES IN THE TEXT

    Chapter 11 Creating a Structure, p. 591Passing a Structure to a Function, p. 610Solving the Weather Statistics Problem, p. 644

    Chapter 12 Passing File Stream Objects to Functions, p. 661Working with Multiple Files, p. 673Solving the File Encryption Filter Problem, p. 703

    Chapter 13 Writing a Class, p. 712Defining an Instance of a Class, p. 717Solving the Employee Class Problem, p. 792

    Chapter 14 Operator Overloading, p. 819Class Aggregation, p. 848Solving the NumDays Problem, p. 864

    Chapter 15 Redefining a Base Class Function in a Derived Class, p. 893Polymorphism, p. 903Solving the Employee and ProductionWorker Classes Problem, p. 936

  • From Control Structuresthrough Objects

    6th Edition Brief Version

    Tony Gaddis

  • Editor-in-Chief: Michael HirschEditorial Assistant: Stephanie SellingerManaging Editor: Jeffrey HolcombSenior Production Supervisor: Marilyn LloydMarketing Manager: Erin DavisMarketing Coordinator: Kathryn FerrantiSenior Media Buyer: Ginny MichaudMedia Assistant: Katelyn BollerSenior Manufacturing Buyer: Carol MelvilleProject Coordination: Sherrill Redd/Aptara, Inc.Production Services: Aptara, Inc.Art Director, Cover: Linda KnowlesCover Designer: Joyce Cosentino Wells/JWells DesignCover Image: Getty Images/Photographer: David Muir

    Access the latest information about Addison-Wesley Computer Science titles from our World Wide Web site: http://www.pearsonhighered.com/cs

    Many of the designations used by manufacturers 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.

    The interior of this book was composed in Aptara, Inc. The basal text font is set in Sabon 10/12.5; the chapter titles, headings, running heads, and folios are all set in Stone Sans; the programming code is set in Courier10 Pitch BT 9/11.

    Library of Congress Cataloging-in-Publication Data

    Copyright 2010 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 per-mission of the publisher. Printed 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 Department, 501 Boylston Street, Suite 900, Boston, MA 02116, fax your request to 617-671-3447, or e-mail at http://www.pearsoned.com/legal/permissions.htm.

    ISBN 13: 978-0-136-02253-4ISBN 10: 0-136-02253-7

    1 2 3 4 5 6 7 8 9 10EB12 11 10 09

    http://www.pearsonhighered.com/cshttp://www.pearsoned.com/legal/permissions.htmwww.pearsonhighered.com

  • v

    Contents at a Glance

    Preface xiii

    CHAPTER 1 Introduction to Computers and Programming 1

    CHAPTER 2 Introduction to C++ 27

    CHAPTER 3 Expressions and Interactivity 79

    CHAPTER 4 Making Decisions 159

    CHAPTER 5 Looping 241

    CHAPTER 6 Functions 297

    CHAPTER 7 Arrays 373

    CHAPTER 8 Searching and Sorting Arrays 451

    CHAPTER 9 Pointers 491

    CHAPTER 10 Characters, Strings, and the string Class 541

    CHAPTER 11 Structured Data 589

    CHAPTER 12 Advanced File Operations 651

    CHAPTER 13 Introduction to Classes 705

    CHAPTER 14 More About Classes 801

    CHAPTER 15 Inheritance, Polymorphism, and Virtual Functions 871

    Appendix A: The ASCII Character Set 945

    Appendix B: Operator Precedence and Associativity 949

    Index 951

    Student CD The following appendices are on the accompanying Student CD.

    Appendix C: Introduction to Flowcharting

    Appendix D: Using UML in Class Design

    Appendix E: Namespaces

    Appendix F: Writing Managed C++ Code for the .NET Framework

    Appendix G: Passing Command Line Arguments

    Appendix H: Header File and Library Function Reference

  • vi Contents at a Glance

    Appendix I: Binary Numbers and Bitwise Operations

    Appendix J: Multi-Source File Programs

    Appendix K: Stream Member Functions for Formatting

    Appendix L: Introduction to Microsoft Visual C++ 2008 Express Edition

    Appendix M: Answers to Checkpoints

    Appendix N: Answers to Odd Numbered Review Questions

  • vii

    Contents

    Table of Location of videonotes

    Preface xiii

    CHAPTER 1 Introduction to Computers and Programming 1

    1.1 Why Program? 11.2 Computer Systems: Hardware and Software 21.3 Programs and Programming Languages 61.4 What Is a Program Made of? 121.5 Input, Processing, and Output 161.6 The Programming Process 171.7 Procedural and Object-Oriented Programming 21

    CHAPTER 2 Introduction to C++ 27

    2.1 The Parts of a C++ Program 272.2 The cout Object 312.3 The #include Directive 362.4 Variables and Literals 372.5 Identifiers 412.6 Integer Data Types 422.7 The char Data Type 472.8 Floating-Point Data Types 522.9 The bool Data Type 552.10 Determining the Size of a Data Type 562.11 Variable Assignments and Initialization 572.12 Scope 582.13 Arithmetic Operators 592.14 Comments 652.15 Focus on Software Engineering: Programming Style 672.16 If You Plan to Continue in Computer Science: Standard and Prestandard C++ 69

    CHAPTER 3 Expressions and Interactivity 79

    3.1 The cin Object 793.2 Mathematical Expressions 873.3 When You Mix Apples and Oranges: Type Conversion 96

  • viii Contents

    3.4 Overflow and Underflow 983.5 Type Casting 1003.6 Named Constants 1033.7 Multiple Assignment and Combined Assignment 1083.8 Formatting Output 1123.9 Formatted Input 1213.10 Focus on Object-Oriented Programming: More About Member Functions 1263.11 More Mathematical Library Functions 1273.12 Focus on Debugging: Hand Tracing a Program 1313.13 Focus on Problem Solving: A Case Study 1333.14 Introduction to File Input and Output 136

    CHAPTER 4 Making Decisions 159

    4.1 Relational Operators 1594.2 The if Statement 1644.3 Flags 1724.4 Expanding the if Statement 1734.5 The if/else Statement 1774.6 Nested if Statements 1804.7 The if/else if Statement 1874.8 Menus 1914.9 Logical Operators 1954.10 Checking Numeric Ranges with Logical Ope