O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Beginning C++ Programming

Book Description

Modern C++ at your fingertips!

About This Book

  • This book gets you started with the exciting world of C++ programming
  • It will enable you to write C++ code that uses the standard library, has a level of object orientation, and uses memory in a safe and effective way
  • It forms the basis of programming and covers concepts such as data structures and the core programming language

Who This Book Is For

A computer, an internet connection, and the desire to learn how to code in C++ is all you need to get started with this book.

What You Will Learn

  • Get familiar with the structure of C++ projects
  • Identify the main structures in the language: functions and classes
  • Feel confident about being able to identify the execution flow through the code
  • Be aware of the facilities of the standard library
  • Gain insights into the basic concepts of object orientation
  • Know how to debug your programs
  • Get acquainted with the standard C++ library

In Detail

C++ has come a long way and is now adopted in several contexts. Its key strengths are its software infrastructure and resource-constrained applications, including desktop applications, servers, and performance-critical applications, not to forget its importance in game programming. Despite its strengths in these areas, beginners usually tend to shy away from learning the language because of its steep learning curve.

The main mission of this book is to make you familiar and comfortable with C++. You will finish the book not only being able to write your own code, but more importantly, you will be able to read other projects. It is only by being able to read others' code that you will progress from a beginner to an advanced programmer. This book is the first step in that progression.

The first task is to familiarize you with the structure of C++ projects so you will know how to start reading a project. Next, you will be able to identify the main structures in the language, functions, and classes, and feel confident being able to identify the execution flow through the code. You will then become aware of the facilities of the standard library and be able to determine whether you need to write a routine yourself, or use an existing routine in the standard library.

Throughout the book, there is a big emphasis on memory and pointers. You will understand memory usage, allocation, and access, and be able to write code that does not leak memory. Finally, you will learn about C++ classes and get an introduction to object orientation and polymorphism.

Style and approach

This straightforward tutorial will help you build strong skills in C++ programming, be it for enterprise software or for low-latency applications such as games or embedded programming. Filled with examples, this book will take you gradually up the steep learning curve of C++.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Customer Feedback
  2. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  3. Starting with C++
    1. What will you find in this chapter?
    2. What is C++?
    3. Installing Visual C++
      1. Setting up
      2. Downloading the installation files
      3. Installing Visual Studio
      4. Registering with Microsoft
    4. Examining C++ projects
      1. Compilers
      2. Linking the code
      3. Source files
        1. A simple example
        2. Creating source files
        3. Compiling the code
        4. Passing parameters between the command-line and an executable
      4. The preprocessor and symbols
        1. Defining constants
        2. Using macros
        3. Using symbols
        4. Using pragmas
      5. Dependencies
        1. Libraries
        2. Pre-compiled headers
        3. Project structure
        4. Managing dependencies
        5. Makefiles
    5. Writing a simple project
      1. The project structure
      2. Creating the precompiled header
      3. Creating the main file
      4. Using input and output streams
      5. Using time functions
      6. Building the executable
      7. Testing the code
      8. Changing the project
    6. Summary
  4. Understanding Language Features
    1. Writing C++
      1. Using white space
      2. Formatting code
      3. Writing statements
        1. Working with expressions
        2. Using the comma operator
      4. Using types and variables
      5. Using constants and literals
        1. Defining constants
        2. Using constant expressions
        3. Using enumerations
        4. Declaring pointers
      6. Using namespaces
      7. C++ scoping of variables
    2. Using operators
      1. Exploring the built-in operators
        1. Arithmetic operators
        2. Increment and decrement operators
        3. Bitwise operators
        4. Boolean operators
        5. Bitwise shift operators
        6. Assignment operators
    3. Controlling execution flow
      1. Using conditional statements
      2. Selecting
      3. Looping
        1. Looping with iteration
        2. Conditional loops
      4. Jumping
    4. Using C++ language features
    5. Summary
  5. Exploring C++ Types
    1. Exploring built-in types
      1. Integers
        1. Specifying integer literals
          1. Using bitset to show bit patterns
        2. Determining integer byte order
      2. Floating point types
        1. Specifying floating point literals
      3. Characters and strings
        1. Character types
        2. Using character macros
        3. Specifying character literals
        4. Specifying string literals
          1. Unicode literals
          2. Raw strings
          3. String byte order
      4. Boolean
      5. void
      6. Initializers
      7. Default values
      8. Declarations without a type
      9. Storage classes
      10. Using type aliases
      11. Aggregating data in record types
        1. Structures
          1. Initializing
          2. Structure fields
          3. Using structure names
          4. Determining alignment
        2. Storing data in the same memory with unions
      12. Accessing runtime type information
      13. Determining type limits
    2. Converting between types
      1. Type conversions
        1. Promoting conversions
        2. Narrowing conversions
        3. Narrowing to bool
        4. Converting signed types
      2. Casting
        1. Casting away const-ness
        2. Casting without runtime checks
        3. Casting pointers without runtime checks
        4. Casting with runtime checks
        5. Casting with list initializer
        6. Using C casts
    3. Using C++ types
    4. Summary
  6. Working with Memory, Arrays, and Pointers
    1. Using memory in C++
      1. Using C++ pointer syntax
      2. Using null pointers
      3. Types of memory
      4. Pointer arithmetic
    2. Using arrays
      1. Function parameters
      2. Multidimensional arrays
      3. Passing multidimensional arrays to functions
      4. Using arrays of characters
        1. Comparing strings
        2. Preventing buffer overruns
    3. Using pointers in C++
      1. Accessing out of bounds
      2. Pointers to deallocated memory
      3. Converting pointers
        1. Constant pointers
        2. Changing the type pointed to
    4. Allocating memory in code
      1. Allocating individual objects
      2. Allocating arrays of objects
      3. Handling failed allocations
      4. Using other versions of the new operator
      5. Memory lifetime
      6. The Windows SDK and pointers
    5. Memory and the C++ Standard Library
      1. Standard Library arrays
        1. Using the stack-based array class
        2. Using the dynamically allocated vector class
    6. References
      1. Constant references
      2. Returning references
      3. Temporaries and references
      4. The rvalue references
      5. Ranged for and references
    7. Using pointers in practice
      1. Creating the project
      2. Adding a task object to the list
      3. Deleting the task list
      4. Iterating the task list
      5. Inserting items
    8. Summary
  7. Using Functions
    1. Defining C++ functions
      1. Declaring and defining functions
      2. Specifying linkage
      3. Inlining
      4. Determining the return type
      5. Naming the function
      6. Function parameters
      7. Specifying exceptions
      8. Function body
    2. Using function parameters
      1. Passing Initializer lists
      2. Using default parameters
      3. Variable number of parameters
        1. Initializer lists
        2. Argument lists
    3. Function features
      1. Call stack
      2. Specifying calling conventions
        1. Using C linkage
        2. Specifying how the stack Is maintained
      3. Using recursion
      4. Overloading functions
      5. Functions and scope
      6. Deleted functions
      7. Passing by value and passing by reference
    4. Designing functions
      1. Pre- and post-conditions
      2. Using invariants
    5. Function pointers
      1. Remember the parentheses!
      2. Declaring function pointers
      3. Using function pointers
    6. Templated functions
      1. Defining templates
      2. Using template parameter values
      3. Specialized templates
      4. Variadic templates
    7. Overloaded operators
      1. Function objects
      2. Introducing lambda expressions
    8. Using functions in C++
    9. Summary
  8. Classes
    1. Writing classes
      1. Reviewing structures
      2. Defining classes
      3. Defining class behavior
        1. Using the this pointer
        2. Using the scope resolution operator
        3. Defining class state
      4. Creating objects
        1. Construction of objects
        2. Defining constructors
        3. Delegating constructors
        4. Copy constructor
        5. Converting between types
        6. Making friends
        7. Marking constructors as explicit
        8. Destructing objects
        9. Assigning objects
        10. Move semantics
      5. Declaring static members
        1. Defining static members
        2. Using static and global objects
        3. Named constructors
      6. Nested classes
      7. Accessing const objects
    2. Using objects with pointers
      1. Getting pointers to object members
      2. Operator overloading
      3. Defining function classes
      4. Defining conversion operators
      5. Managing resources
      6. Writing wrapper classes
      7. Using smart pointers
        1. Managing exclusive ownership
        2. Sharing ownership
        3. Handling dangling pointers
    3. Templates
    4. Using classes
    5. Summary
  9. Introduction to Object-Orientated Programming
    1. Inheritance and composition
      1. Inheriting from a class
      2. Overriding methods and hiding names
      3. Using pointers and references
      4. Access levels
        1. Changing access level through inheritance
      5. Inheritance access levels
      6. Multiple inheritance
        1. Object slicing
    2. Introducing polymorphism
      1. Virtual methods
      2. Virtual method tables
      3. Multiple inheritance and virtual method tables
      4. Virtual methods, construction, and destruction
      5. Containers and virtual methods
      6. Friends and inheritance
      7. Override and final
      8. Virtual inheritance
      9. Abstract classes
      10. Obtaining type information
      11. Smart pointers and virtual methods
      12. Interfaces
      13. Class relationships
      14. Using mixin classes
    3. Using polymorphism
    4. Summary
  10. Using the Standard Library Containers
    1. Working with pairs and tuples
    2. Containers
      1. Sequence containers
        1. List
        2. Forward list
        3. Vector
        4. Deque
      2. Associative containers
        1. Maps and multimaps
        2. Sets and multisets
        3. Unordered containers
      3. Special purpose containers
      4. Using iterators
        1. Input and output iterators
        2. Stream iterators
        3. Using iterators with the C Standard Library
    3. Algorithms
      1. Iteration of items
      2. Getting information
      3. Comparing containers
      4. Changing Items
      5. Finding Items
      6. Sorting items
    4. Using the numeric libraries
      1. Compile time arithmetic
      2. Complex numbers
    5. Using the Standard Library
    6. Summary
  11. Using Strings
    1. Using the string class as a container
      1. Getting information about a string
      2. Altering strings
      3. Searching strings
    2. Internationalization
      1. Using facets
    3. Strings and numbers
      1. Converting strings to numbers
      2. Converting numbers to strings
    4. Using stream classes
      1. Outputting floating point numbers
      2. Outputting integers
      3. Outputting time and money
      4. Converting numbers to strings using streams
      5. Reading numbers from strings using streams
    5. Using regular expressions
      1. Defining regular expressions
      2. Standard Library classes
        1. Matching expressions
        2. Using iterators
        3. Replacing strings
    6. Using strings
      1. Creating the project
      2. Processing header subitems
    7. Summary
  12. Diagnostics and Debugging
    1. Preparing your code
      1. Invariants and conditions
      2. Conditional compilation
      3. Using pragmas
      4. Adding informational messages
      5. Compiler switches for debugging
      6. Pre-processor symbols
      7. Producing diagnostic messages
        1. Trace messages with the C runtime
        2. Tracing messages with Windows
      8. Using asserts
    2. Application termination
    3. Error values
      1. Obtaining message descriptions
      2. Using the Message Compiler
    4. C++ exceptions
      1. Exception specifications
      2. C++ exception syntax
      3. Standard exception classes
      4. Catching exceptions by type
      5. Function try blocks
      6. System errors
      7. Nested exceptions
      8. Structured Exception Handling
      9. Compiler exception switches
      10. Mixing C++ and SEH exception handling
      11. Writing exception-safe classes
    5. Summary