ebook img

C++ For Dummies (7th Edition). PDF

419 Pages·2016·3.51 MB·English
by  
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 (7th Edition).

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 products, 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 C++ For Dummies Visit www.dummies.com/cheatsheet/cplusplus to view this book's cheat sheet. Table of Contents Introduction About This Book Icons Used in This Book Beyond the Book Where to Go from Here Part I: Getting Started with C++ Programming Chapter 1: Writing Your First C++ Program Grasping C++ Concepts Installing Code::Blocks Windows Ubuntu Linux Macintosh Creating Your First C++ Program Creating a project Entering the C++ code Cheating Building your program Executing Your Program Reviewing the Annotated Program Examining the framework for all C++ programs Clarifying source code with comments Basing programs on C++ statements Writing declarations Generating output Calculating Expressions Storing the results of an expression Examining the remainder of Conversion Chapter 2: Declaring Variables Constantly Declaring Variables Declaring Different Types of Variables Reviewing the limitations of integers in C++ Solving the truncation problem Looking at the limits of floating point numbers Declaring Variable Types Types of constants Range of Numeric Types Special characters Wide Loads on Char Highway Are These Calculations Really Logical? Mixed Mode Expressions Automatic Declarations Chapter 3: Performing Mathematical Operations Performing Simple Binary Arithmetic Decomposing Expressions Determining the Order of Operations Performing Unary Operations Using Assignment Operators Chapter 4: Performing Logical Operations Why Mess with Logical Operations? Using the Simple Logical Operators Storing logical values Using logical int variables Be careful performing logical operations on floating-point variables Expressing Binary Numbers The decimal number system Other number systems The binary number system Performing Bitwise Logical Operations The single-bit operators Using the bitwise operators A simple test Chapter 5: Controlling Program Flow Controlling Program Flow with the Branch Commands Executing Loops in a Program Looping while a condition is true Using the autoincrement/autodecrement feature Using the for loop Avoiding the dreaded infinite loop For each his own Applying special loop controls Nesting Control Commands Switching to a Different Subject? Part II: Becoming a Functional C++Programmer Chapter 6: Creating Functions Writing and Using a Function Defining our first function Defining the sumSequence() function Calling the function sumSequence() Divide and conquer Understanding the Details of Functions Understanding simple functions Understanding functions with arguments Overloading Function Names Defining Function Prototypes Defaulting Arguments Passing by Value and Passing by Reference Variable Storage Types Chapter 7: Storing Sequences in Arrays Arraying the Arguments for Arrays Using an array Initializing an array Accessing too far into an array Arraying range-based for loops Defining and using arrays of arrays Using Arrays of Characters Creating an array of characters Creating a string of characters Manipulating Strings with Character Adding Some Library Functions Making Room for Wide Strings Chapter 8: Taking a First Look at C++ Pointers Variable Size What’s in an Address? Address Operators Using Pointer Variables Using different types of pointers Passing Pointers to Functions Passing by value Passing pointer values Passing by reference Constant const Irritation Making Use of a Block of Memory Called the Heap Limited scope Examining the scope problem Providing a solution using the heap Chapter 9: Taking a Second Look at C++ Pointers Defining Operations on Pointer Variables Reexamining arrays in light of pointer variables Applying operators to the address of an array Expanding pointer operations to a string Justifying pointer-based string manipulation Applying operators to pointer types other than char Contrasting a pointer with an array When Is a Pointer Not? Declaring and Using Arrays of Pointers Utilizing arrays of character strings Accessing the arguments to main() Chapter 10: The C++ Preprocessor What Is a Preprocessor? Including Files #Defining Things Okay, how about not #defining things? Enumerating other options Including Things #if I Say So Intrinsically Defined Objects Typedef Part III: Introduction to Classes Chapter 11: Examining Object-Oriented Programming Abstracting Microwave Ovens Preparing functional nachos Preparing object-oriented nachos Classifying Microwave Ovens Why Classify? Chapter 12: Adding Class to C++ Introducing the Class The Format of a Class Accessing the Members of a Class Activating Our Objects Simulating real-world objects Why bother with member functions? Adding a Member Function Calling a Member Function Accessing other members from a member function Scope Resolution (And I Don’t Mean How Well Your Telescope Works) Defining a Member Function in the Class Keeping a Member Function after Class Overloading Member Functions Chapter 13: Point and Stare at Objects Declaring Arrays of Objects Declaring Pointers to Objects Dereferencing an object pointer Pointing toward arrow pointers Passing Objects to Functions Calling a function with an object value Calling a function with an object pointer Calling a function by using the reference operator Why Bother with Pointers or References? Returning to the Heap Allocating heaps of objects When memory is allocated for you Linking Up with Linked Lists Performing other operations on a linked list Hooking up with a LinkedListData program Ray of Hope: A List of Containers Linked to the C++ Library Chapter 14: Protecting Members: Do Not Disturb Protecting Members Why you need protected members Discovering how protected members work Making an Argument for Using Protected Members Protecting the internal state of the class Using a class with a limited interface Giving Non-member Functions Access to Protected Members Chapter 15: “Why Do You Build Me Up, Just toTear Me Down, Baby?” Creating Objects Using Constructors Constructing a single object Constructing multiple objects Constructing a duplex Dissecting a Destructor Why you need the destructor Working with destructors Chapter 16: Making Constructive Arguments Outfitting Constructors with Arguments Using a constructor Placing Too Many Demands on the Carpenter: Overloading the Constructor Defaulting Default Constructors Constructing Class Members Constructing a complex data member Constructing a constant data member Reconstructing the Order of Construction Local objects construct in order Static objects construct only once All global objects construct before main() Global objects construct in no particular order Members construct in the order in which they are declared Destructors destruct in the reverse order of the constructors Constructing Arrays Constructors as a Form of Conversion Chapter 17: The Copy/Move Constructor Copying an Object Why you need the copy constructor Using the copy constructor The Automatic Copy Constructor Creating Shallow Copies versus Deep Copies It’s a Long Way to Temporaries Avoiding temporaries, permanently The move constructor Chapter 18: Static Members: Can Fabric Softener Help? Defining a Static Member Why you need static members Using static members Referencing static data members Uses for static data members Declaring Static Member Functions What Is this About Anyway? Part IV: Inheritance Chapter 19: Inheriting a Class Do I Need My Inheritance? How Does a Class Inherit? Using a subclass Constructing a subclass Destructing a subclass Inheriting constructors Having a HAS_A Relationship Chapter 20: Examining Virtual Member Functions: Are They for Real? Why You Need Polymorphism How Polymorphism Works When Is a Virtual Function Not? Considering Virtual Considerations Chapter 21: Factoring Classes Factoring Implementing Abstract Classes Describing the abstract class concept Making an honest class out of an abstract class Passing abstract classes Part V: Security Chapter 22: A New Assignment Operator, Should You Decide to Accept It Comparing Operators with Functions Comparing Operators with Functions Inserting a New Operator Creating Shallow Copies Is a Deep Problem Overloading the Assignment Operator Overloading the Subscript Operator The Move Constructor and Move Operator Chapter 23: Using Stream I/O How Stream I/O Works Default stream objects Stream Input/Output Open modes Hey, file, what state are you in? Can you show me an example? Other Methods of the Stream Classes Reading and writing streams directly Controlling format What's up with endl? Positioning the pointer within a file Using the stringstream Subclasses Manipulating Manipulators Chapter 24: Handling Errors — Exceptions Justifying a New Error Mechanism? Examining the Exception Mechanism What Kinds of Things Can I Throw? Just Passing Through Chapter 25: Inheriting Multiple Inheritance Describing the Multiple Inheritance Mechanism Straightening Out Inheritance Ambiguities Adding Virtual Inheritance Constructing the Objects of Multiple Inheritance Voicing a Contrary Opinion Chapter 26: Tempting C++ Templates Generalizing a Function into a Template Class Templates Tips for Using Templates External Template Instantiations Implementing an Initializer List Chapter 27: Standardizing on the Standard Template Library The string Container Iterating through Lists Making your way through a list Operations on an entire list Can you show me an example? Chapter 28: Writing Hacker-Proof Code Understanding the Hacker's Motives Understanding Code Injection Examining an example SQL injection Avoiding code injection Overflowing Buffers for Fun and Profit Can I see an example?

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.