You are previewing Sams Teach Yourself C++ in 24 Hours, Sixth Edition.
O'Reilly logo
Sams Teach Yourself C++ in 24 Hours, Sixth Edition

Book Description

Sams Teach Yourself C++ in 24 Hours is a hands-on guide to the C++ programming language. Readers are provided with short, practical examples that illustrate key concepts, syntax, and techniques.

Using a straightforward 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 new in C++14.

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

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 and classes

  • Add rich functionality with templates and lambda expressions

  • Debug your programs for flawless code

  • Learn exception and error-handling techniques

  • Put to use the new features in C++14, the latest version of the language

  • Create and use templates

  • Control program flow with loops

  • Store information in arrays and strings

  • Declare and use pointers

  • Use operator overloading

  • Extend classes with inheritance

  • Use polymorphism and derived classes

  • Employ object-oriented analysis and design

  • Table of Contents

    1. About This E-Book
    2. Title Page
    3. Copyright Page
    4. Contents at a Glance
    5. Table of Contents
    6. About the Authors
    7. We Want to Hear from You!
    8. Reader Services
    9. Introduction
      1. Who Should Read This Book?
      2. Should I Learn C First?
      3. Why Should I Learn C++?
      4. What If I Don’t Want This Book?
      5. Conventions Used in This Book
    10. Part I: Beginning C++
      1. Hour 1. Writing Your First Program
        1. Using C++
        2. Compiling and Linking the Source Code
        3. Creating Your First Program
        4. Summary
        5. Q&A
        6. Workshop
          1. Quiz
          2. Answers
          3. Activities
      2. Hour 2. Organizing the Parts of a Program
        1. Reasons to Use C++
          1. Styles of Programming
          2. C++ and Object-Oriented Programming
        2. The Parts of a Program
          1. Preprocessor Directives
          2. Source Code Line by Line
        3. Comments
        4. Functions
          1. Using Arguments with Functions
        5. Summary
        6. Q&A
        7. Workshop
          1. Quiz
          2. Answers
          3. Activities
      3. Hour 3. Creating Variables and Constants
        1. What Is a Variable?
          1. Storing Variables in Memory
          2. Signed and Unsigned Variables
          3. Variable Types
        2. Defining a Variable
        3. Assigning Values to Variables
        4. Using Type Definitions
        5. Constants
          1. Defining Constants
          2. Enumerated Constants
        6. Auto-Typed Variables
        7. Summary
        8. Q&A
        9. Workshop
          1. Quiz
          2. Answers
          3. Activities
      4. Hour 4. Using Expressions, Statements, and Operators
        1. Statements
          1. Whitespace
          2. Compound Statements
        2. Expressions
        3. Operators
          1. Assignment Operator
          2. Mathematical Operators
          3. Combining Operators
          4. Increment and Decrement Operators
          5. Prefix and Postfix Operators
          6. Operator Precedence
          7. Relational Operators
        4. If-Else Conditional Statements
          1. The Else Clause
          2. Compound If Statements
        5. Logical Operators
          1. AND Operator
          2. OR Operator
          3. NOT Operator
          4. Relational Precedence
        6. Tricky Expression Values
        7. Summary
        8. Q&A
        9. Workshop
          1. Quiz
          2. Answers
          3. Activities
      5. Hour 5. Calling Functions
        1. What Is a Function?
        2. Declaring and Defining Functions
        3. Using Variables with Functions
          1. Local Variables
          2. Global Variables
        4. Function Parameters
        5. Returning Values from Functions
        6. Default Function Parameters
        7. Overloading Functions
          1. Inline Functions
        8. Auto-Typed Return Values
        9. Summary
        10. Q&A
        11. Workshop
          1. Quiz
          2. Answers
          3. Activities
      6. Hour 6. Controlling the Flow of a Program
        1. Looping
        2. while Loops
          1. Breaking Out of Loops
          2. Continuing to the Next Loop
        3. do-while Loops
        4. for Loops
          1. Advanced for Loops
          2. Nested Loops
        5. switch Statements
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
          3. Activities
      7. Hour 7. Storing Information in Arrays and Strings
        1. What Is an Array?
        2. Writing Past the End of Arrays
        3. Initializing Arrays
        4. Multidimensional Arrays
          1. Initializing Multidimensional Arrays
          2. A Word About Memory
        5. Character Arrays
        6. Copying Strings
        7. Reading Arrays with Foreach Loops
        8. Summary
        9. Q&A
        10. Workshop
          1. Quiz
          2. Answers
          3. Activities
    11. Part II: Classes
      1. Hour 8. Creating Basic Classes
        1. What Is a Type?
        2. Creating New Types
        3. Classes and Members
          1. Declaring a Class
          2. Defining an Object
        4. Accessing Class Members
        5. Private Versus Public Access
        6. Implementing Member Functions
        7. Creating and Deleting Objects
          1. Default Constructors
          2. Constructors Provided by the Compiler
        8. Summary
        9. Q&A
        10. Workshop
          1. Quiz
          2. Answers
          3. Activities
      2. Hour 9. Moving into Advanced Classes
        1. const Member Functions
        2. Interface Versus Implementation
        3. Organizing Class Declarations and Function Definitions
        4. Inline Implementation
        5. Classes with Other Classes as Member Data
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
          3. Activities
    12. Part III: Memory Management
      1. Hour 10. Creating Pointers
        1. Understanding Pointers and Their Usage
          1. Storing the Address in a Pointer
          2. The Indirection Operator, or Using Pointers Indirectly
          3. Pointers, Addresses, and Variables
          4. Manipulating Data by Using Pointers
          5. Examining Addresses Stored in Pointers
          6. Why Use Pointers?
        2. The Stack and the Heap
          1. Using the new Keyword
          2. Using the delete Keyword
          3. Avoiding Memory Leaks
        3. Null Pointer Constant
        4. Summary
        5. Q&A
        6. Workshop
          1. Quiz
          2. Answers
          3. Activities
      2. Hour 11. Developing Advanced Pointers
        1. Creating Objects on the Heap
        2. Deleting Objects
        3. Accessing Data Members Using Pointers
        4. Member Data on the Heap
        5. The this Pointer
        6. Stray or Dangling Pointers
        7. const Pointers
        8. const Pointers and const Member Functions
        9. Summary
        10. Q&A
        11. Workshop
          1. Quiz
          2. Answers
          3. Activities
      3. Hour 12. Creating References
        1. What is a Reference?
        2. Creating a Reference
        3. Using the Address of Operator on References
        4. What Can Be Referenced?
        5. Null Pointers and Null References
        6. Passing Function Arguments by Reference
          1. Making swap() Work with Pointers
          2. Implementing swap() with References
        7. Understanding Function Headers and Prototypes
        8. Returning Multiple Values
          1. Returning Values by Reference
        9. Summary
        10. Q&A
        11. Workshop
          1. Quiz
          2. Answers
          3. Activities
      4. Hour 13. Developing Advanced References and Pointers
        1. Passing by Reference for Efficiency
        2. Passing a const Pointer
        3. References as an Alternative to Pointers
        4. When to Use References and When to Use Pointers
        5. References to Objects Not in Scope
        6. Returning a Reference to an Object on the Heap
        7. Pointer, Pointer, Who Has the Pointer?
        8. Summary
        9. Q&A
        10. Workshop
          1. Quiz
          2. Answers
          3. Activities
    13. Part IV: Advanced C++
      1. Hour 14. Calling Advanced Functions
        1. Overloaded Member Functions
        2. Using Default Values
        3. Initializing Objects
        4. The Copy Constructor
        5. Compile-Time Constant Expressions
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
          3. Activities
      2. Hour 15. Using Operator Overloading
        1. Operator Overloading
          1. Writing an Increment Method
          2. Overloading the Postfix Operator
          3. Overloading the Addition Operator
          4. Limitations on Operator Overloading
          5. operator=
        2. Conversion Operators
          1. The int() Operator
        3. Summary
        4. Q&A
        5. Workshop
          1. Quiz
          2. Answers
          3. Activities
    14. Part V: Inheritance and Polymorphism
      1. Hour 16. Extending Classes with Inheritance
        1. What Is Inheritance?
          1. Inheritance and Derivation
          2. Animals and Inheritance
          3. The Syntax of Derivation
        2. Private Versus Protected
        3. Constructors and Destructors
        4. Passing Arguments to Base Constructors
        5. Overriding Functions
          1. Overloading Versus Overriding
          2. Hiding the Base Class Member Function
          3. Calling the Base Member Function
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
          3. Activities
      2. Hour 17. Using Polymorphism and Derived Classes
        1. Polymorphism Implemented with Virtual Member Functions
        2. How Virtual Member Functions Work
          1. You Can’t Get There from Here
          2. Slicing
          3. Virtual Destructors
          4. Virtual Copy Constructors
          5. The Cost of Virtual Member Functions
        3. Summary
        4. Q&A
        5. Workshop
          1. Quiz
          2. Answers
          3. Activities
      3. Hour 18. Making Use of Advanced Polymorphism
        1. Problems with Single Inheritance
        2. Abstract Data Types
          1. Pure Virtual Functions
          2. Implementing Pure Virtual Functions
          3. Complex Hierarchies of Abstraction
          4. Which Types Are Abstract?
        3. Summary
        4. Q&A
        5. Workshop
          1. Quiz
          2. Answers
          3. Activities
    15. Part VI: Special Topics
      1. Hour 19. Storing Information in Linked Lists
        1. Linked Lists and Other Structures
        2. Linked List Case Study
          1. Delegation of Responsibility
          2. Component Parts
        3. Linked Lists as Objects
        4. Summary
        5. Q&A
        6. Workshop
          1. Quiz
          2. Answers
          3. Activities
      2. Hour 20. Using Special Classes, Functions, and Pointers
        1. Static Member Data
        2. Static Member Functions
        3. Containment of Classes
          1. Accessing Members of the Contained Class
          2. Filtering Access to Contained Members
          3. Copying by Value Versus by Reference
        4. Friend Classes and Functions
          1. Pointers to Functions
          2. Arrays of Pointers to Functions
          3. Passing Pointers to Functions to Other Functions
          4. Using typedef with Pointers to Functions
          5. Pointers to Member Functions
          6. Arrays of Pointers to Member Functions
        5. Summary
        6. Q&A
        7. Workshop
          1. Quiz
          2. Answers
          3. Activities
      3. Hour 21. Using New Features of C++14
        1. The Newest Version of C++
        2. Using auto in Function Return Types
        3. Improved Numeric Literals
        4. The constexpr Keyword
        5. Lambda Expressions
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
          3. Activities
      4. Hour 22. Employing Object-Oriented Analysis and Design
        1. The Development Cycle
        2. Simulating an Alarm System
          1. Conceptualization
          2. Analysis and Requirements
          3. High-Level and Low-Level Design
          4. Other Objects
          5. Designing the Classes
          6. Adding More Classes
          7. Event Loops
        3. PostMaster: A Case Study
          1. Measure Twice, Cut Once
          2. Divide and Conquer
          3. Message Format
          4. Initial Class Design
          5. Rooted Hierarchies Versus Non-Rooted
          6. Designing the Interfaces
          7. Building a Prototype
          8. The 80/80 Rule
          9. Designing the PostMasterMessage Class
          10. The Application Programming Interface
          11. Programming in Large Groups
          12. Ongoing Design Considerations
          13. Working with Driver Programs
        4. Summary
        5. Q&A
        6. Workshop
          1. Quiz
          2. Answers
          3. Activities
      5. Hour 23. Creating Templates
        1. What Are Templates?
        2. Instances of the Template
        3. Template Definition
        4. Using Template Items
        5. Summary
        6. Q&A
        7. Workshop
          1. Quiz
          2. Answers
          3. Activities
      6. Hour 24. Dealing with Exceptions and Error Handling
        1. Bugs, Errors, Mistakes, and Code Rot
        2. Handling the Unexpected
        3. Exceptions
          1. How Exceptions are Used
        4. Using try and catch Blocks
          1. Catching Exceptions
          2. More Than One Catch
          3. Catching by Reference and Polymorphism
        5. Writing Professional-Quality Code
          1. Braces
          2. Long Lines
          3. switch Statements
          4. Program Text
          5. Identifier Names
          6. Spelling and Capitalization of Names
          7. Comments
          8. Access
          9. Class Definitions
          10. include Files
          11. const
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
          3. Activities
    16. Part VII: Appendixes
      1. Appendix A. Binary and Hexadecimal
        1. Other Bases
        2. Around the Bases
          1. Binary
          2. Why Base 2?
          3. Bits, Bytes, and Nybbles
          4. What’s a KB?
          5. Binary Numbers
        3. Hexadecimal
      2. Appendix B. Glossary
      3. Appendix C. This Book’s Website
      4. Appendix D. Using the MinGW C++ Compiler on Windows
        1. Downloading MinGW-w64
        2. Setting the Path Environment Variable
        3. Testing Your Installation
          1. Opening Folders in MS-DOS
          2. Creating Folders in MS-DOS
          3. Running Programs in MS-DOS
    17. Index
    18. Code Snippets