c++ teach yourself

Download C++ teach yourself

Post on 15-Jul-2015

413 views

Category:

Documents

12 download

Embed Size (px)

TRANSCRIPT

  • ptg7987094

    www.it-ebooks.info

  • ptg7987094

    in One Hour a Day

    C++SamsTeachYourself

    Siddhartha Rao

    800 East 96th Street, Indianapolis, Indiana 46240

    Seventh Edition

    www.it-ebooks.info

  • ptg7987094

    Sams Teach Yourself C++ in One Hour a Day,Seventh EditionCopyright 2012 by Pearson Education, Inc.

    All rights reserved. No part of this book shall be reproduced, stored in a retrieval system,or transmitted by any means, electronic, mechanical, photocopying, recording, or other-wise, without written permission from the publisher. No patent liability is assumed withrespect to the use of the information contained herein. Although every precaution has beentaken in the preparation of this book, the publisher and author assume no responsibility forerrors or omissions. Nor is any liability assumed for damages resulting from the use of theinformation contained herein.

    ISBN-13: 978-0-672-33567-9ISBN-10: 0-672-33567-0

    The Library of Congress Cataloging-in-Publication Data is on file.

    Printed in the United States of America

    First Printing May 2012

    TrademarksAll terms mentioned in this book that are known to be trademarks or service marks havebeen appropriately capitalized. Sams Publishing cannot attest to the accuracy of this infor-mation. Use of a term in this book should not be regarded as affecting the validity of anytrademark or service mark.

    Warning and DisclaimerEvery effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an as is basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.

    Bulk SalesSams Publishing offers excellent discounts on this book when ordered in quantity for bulkpurchases or special sales. For more information, please contact

    U.S. Corporate and Government Sales1-800-382-3419corpsales@pearsontechgroup.com

    For sales outside of the U.S., please contact

    International Salesinternational@pearsoned.com

    Acquisitions EditorMark Taber

    Development EditorSonglin Qiu

    Managing EditorSandra Schroeder

    Project EditorMandie Frank

    Copy EditorCharlotte Kughen

    IndexerTim Wright

    ProofreaderMegan Wade

    Technical EditorJon Upchurch

    PublishingCoordinatorVanessa Evans

    DesignerGary Adair

    CompositorStudio Galou, LLC

    www.it-ebooks.info

  • ptg7987094

    Contents at a GlanceIntroduction 1

    PART I: The Basics1 Getting Started 5

    2 The Anatomy of a C++ Program 15

    3 Using Variables, Declaring Constants 29

    4 Managing Arrays and Strings 57

    5 Working with Expressions, Statements, and Operators 77

    6 Controlling Program Flow 105

    7 Organizing Code with Functions 141

    8 Pointers and References Explained 165

    PART II: Fundamentals of Object-Oriented C++ Programming9 Classes and Objects 203

    10 Implementing Inheritance 251

    11 Polymorphism 283

    12 Operator Types and Operator Overloading 311

    13 Casting Operators 353

    14 An Introduction to Macros and Templates 367

    PART III: Learning the Standard Template Library (STL)15 An Introduction to the Standard Template Library 393

    16 The STL String Class 405

    17 STL Dynamic Array Classes 423

    18 STL list and forward_list 445

    19 STL Set Classes 467

    20 STL Map Classes 487

    PART IV: More STL21 Understanding Function Objects 511

    22 C++11 Lambda Expressions 527

    23 STL Algorithms 543

    24 Adaptive Containers: Stack and Queue 579

    25 Working with Bit Flags Using STL 597

    PART V: Advanced C++ Concepts26 Understanding Smart Pointers 607

    27 Using Streams for Input and Output 621

    www.it-ebooks.info

  • ptg7987094

    28 Exception Handling 643

    29 Going Forward 659

    AppendixesA Working with Numbers: Binary and Hexadecimal 671

    B C++ Keywords 677

    C Operator Precedence 679

    D Answers 681

    E ASCII Codes 723

    Index 727

    www.it-ebooks.info

  • ptg7987094

    Table of Contents

    Introduction 1

    PART I: The Basics

    LESSON 1: Getting Started 5

    A Brief History of C++ . ............................................................................................................................................................... 6Connection to C . ............................................................................................................................................................... 6Advantages of C++ . ........................................................................................................................................................ 6Evolution of the C++ Standard . .............................................................................................................................. 7Who Uses Programs Written in C++? . ............................................................................................................ 7

    Programming a C++ Application . ......................................................................................................................................... 7Steps to Generating an Executable . .................................................................................................................... 8Analyzing Errors and Firefighting. ....................................................................................................................... 8Integrated Development Environments. ............................................................................................................ 8Programming Your First C++ Application . .................................................................................................. 9Building and Executing Your First C++ Application . ........................................................................ 10Understanding Compiler Errors . ....................................................................................................................... 12

    Whats New in C++11 . ............................................................................................................................................................... 12Summary. ............................................................................................................................................................................................... 13Q&A . ....................................................................................................................................................................................................... 13Workshop . ............................................................................................................................................................................................ 14

    LESSON 2: The Anatomy of a C++ Program 15

    Part of the Hello World Program . ...................................................................................................................................... 16Preprocessor Directive #include . ....................................................................................................................... 16The Body of Your Program main() . ................................................................................................................ 17Returning a Value . ........................................................................................................................................................ 18

    The Concept of Namespaces . ................................................................................................................................................. 19Comments in C++ Code. ........................................................................................................................................................... 20Functions in C++ . .......................................................................................................................................................................... 21Basic Input Using std::cin and Output Using std::cout . ................................................................................... 24Summary. ............................................................................................................................................................................................... 26Q&A . ....................................................................................................................................................................................................... 26Workshop . ............................................................................................................................................................................................ 27

    www.it-ebooks.info

  • ptg7987094

    LESSON 3: Using Variables, Declaring Constants 29

    What Is a Variable? . ...................................................................................................................................................................... 30Memory and Addressing in Brief . .................................................................................................................... 30Declaring Variables to Access and Use Memory . ................................................................................ 30Declaring and Initializing Multiple Variables of a Type . ................................................................. 32Understanding the Scope of a Variable . ......................................................................................................... 33