Always Learning

Advanced Search

Sams Teach Yourself C++ in 24 Hours

Sams Teach Yourself C++ in 24 Hours

5th Edition

Jesse Liberty, Rogers Cadenhead

May 2011, Paperback with CD-ROM, 464 pages
ISBN13: 9780672333316
ISBN10: 0672333317
 
This title is available in the following formats:
Format RRPYour Price
Paperback with CD-ROM £22.99 £22.99
CourseSmart eTextbook £13.99 £13.99
  • Print pagePrint page
  • Email this pageEmail page
  • Write a reviewWrite a review
  • Share

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.

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

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

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.

Your opinions count

Be the first to review this product. Write your review now.