ebook img

C++ For Dummies PDF

477 Pages·2014·3.652 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 C++ For Dummies

C++ 7th Edition by Stephen R. Davis C++ For Dummies®, 7th Edition Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2014 by John Wiley & Sons, Inc., Hoboken, New Jersey Media and software compilation copyright © 2014 by John Wiley & Sons, Inc. All rights reserved. Published simultaneously in Canada 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, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. FULFILLMENT OF EACH COUPON OFFER IS THE SOLE RESPONSIBILITY OF THE OFFEROR. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit www.wiley.com/techsupport. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley prod- ucts, visit www.wiley.com. Library of Congress Control Number: 2013958400 ISBN 978-1-118-82377-4 (pbk); ISBN 978-1-118-82382-8 (ebk); ISBN 978-1-118-82383-5 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Contents at a Glance Introduction ................................................................ 1 Part I: Getting Started with C++ Programming .............. 7 Chapter 1: Writing Your First C++ Program....................................................................9 Chapter 2: Declaring Variables Constantly...................................................................33 Chapter 3: Performing Mathematical Operations ........................................................47 Chapter 4: Performing Logical Operations ...................................................................53 Chapter 5: Controlling Program Flow ............................................................................69 Part II: Becoming a Functional C++ Programmer ......... 87 Chapter 6: Creating Functions........................................................................................89 Chapter 7: Storing Sequences in Arrays .....................................................................105 Chapter 8: Taking a First Look at C++ Pointers ..........................................................121 Chapter 9: Taking a Second Look at C++ Pointers .....................................................135 Chapter 10: The C++ Preprocessor ..............................................................................153 Part III: Introduction to Classes ................................ 167 Chapter 11: Examining Object-Oriented Programming .............................................169 Chapter 12: Adding Class to C++ ..................................................................................175 Chapter 13: Point and Stare at Objects .......................................................................191 Chapter 14: Protecting Members: Do Not Disturb .....................................................207 Chapter 15: “Why Do You Build Me Up, Just to Tear Me Down, Baby?”.................215 Chapter 16: Making Constructive Arguments ............................................................225 Chapter 17: The Copy/Move Constructor ..................................................................247 Chapter 18: Static Members: Can Fabric Softener Help? ..........................................261 Part IV: Inheritance ................................................. 271 Chapter 19: Inheriting a Class ......................................................................................273 Chapter 20: Examining Virtual Member Functions: Are They for Real? .................281 Chapter 21: Factoring Classes ......................................................................................291 Part V: Security ....................................................... 301 Chapter 22: A New Assignment Operator, Should You Decide to Accept It ..........303 Chapter 23: Using Stream I/O .......................................................................................315 Chapter 24: Handling Errors — Exceptions ................................................................337 Chapter 25: Inheriting Multiple Inheritance ...............................................................347 Chapter 26: Tempting C++ Templates .........................................................................359 Chapter 27: Standardizing on the Standard Template Library ................................369 Chapter 28: Writing Hacker-Proof Code ......................................................................381 Part VI: The Part of Tens .......................................... 407 Chapter 29: Ten Ways to Avoid Adding Bugs to Your Program ..............................409 Chapter 30: Ten Ways to Protect Your Programs from Hackers .............................417 Index ....................................................................... 431 Table of Contents Introduction ................................................................. 1 About This Book ..............................................................................................1 Icons Used in This Book .................................................................................4 Beyond the Book .............................................................................................4 Where to Go from Here ...................................................................................5 Part I: Getting Started with C++ Programming ............... 7 Chapter 1: Writing Your First C++ Program . . . . . . . . . . . . . . . . . . . . . . . 9 Grasping C++ Concepts ...................................................................................9 Installing Code::Blocks ..................................................................................11 Windows................................................................................................11 Ubuntu Linux ........................................................................................13 Macintosh .............................................................................................15 Creating Your First C++ Program .................................................................19 Creating a project ................................................................................19 Entering the C++ code .........................................................................21 Cheating ................................................................................................23 Building your program ........................................................................24 Executing Your Program ...............................................................................25 Reviewing the Annotated Program .............................................................26 Examining the framework for all C++ programs ..............................27 Clarifying source code with comments ............................................27 Basing programs on C++ statements .................................................28 Writing declarations ............................................................................29 Generating output ................................................................................30 Calculating Expressions ................................................................................30 Storing the results of an expression ..................................................30 Examining the remainder of Conversion ..........................................31 Chapter 2: Declaring Variables Constantly . . . . . . . . . . . . . . . . . . . . . . 33 Declaring Variables .......................................................................................33 Declaring Different Types of Variables .......................................................34 Reviewing the limitations of integers in C++ ....................................35 Solving the truncation problem .........................................................36 Looking at the limits of floating point numbers ...............................37 vi C++ For Dummies, 7th Edition Declaring Variable Types .............................................................................38 Types of constants ..............................................................................40 Range of Numeric Types .....................................................................41 Special characters ...............................................................................42 Wide Loads on Char Highway ......................................................................43 Are These Calculations Really Logical? ......................................................44 Mixed Mode Expressions ..............................................................................44 Automatic Declarations ................................................................................46 Chapter 3: Performing Mathematical Operations . . . . . . . . . . . . . . . . . 47 Performing Simple Binary Arithmetic .........................................................47 Decomposing Expressions ...........................................................................48 Determining the Order of Operations .........................................................49 Performing Unary Operations ......................................................................50 Using Assignment Operators .......................................................................51 Chapter 4: Performing Logical Operations . . . . . . . . . . . . . . . . . . . . . . . 53 Why Mess with Logical Operations? ...........................................................53 Using the Simple Logical Operators ............................................................54 Storing logical values ..........................................................................55 Using logical int variables ...................................................................57 Be careful performing logical operations  on floating-point variables ..............................................................57 Expressing Binary Numbers .........................................................................59 The decimal number system ..............................................................59 Other number systems........................................................................60 The binary number system ................................................................60 Performing Bitwise Logical Operations ......................................................62 The single-bit operators ......................................................................63 Using the bitwise operators ...............................................................64 A simple test .........................................................................................65 Chapter 5: Controlling Program Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Controlling Program Flow with the Branch Commands ...........................69 Executing Loops in a Program .....................................................................71 Looping while a condition is true ......................................................72 Using the autoincrement/autodecrement feature ...........................74 Using the for loop ................................................................................75 Avoiding the dreaded infinite loop ....................................................78 For each his own ..................................................................................79 Applying special loop controls ..........................................................80 Nesting Control Commands .........................................................................82 Switching to a Different Subject? .................................................................84 vii Table of Contents Part II: Becoming a Functional C++ Programmer .......... 87 Chapter 6: Creating Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Writing and Using a Function .......................................................................89 Defining our first function ...................................................................92 Defining the sumSequence( ) function ..............................................92 Calling the function sumSequence( ) .................................................92 Divide and conquer .............................................................................93 Understanding the Details of Functions .....................................................93 Understanding simple functions ........................................................94 Understanding functions with arguments ........................................94 Overloading Function Names .......................................................................98 Defining Function Prototypes ......................................................................99 Defaulting Arguments .................................................................................101 Passing by Value and Passing by Reference ............................................102 Variable Storage Types ...............................................................................104 Chapter 7: Storing Sequences in Arrays . . . . . . . . . . . . . . . . . . . . . . . . 105 Arraying the Arguments for Arrays ...........................................................105 Using an array ....................................................................................107 Initializing an array ............................................................................110 Accessing too far into an array ........................................................110 Arraying range-based for loops........................................................111 Defining and using arrays of arrays .................................................112 Using Arrays of Characters ........................................................................112 Creating an array of characters .......................................................112 Creating a string of characters .........................................................114 Manipulating Strings with Character ........................................................115 Adding Some Library Functions ................................................................117 Making Room for Wide Strings ..................................................................118 Chapter 8: Taking a First Look at C++ Pointers . . . . . . . . . . . . . . . . . . 121 Variable Size .................................................................................................121 What’s in an Address? .................................................................................122 Address Operators ......................................................................................123 Using Pointer Variables ..............................................................................125 Using different types of pointers .....................................................126 Passing Pointers to Functions ....................................................................126 Passing by value .................................................................................127 Passing pointer values ......................................................................127 Passing by reference .........................................................................128 Constant const Irritation ............................................................................129 viii C++ For Dummies, 7th Edition Making Use of a Block of Memory Called the Heap .................................130 Limited scope .....................................................................................131 Examining the scope problem ..........................................................132 Providing a solution using the heap ................................................133 Chapter 9: Taking a Second Look at C++ Pointers . . . . . . . . . . . . . . . 135 Defining Operations on Pointer Variables ................................................135 Reexamining arrays in light of pointer variables ...........................136 Applying operators to the address of an array ..............................138 Expanding pointer operations to a string .......................................139 Justifying pointer-based string manipulation ................................141 Applying operators to pointer types other than char ..................142 Contrasting a pointer with an array ................................................142 When Is a Pointer Not? ................................................................................144 Declaring and Using Arrays of Pointers ....................................................145 Utilizing arrays of character strings ................................................146 Accessing the arguments to main( ) ................................................148 Chapter 10: The C++ Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 What Is a Preprocessor? .............................................................................153 Including Files ..............................................................................................154 #Defining Things ..........................................................................................157 Okay, how about not #defining things?...........................................160 Enumerating other options...............................................................161 Including Things #if I Say So ......................................................................162 Intrinsically Defined Objects ......................................................................164 Typedef .........................................................................................................166 Part III: Introduction to Classes ................................. 167 Chapter 11: Examining Object-Oriented Programming . . . . . . . . . . . 169 Abstracting Microwave Ovens ...................................................................169 Preparing functional nachos ............................................................170 Preparing object-oriented nachos ...................................................171 Classifying Microwave Ovens ....................................................................171 Why Classify? ...............................................................................................172 Chapter 12: Adding Class to C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Introducing the Class ..................................................................................175 The Format of a Class .................................................................................176 Accessing the Members of a Class ............................................................177 Activating Our Objects ...............................................................................177 Simulating real-world objects ...........................................................178 Why bother with member functions? ..............................................178

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.