The Rook's Guide to C++ · PDF fileandLeviSchuck,who,ondifferentsidesoftheplanet,managedto contributeextensivelytotheheavyli ingofge ingthebookinto

  • Upload
    hadang

  • View
    219

  • Download
    1

Embed Size (px)

Citation preview

  • e Rooks Guide to C++

    26 November 2013

  • 2013 Jeremy A. HansenAll rights reserved.

    iswork is licensed under a Creative CommonsAribution-NonCommercial-ShareAlike 3.0 Unported License, as described at

    http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode

    Printed in the United States of America

    First edition: November 2013

    ISBN 978-1-304-66105-0

    Rooks Guide Press19 Black RoadBerlin, VT 05602http://rooksguide.org

    http://creativecommons.org/licenses/by-nc-sa/3.0/legalcodehttp://rooksguide.org

  • Preface

    What you are reading is the first of what I hope to be many ever-improving iterations of a useful C++ textbook. Weve gone fairlyquickly from whim to print on an all-volunteer basis, and as a result,there are many things that Id add and change if I had an infiniteamount of time in my schedule. e vast majority of the contentswere wrien in less than 36 hours by 25 students (mostly freshmen!)at Norwich University over a long weekend. Some of it is mine, andsome was added by our crack team of technical editors as we trans-lated sleep-deprived poor grammar into sleep-deprived beer gram-mar.

    Where it goes from here is mostly up to you! If theres a sectionthats missing or in need of clarification, please take a bit of timeand make those changes. If you dont want to bother yourself withthe GitHub repository, send me your additions and modifications di-rectly.

    I want to first thank my family for the time I didnt spend withthem on the writing weekend and throughout the summer when Iwas editing and typeseing. I promise I wont do this next summer!

    My next thanks go out to the technical editors and typeseers,without whom you would have a much uglier book. anks to TedRolle for building the initial LATEXframework and to Ma Jadud forthe incredibly helpful pointers on how to manage the pile of typeset-ting files in a collaborative environment. I also thank Craig Robbins

    iii

  • and Levi Schuck, who, on different sides of the planet, managed tocontribute extensively to the heavy liing of geing the book intothe shape its in now. If we ever meet, I owe you a beer or whateveryoure having!

    I also would like to thank all of the Kickstarter backers not onlyfor the money which made this possible, but for reinforcing the ideathat this is aworthwhile contribution to the community. Peter Stephen-son and Andrew Pedley also contributed food directly over the text-bookwriting hackathonweekend, andwithout themwed never havegoen our saturated fat quota! (Note to future project leaders: theresnothing that gets a bunch of college students who are generally luke-warm about programming to write a textbook like free food. It didnteven maer what the food was. Really.)

    anks to Ma Russo for shooting the video and organizing themedia and social networking effortswith the Kickstarter project throughthe writing weekend.

    Special thanks to Allyson LeFebvre1 for the textbook photogra-phy, several diagrams, and the extensive search through the semi-final textbook that turned up a bunch of mistakes that I missed.

    And my last (and not at all least) thanks go out to all the stu-dents who showed up in person or digitally. And without geingtoo grandiose, you remind us all that we can make the world beerby showing up. Keep showing up!

    [email protected] November 2013

    1ats la-fave, everyone

  • Contents

    1 History 1

    2 Variables 32.1 How do I decide which data type I need? . . . . . . . 42.2 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . 42.3 Declaring a Variable . . . . . . . . . . . . . . . . . . . 42.4 Initializing Variables . . . . . . . . . . . . . . . . . . . 52.5 Assignment Statements . . . . . . . . . . . . . . . . . 52.6 Review estions . . . . . . . . . . . . . . . . . . . . 62.7 Review Answers . . . . . . . . . . . . . . . . . . . . . 62.8 Further Reading . . . . . . . . . . . . . . . . . . . . . 6

    3 Literals and Constants 73.1 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . 73.2 Declared Constants . . . . . . . . . . . . . . . . . . . 73.3 Review estions . . . . . . . . . . . . . . . . . . . . 83.4 Review Answers . . . . . . . . . . . . . . . . . . . . . 8

    4 Assignments 114.1 Review estions . . . . . . . . . . . . . . . . . . . . 134.2 Review Answers . . . . . . . . . . . . . . . . . . . . . 13

    v

  • 5 Output 155.1 Review estions . . . . . . . . . . . . . . . . . . . . 185.2 Review Answers . . . . . . . . . . . . . . . . . . . . . 195.3 Further Reading . . . . . . . . . . . . . . . . . . . . . 19

    6 Input 216.1 Review estions . . . . . . . . . . . . . . . . . . . . 236.2 Review Answers . . . . . . . . . . . . . . . . . . . . . 246.3 Further Reading . . . . . . . . . . . . . . . . . . . . . 24

    7 Arithmetic 257.1 Review estions . . . . . . . . . . . . . . . . . . . . 287.2 Review Answers . . . . . . . . . . . . . . . . . . . . . 307.3 Further Reading . . . . . . . . . . . . . . . . . . . . . 31

    8 Comments 338.1 Review estions . . . . . . . . . . . . . . . . . . . . 358.2 Review estions . . . . . . . . . . . . . . . . . . . . 37

    9 Data Types and Conversion 399.1 Floating-point types . . . . . . . . . . . . . . . . . . . 409.2 Other types introduced by C++11 . . . . . . . . . . . 429.3 Conversion Between Types . . . . . . . . . . . . . . . 429.4 Coercion & Casting . . . . . . . . . . . . . . . . . . . 439.5 Automatic Types in C++11 . . . . . . . . . . . . . . . 449.6 Review estions . . . . . . . . . . . . . . . . . . . . 459.7 Review Answers . . . . . . . . . . . . . . . . . . . . . 459.8 Further Reading . . . . . . . . . . . . . . . . . . . . . 45

    10 Conditionals 4710.1 if, else, and else if . . . . . . . . . . . . . . . 48

    10.1.1 A small digression on expressions . . . . . . . 4810.1.2 Using else . . . . . . . . . . . . . . . . . . . 49

    10.2 switch statements . . . . . . . . . . . . . . . . . . 51

  • 10.3 Review estions . . . . . . . . . . . . . . . . . . . . 5410.4 Review Answers . . . . . . . . . . . . . . . . . . . . . 54

    11 Strings 5511.1 Review estions . . . . . . . . . . . . . . . . . . . . 5911.2 Review Answers . . . . . . . . . . . . . . . . . . . . . 6011.3 Further Reading . . . . . . . . . . . . . . . . . . . . . 60

    12 Loops 6112.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . 6112.2 Having Fun while Programming . . . . . . . . . . . 6112.3 do-while Loops . . . . . . . . . . . . . . . . . . . . 6312.4 Event-Based Loops vs Count-Based Loops . . . . . . 6512.5 for work or for play . . . . . . . . . . . . . . . . . 6612.6 Picking a Loop . . . . . . . . . . . . . . . . . . . . . . 6712.7 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . 6812.8 Infinite Loops . . . . . . . . . . . . . . . . . . . . . . 6812.9 Review estions . . . . . . . . . . . . . . . . . . . . 6912.10 Review Answers . . . . . . . . . . . . . . . . . . . . . 7012.11 Further Reading . . . . . . . . . . . . . . . . . . . . . 70

    13 Arrays 7113.1 Multi-dimensional Arrays . . . . . . . . . . . . . . . . 7313.2 Review estions . . . . . . . . . . . . . . . . . . . . 7413.3 Review Answers . . . . . . . . . . . . . . . . . . . . . 7413.4 Further Reading . . . . . . . . . . . . . . . . . . . . . 75

    14 Blocks, Functions, and Scope 7714.1 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . 7714.2 Basic Functions in C++ . . . . . . . . . . . . . . . . . 78

    14.2.1 What are functions and why do we use them? 7814.2.2 e parts of a basic function . . . . . . . . . . 78

    14.3 void Functions . . . . . . . . . . . . . . . . . . . . . 8114.4 Overloading Function Names . . . . . . . . . . . . . . 82

  • 14.5 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . 8314.6 Review estions . . . . . . . . . . . . . . . . . . . . 8514.7 Review Answers . . . . . . . . . . . . . . . . . . . . . 8614.8 Further Reading . . . . . . . . . . . . . . . . . . . . . 88

    15 Problem Solving & Troubleshooting 8915.1 e Compilation Error . . . . . . . . . . . . . . . . . 8915.2 e Logic Error . . . . . . . . . . . . . . . . . . . . . 9015.3 e Infinite Loop . . . . . . . . . . . . . . . . . . . . 9015.4 Review estions . . . . . . . . . . . . . . . . . . . . 9115.5 Review Answers . . . . . . . . . . . . . . . . . . . . . 92

    16 e Preprocessor 9316.1 Review estions . . . . . . . . . . . . . . . . . . . . 9416.2 Review Answers . . . . . . . . . . . . . . . . . . . . . 95

    17 Advanced Arithmetic 9717.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . 98

    17.1.1 pow() . . . . . . . . . . . . . . . . . . . . . 9817.1.2 sqrt() . . . . . . . . . . . . . . . . . . . . 9917.1.3 Modulo . . . . . . . . . . . . . . . . . . . . . 100

    17.2 Review estions . . . . . . . . . . . . . . . . . . . . 10117.3 Review Answers . . . . . . . . . . . . . . . . . . . . . 10117.4 Further Reading . . . . . . . . . . . . . . . . . . . . . 102

    18 File I/O 10318.1 I/O Streams . . . . . . . . . . . . . . . . . . . . . . . . 10318.2 File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . 10418.3 Opening and closing a File . . . . . . . . . . . . . . . 10418.4 Reading from a File . . . . . . . . . . . . . . . . . . . 10518.5 Writing data to a File . . . . . . . . . . . . . . . . . . 10618.6 Introduction to Classes and Objects . . . . . . . . . . 10718.7 Other functions . . . . . . . . . . . . . . . . . . . . . 10818.8 Review estions . . . . . . . . . . . . . . . . . . . . 109

  • 18.9 Review Answers . . . . . . . . . . . . . . . . . . . . . 11018.10 Further Reading . . . . . . . . . . . . . . . . . . . . . 110

    19 Pointers 11119.1 Review estions . . . . . . . . . . . . . . . . . . . . 11419.2 Review Answers . . . . . . . . . . . . . . . . . . . . . 115

    20 Dynamic Data 11720.1 Review estions . . . . . . . . . . . . .