Sams Teach Yourself C++ in 24 Hours

( 2 )

Overview

Starter Kit

Includes C++ compiler and IDE for Windows, Mac & Linux

In just 24 lessons of one hour or less, you can learn the basics of programming with C++–one of the most popular and powerful programming languages ever created.

Using a straightforward, step-by-step approach, this fast and friendly tutorial teaches you everything you need to know, from installing and ...

See more details below
Paperback
$25.64
BN.com price
(Save 26%)$34.99 List Price

Pick Up In Store

Reserve and pick up in 60 minutes at your local store

Other sellers (Paperback)
  • All (15) from $13.65   
  • New (8) from $19.29   
  • Used (7) from $13.65   
Sams Teach Yourself C++ in 24 Hours

Available on NOOK devices and apps  
  • NOOK Devices
  • Samsung Galaxy Tab 4 NOOK
  • NOOK HD/HD+ Tablet
  • NOOK
  • NOOK Color
  • NOOK Tablet
  • Tablet/Phone
  • NOOK for Windows 8 Tablet
  • NOOK for iOS
  • NOOK for Android
  • NOOK Kids for iPad
  • PC/Mac
  • NOOK for Windows 8
  • NOOK for PC
  • NOOK for Mac

Want a NOOK? Explore Now

NOOK Book (eBook)
$15.49
BN.com price
(Save 44%)$27.99 List Price

Overview

Starter Kit

Includes C++ compiler and IDE for Windows, Mac & Linux

In just 24 lessons of one hour or less, you can learn the basics of programming with C++–one of the most popular and powerful programming languages ever created.

Using a straightforward, step-by-step approach, this fast and friendly tutorial teaches you everything you need to know, from installing and using a compiler, to debugging the programs you’ve created, to what’s coming in C++0x, the next version of C++.

Each lesson builds on what you’ve already learned, giving you a solid understanding of the basics of C++ programming concepts and techniques.

Step-by-step instructions carefully walk you through the most common C++ programming tasks

Quizzes and Exercises at the end of each chapter help you test yourself to make sure you’re ready to go on

Starter Kit software provides everything you need to create and compile C++ programs on any platform–Windows, Mac or Linux

Learn how to…

  • Install and use a C++ compiler for Windows, Mac OS X or Linux
  • Build object-oriented programs in C++
  • Master core C++ concepts such as functions, classes, arrays, and pointers
  • Add rich functionality with linked lists and templates
  • Debug your programs for flawless code
  • Learn exception and error-handling techniques
  • Discover what’s new in C++0x, the next version of C++

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. where he provides custom programming, consulting, and training.

Rogers Cadenhead is a web application developer who has written many books on Internet-related topics, including Teach Yourself Java in 24 Hours. He maintains this book’s official website at http://cplusplus.cadenhead.org.

CD-ROM Includes

C++ compiler

Visual development environment for Windows, Mac and Linux

Source code for the book’s examples

Register your book at informit.com/register for convenient access to updates and corrections as they become available.

Read More Show Less

Product Details

  • ISBN-13: 9780672333316
  • Publisher: Sams
  • Publication date: 4/28/2011
  • Series: Sams Teach Yourself Series
  • Edition number: 5
  • Pages: 464
  • Sales rank: 306,616
  • Product dimensions: 6.90 (w) x 9.00 (h) x 1.10 (d)

Meet the Author

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.

Read More Show Less

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

Read More Show Less

Customer Reviews

Average Rating 4.5
( 2 )
Rating Distribution

5 Star

(1)

4 Star

(1)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing all of 2 Customer Reviews
  • Anonymous

    Posted June 18, 2012

    No text was provided for this review.

  • Anonymous

    Posted July 11, 2013

    No text was provided for this review.

Sort by: Showing all of 2 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)