did-you-know? rent-now

Amazon no longer offers textbook rentals. We do!

did-you-know? rent-now

Amazon no longer offers textbook rentals. We do!

We're the #1 textbook rental company. Let us show you why.

9780672333316

Sams Teach Yourself C++ in 24 Hours

by ;
  • ISBN13:

    9780672333316

  • ISBN10:

    0672333317

  • Edition: 5th
  • Format: Paperback
  • Copyright: 2011-04-14
  • Publisher: Sams Publishing
  • View Upgraded Edition

Note: Supplemental materials are not guaranteed with Rental or Used book purchases.

Purchase Benefits

  • Free Shipping Icon Free Shipping On Orders Over $35!
    Your order must be $35 or more to qualify for free economy shipping. Bulk sales, PO's, Marketplace items, eBooks and apparel do not qualify for this offer.
  • eCampus.com Logo Get Rewarded for Ordering Your Textbooks! Enroll Now
List Price: $34.99 Save up to $8.75
  • Buy Used
    $26.24

    USUALLY SHIPS IN 2-4 BUSINESS DAYS

Supplemental Materials

What is included with this book?

Summary

The best-selling C++ tutorial - updated for the latest standards, and enhanced to be even more engaging for beginners Teaches C++ as a first programming language, through short, simple chapters that can be completed in no more than an hour each Includes well-crafted, up-to-date, and concise examples, and provides a C++ compiler to test them Contains new and revised material that reflects the forthcoming new C++ language standard Sams Teach Yourself C++ in 24 Hours, 5/e, will help C++ beginners gain true mastery over the latest version of the C++ standard. In this book's straightforward, step-by-step approach, each lesson builds on everything that's come before, helping readers learn C++'s core features and techniques from the ground up. Friendly, accessible, and conversational, this book offers a practical grounding in the language, without ever becoming overwhelming or intimidating. Readers master key concepts, syntax, and techniques through short, practical examples that reflect today's best practices and cross-platform language standards. Coverage includes: organizing C++ programs; using variables, constants, expressions, statements, operators, and functions; controlling program flow; working with arrays and strings; creating basic and advanced classes; managing memory; using operator overloading; working with inheritance, polymorphism, and linked lists; reading and writing IO; employing object-oriented analysis and design; creating templates; handling exceptions and errors; and much more. By the time readers are finished with this book's lessons, they won't just understand C++: they'll be comfortable using it in real-world projects.

Author Biography

Jesse Liberty is the author of numerous books on software development, including best-selling titles on C++ and .NET. He is the president of Liberty Associates, Inc. (http://www.libertyassociates. com), where he provides custom programming, consulting, and training.

 

Rogers Cadenhead is a writer, computer programmer, and web developer who has written 23 books on Internet-related topics, including Sams Teach Yourself Java in 21 Days and Sams Teach Yourself Java in 24 Hours. He publishes the Drudge Retort and other websites that receive more than 22 million visits a year. This book’s official website is at http://cplusplus.cadenhead.org.

Table of Contents

Introduction                                                                              1

Part I: Beginning C++

HOUR 1: Writing Your First Program                                                   5

    Using C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    Finding a Compiler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . 6

    Compiling and Linking the Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

    Creating Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

HOUR 2: Organizing the Parts of a Program 15

    Reasons to Use C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . 15

    The Parts of a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

    Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . 22

    Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

HOUR 3: Creating Variables and Constants 29

    What Is a Variable?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

    Defining a Variable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 33

    Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

    Using Type Definitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . 36

    Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

HOUR 4: Using Expressions, Statements, and Operators                                 43

    Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . 43

    Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . 44

    Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

    If-Else Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

    Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

    Tricky Expression Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

HOUR 5: Calling Functions 63

    What Is a Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . 63

    Declaring and Defining Functions . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . 64

    Using Variables with Functions . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . 66

    Function Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . 69

    Returning Values from Functions. . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . 70

    Default Function Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

    Overloading Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

HOUR 6: Controlling the Flow of a Program 81

    Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

    while Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . 81

    do-while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

    for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . 86

    switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

HOUR 7: Storing Information in Arrays and Strings 97

    What Is an Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 97

    Writing Past the End of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

    Initializing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

    Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

    Character Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . 104

    Copying Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

Part II: Classes

HOUR 8: Creating Basic Classes 111

    What Is a Type? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . 111

    Creating New Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

    Classes and Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

    Accessing Class Members . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . 114

    Private Versus Public Access . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . 115

    Implementing Member Functions . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . 116

    Creating and Deleting Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

HOUR 9: Moving into Advanced Classes 125

    const Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . 125

    Interface Versus Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

    Organizing Class Declarations and Function Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . 126

    Inline Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . 127

    Classes with Other Classes as Member Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

Part III: Memory Management

HOUR 10: Creating Pointers 137

    Understanding Pointers and Their Usage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

    The Stack and the Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

HOUR 11: Developing Advanced Pointers 155

    Creating Objects on the Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

    Deleting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . 155

    Accessing Data Members Using Pointers . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . 157

    Member Data on the Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . 158

    The this Pointer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . 160

    Stray or Dangling Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . 161

    const Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . 162

    const Pointers and const Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

HOUR 12: Creating References 169

    What Is a Reference? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

    Creating a Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

    Using the Address of Operator on References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

    What Can Be Referenced?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . 173

    Null Pointers and Null References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

    Passing Function Arguments by Reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

    Understanding Function Headers and Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

    Returning Multiple Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

HOUR 13: Developing Advanced References and Pointers 185

    Passing by Reference for Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

    Passing a const Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

    References as an Alternative to Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

    When to Use References and When to Use Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

    Don’t Return a Reference to an Object That Isn’t in Scope!. . . . . . . . . . . . . . . . . . . . . . . . 193

    Returning a Reference to an Object on the Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

    Pointer, Pointer, Who Has the Pointer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

Part IV: Advanced C++

HOUR 14: Calling Advanced Functions 201

    Overloaded Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

    Using Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

    Initializing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

    The Copy Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . 206

HOUR 15: Using Operator Overloading 215

    Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . 215

    Conversion Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 225

Part V: Inheritance and Polymorphism

HOUR 16: Extending Classes with Inheritance 233

    What Is Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . 233

    Private Versus Protected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

    Constructors and Destructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . 238

    Passing Arguments to Base Constructors . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . 241

    Overriding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

HOUR 17: Using Polymorphism and Derived Classes 253

    Polymorphism Implemented with Virtual Methods . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . 253

    How Virtual Member Functions Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257

HOUR 18: Making Use of Advanced Polymorphism 269

    Problems with Single Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . 269

    Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . 273

HOUR 19: Storing Information in Linked Lists 289

    Linked Lists and Other Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . 289

    Linked List Case Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

    Linked Lists as Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . 299

Part VI: Special Topics

HOUR 20: Using Special Classes, Functions, and Pointers 303

    Static Member Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 303

    Static Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305

    Containment of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307

    Friend Classes and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . 313

HOUR 21: Using New Features of C++0x 331

    The Next Version of C++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331

    Null Pointer Constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332

    Compile-Time Constant Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . 333

    Auto-Typed Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . 335

    New for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338

HOUR 22: Employing Object-Oriented Analysis and Design 343

    The Development Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343

    Simulating an Alarm System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344

    PostMaster: A Case Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

HOUR 23: Creating Templates 373

    What Are Templates?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . 373

    Instances of the Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374

    Template Definition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . 374

    Using Template Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

HOUR 24: Dealing with Exceptions and Error Handling 389

    Bugs, Errors, Mistakes, and Code Rot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389

    Handling the Unexpected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . 390

    Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . 391

    Using try and catch Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

    Writing Professional-Quality Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400

Part VII: Appendices

APPENDIX A: Binary and Hexadecimal 409

APPENDIX B: Glossary 419

APPENDIX C: This Book’s Website 427

TOC, 9780672333316, 3/21/11

 

Supplemental Materials

What is included with this book?

The New copy of this book will include any supplemental materials advertised. Please check the title of the book to determine if it should include any access cards, study guides, lab manuals, CDs, etc.

The Used, Rental and eBook copies of this book are not guaranteed to include any supplemental materials. Typically, only the book itself is included. This is true even if the title states it includes any access cards, study guides, lab manuals, CDs, etc.

Rewards Program