C++ Fundamentals

Book description

Write high-level abstractions while retaining full control of the hardware, performances, and maintainability.

Key Features

  • Transform your ideas into modern C++ code, with both C++11 and C++17
  • Explore best practices for creating high-performance solutions
  • Understand C++ basics and work with concrete real-world examples

Book Description

C++ Fundamentals begins by introducing you to the C++ compilation model and syntax. You will then study data types, variable declaration, scope, and control flow statements. With the help of this book, you'll be able to compile fully working C++ code and understand how variables, references, and pointers can be used to manipulate the state of the program. Next, you will explore functions and classes - the features that C++ offers to organize a program - and use them to solve more complex problems. You will also understand common pitfalls and modern best practices, especially the ones that diverge from the C++98 guidelines.

As you advance through the chapters, you'll study the advantages of generic programming and write your own templates to make generic algorithms that work with any type. This C++ book will guide you in fully exploiting standard containers and algorithms, understanding how to pick the appropriate one for each problem.

By the end of this book, you will not only be able to write efficient code but also be equipped to improve the readability, performance, and maintainability of your programs.

What you will learn

  • C++ compilation model
  • Apply best practices for writing functions and classes
  • Write safe, generic, and efficient code with templates
  • Explore the containers that the C++ standard offers
  • Discover the new features introduced with C++11, C++14, and C++17
  • Get to grips with the core language features of C++
  • Solve complex problems using object-oriented programming in C++

Who this book is for

If you're a developer looking to learn a new powerful language or are familiar with C++ but want to update your knowledge with modern paradigms of C++11, C++14, and C++17, this book is for you. To easily understand the concepts in the book, you must be familiar with the basics of programming.

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. About the Book
      1. About the Authors
      2. Objectives
      3. Audience
      4. Approach
      5. Minimum Hardware Requirements
      6. Software Requirements
      7. Additional Resources
      8. Conventions
  2. Chapter 1
  3. Getting Started
    1. Introduction
    2. The C++ Compilation Model
      1. Difference Between Header and Source Files
      2. Compiling a File into an Object File
      3. Linking Object Files
      4. Working with the main Function
      5. Exercise 1: Compiling and Executing the main Function
    3. Built-in Data Types
      1. Primitive Data Types
      2. Datatype Modifiers
      3. Variable Definition
      4. Demystifying Variable Initialization
    4. Pointers and References
      1. Pointers
      2. References
      3. The const Qualifier
      4. The Scope of Variables
    5. Control Flow Statements
      1. Selection Statement – if-else
      2. Selection Statement – switch
      3. Iteration Statement – for loop
      4. Iteration Statement – while loop
      5. Iteration Statement – do-while loop
      6. Jump Statements – break and continue
    6. The try-catch block
      1. Exercise 2: Counting the Number of Times a Specific Number Appears in a Given List
      2. Activity 1: Finding the Factors of 7 between 1 and 100 Using a while Loop
    7. Arrays
      1. Array Declaration
      2. Array Initialization
      3. Accessing the Values of an Array
      4. Multidimensional Arrays
      5. Activity 2: Defining a Bi-Dimensional Array and Initializing Its Elements
    8. Summary
  4. Chapter 2
  5. Functions
    1. Introduction
    2. Function Declaration and Definition
      1. Defining a Function
      2. Exercise 3: Calling a Function from main()
    3. Local and Global Variables
      1. Working with Variable Objects
      2. Exercise 4: Using Local and Global Variables in a Fibonacci Sequence
    4. Passing Arguments and Returning Values
      1. Pass by Value
      2. Exercise 5: Calculating Age using Pass by Value Arguments
      3. Pass by Reference
      4. Exercise 6: Calculating Incrementation of Age using Pass by Reference
      5. Activity 3: Checking Voting Eligibility
    5. Working with const References or r-value References
      1. Returning Values from Functions
      2. Returning by Value
      3. Returning by Reference
      4. Activity 4: Using Pass by Reference and Pass by Value
    6. Const Parameters and Default Arguments
      1. Passing by const Value
      2. Passing by const Reference
      3. Returning by const Value
      4. Returning by const Reference
    7. Default Arguments
    8. Namespaces
      1. Activity 5: Organizing Functions in Namespaces
    9. Function Overloading
      1. Activity 6: Writing a Math Library for a 3D Game
    10. Summary
  6. Chapter 3
  7. Classes
    1. Introduction
    2. Declaring and Defining a Class
      1. The Advantages of Using Classes
      2. C++ Data Members and Access Specifiers
      3. Static Members
      4. Exercise 7: Working with Static Variables
    3. Member Functions
      1. Declaring a Member Function
      2. Using const Member Functions
      3. The this Keyword
      4. Exercise 8: Creating a Program Using the this Keyword to Greet New Users
      5. Non-Member Class-Related Functions
      6. Activity 7: Information Hiding Through Getters and Setters
    4. Constructors and Destructors
      1. Constructors
      2. Overloading Constructor
      3. Constructor Member Initialization
      4. Aggregate Classes Initialization
      5. Destructors
      6. Exercise 9: Creating a Simple Coordinate Program to Demonstrate the Use of Constructors and Destructors
      7. Default Constructor and Destructor
      8. Activity 8: Representing Positions in a 2D Map
    5. Resource Acquisition Is Initialization
      1. Activity 9: Storing Multiple Coordinates of Different Positions on a Map
    6. Nested Class Declarations
    7. Friend Specifier
      1. Friend Functions
      2. Friend Classes
      3. Exercise 10: Creating a Program to Print the User's Height
      4. Activity 10: The AppleTree Class, which Creates an Apple Instance
    8. Copy Constructors and Assignment Operators
      1. The copy Assignment Operator
      2. The move-constructor and move-assignment Operator
      3. Preventing Implicit Constructors and Assignment Operators
    9. Operator Overloading
      1. Activity 11: Ordering Point Objects
    10. Introducing Functors
      1. Activity 12: Implementing Functors
    11. Summary
  8. Chapter 4
  9. Generic Programming and Templates
    1. Introduction
    2. Templates
      1. Compiling the Template Code
      2. Exercise 11: Finding the Bank Account of the User with the Highest Balance
      3. Using Template Type Parameters
      4. Requirements of Template Parameter Types
    3. Defining Function and Class Templates
      1. Function Template
      2. Class Templates
      3. Dependent Types
      4. Activity 13: Reading Objects from a Connection
      5. Activity 14: Creating a User Account to Support Multiple Currencies
    4. Non-Type Template Parameters
      1. Activity 15: Writing a Matrix Class for Mathematical Operations in a Game
    5. Making Templates Easier to Use
      1. Default Template Arguments
      2. Template Argument Deduction
      3. Parameter and Argument Types
      4. Activity 16: Making the Matrix Class Easier to Use
    6. Being Generic in Templates
      1. Activity 17: Ensuring Users are Logged in When Performing Actions on the Account
    7. Variadic Templates
      1. Activity 18: Safely Performing Operations on the User Cart with an Arbitrary Number of Parameters
    8. Writing Easy-to-Read Templates
      1. Type Alias
      2. Template Type Alias
    9. Summary
  10. Chapter 5
  11. Standard Library Containers and Algorithms
    1. Introduction
    2. Sequence Containers
      1. Array
      2. Vector
      3. Deque
      4. List
      5. Forward-List
      6. Providing Initial Values to Sequence Containers
      7. Activity 19: Storing User Accounts
    3. Associative Containers
      1. Set and Multiset
      2. Map and Multimap
      3. Activity 20: Retrieving a User's Balance from their Given Username
    4. Unordered Containers
    5. Container Adaptors
      1. Stack
      2. Queue
      3. Priority Queue
      4. Activity 21: Processing User Registration in Order
    6. Unconventional Containers
      1. Strings
      2. Exercise 12: Demonstrating Working Mechanism of the c_str() Function
      3. Pairs and Tuples
    7. std::optional
    8. std::variant
      1. Exercise 13: Using Variant in the Program
      2. Exercise 14: Visitor Variant
      3. Activity 22: Airport System Management
    9. Iterators
      1. Exercise 15: Exploring Iterator
      2. Reverse Iterators
      3. Exercise 16: Exploring Functions of Reverse Iterator
      4. Insert Iterators
      5. Stream Iterators
      6. Exercise 17: Stream Iterator
      7. Iterator Invalidation
      8. Exercise 18: Printing All of the Customers' Balances
    10. Algorithms Provided by the C++ Standard Template Library
      1. Lambda
      2. Read-Only Algorithms
      3. Modifying Algorithms
      4. Mutating Algorithms
      5. Sorting Algorithms
      6. Binary Search Algorithms
      7. Numeric Algorithms
      8. Exercise 19: Customer Analytics
    11. Summary
  12. Chapter 6
  13. Object-Oriented Programming
    1. Introduction
    2. Inheritance
      1. Exercise 20: Creating a Program to Illustrate Inheritance in C++
      2. Exercise 21: Using Multiple Inheritance to Create a "Welcome to the Community" Message Application
      3. Activity 23: Creating Game Characters
    3. Polymorphism
      1. Dynamic Binding
    4. Virtual Methods
      1. Exercise 22: Exploring the Virtual Method
      2. Activity 24: Calculating Employee Salaries
    5. Interfaces in C++
      1. Activity 25: Retrieving User Information
    6. Dynamic Memory
    7. Safe and Easy Dynamic Memory
      1. A Single Owner Using std::unique_ptr
      2. Shared Ownership Using std::shared_ptr
      3. Activity 26: Creating a Factory for UserProfileStorage
      4. Activity 27: Using a Database Connection for Multiple Operations
    8. Summary
  14. Appendix
    1. Lesson 1: Getting Started
      1. Activity 1: Find the Factors of 7 between 1 and 100 Using a while Loop
      2. Activity 2: Define a Bi-Dimensional Array and Initialize Its Elements
    2. Lesson 2: Functions
      1. Activity 3: Calculating if a Person is Eligible to Vote or Not
      2. Activity 4: Apply the Understanding of Passing by Reference or Value in Functions
      3. Activity 5: Organizing Functions in Namespaces
      4. Activity 6: Writing a Math Library for use in a 3D Game
    3. Lesson 3: Classes
      1. Activity 7: Information Hiding Through Getters and Setters
      2. Activity 8: Representing Positions in a 2D Map
      3. Activity 9: Storing Multiple Coordinates of Different Positions in the Map
      4. Activity 10: The AppleTree Class, which Creates an Apple Instance
      5. Activity 11: Ordering Point Objects
      6. Activity 12: Implementing Functors
    4. Lesson 04: Generic Programming and Templates
      1. Activity 13: Read Objects from a Connection
      2. Activity 14: UserAccount to Support Multiple Currencies
      3. Activity 15: Write a Matrix Class for Mathematical Operations in a Game
      4. Solution bonus step:
      5. Activity 16: Make the Matrix Class Easier to Use
      6. Activity 17: Ensure Users are Logged in When Performing Actions on the Account
      7. Activity 18: Safely Perform Operations on User Cart with an Arbitrary Number of Parameters
    5. Lesson 5: Standard Library Containers and Algorithms
      1. Activity 19: Storing User Accounts
      2. Activity 20: Retrieving a User’s Balance from their Given Username
      3. Activity 21: Processing User Registration in Order
      4. Activity 22: Airport System Management
    6. Lesson 6: Object-Oriented Programming
      1. Activity 23: Creating Game Characters
      2. Activity 24: Calculating Employee Salaries
      3. Activity 25: Retrieving User Information
      4. Activity 26: Creating a Factory for UserProfileStorage
      5. Activity 27: Using a Database Connection for Multiple Operations

Product information

  • Title: C++ Fundamentals
  • Author(s): Antonio Mallia, Francesco Zoffoli
  • Release date: March 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789801491