Get Started Now

Intro to C++ Programming

Starting from the fundamentals, become a C++ software engineer, step by step.

LATEST UPDATES

Screenshot from Cyberpunk 2077
Module One

Intro to C++ Programming

Starting from the basics, become a C++ software engineer, step by step

Screenshot from Cyberpunk 2077
Screenshot from The Witcher 3: Wild Hunt
Screenshot from Warhammer: Total War
Screenshot from Cyberpunk 2077
Module Two

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects

Screenshot from Cyberpunk 2077
Screenshot from The Witcher 3: Wild Hunt
Screenshot from Warhammer: Total War
Screenshot from Cyberpunk 2077
Coming Soon

SDL and C++ Development

Learn C++ and SDL development by recreating classic retro games

Screenshot from Cyberpunk 2077
Screenshot from The Witcher 3: Wild Hunt
Screenshot from Warhammer: Total War
MOST POPULAR

Lambdas

An introduction to lambda expressions - a concise way of defining simple, ad-hoc functions
Abstract art representing computer programming
Ryan McCombe
Updated
Published

In this lesson, we introduce Lambdas. They provide a more concise syntax for creating functions. Lambdas give us more options for designing our software, as they have two key differences compared to regular functions:

  • They can be defined almost anywhere - including within the bodies of other functions
  • They can be anonymous - we don't need to give them a name

This makes lambdas the default choice for scenarios needing simple, one-off functions. For example, if we need a function to pass as the parameter of another function, lambdas are often the default choice.

This is because they make our code faster to write, and more readable. The lambda is defined at the same place it is being used and, if the lambda is used only in that place, we don't need to clutter our code with unnecessary identifiers.

Standard Library Function Helpers

A comprehensive overview of function helpers in the standard library, including std::invocable, std::predicate and std::function.
Abstract art representing computer programming
Ryan McCombe
Updated
Published

In this lesson, we’ll introduce some of the standard library utilities that can make working with the features we covered earlier in this chapter easier. We’ll cover:

  • The std::invocable concept lets us specify type requirements for any of the callables we’ve covered
  • The more specific std::predicate concept, lets us specify that our templates require a predicate
  • The std::function container provides a generalized way to store a callable
  • Some standard library functors allow us to quickly create some commonly needed callables

Function Binding and Partial Application

This lesson covers function binding and partial application using std::bind(), std::bind_front(), std::bind_back() and std::placeholders.
Abstract art representing computer programming
Ryan McCombe
Updated
Published

Earlier in the chapter, we introduced the concept of partial application. With partial application, we provide only some of a function’s required arguments - the remaining arguments are provided later, elsewhere in our code.

Being able to implement this approach gives us more flexibility in our designs, and is sometimes required. We introduced an example where we wanted to determine if a Player object was a minimum level but, because of constraints in our design, we were not able to provide the Player and MinimumLevel at the same time.

We solved this using a functor. We provided the MinLevel as a constructor argument, where it then got saved to a member variable. We then later provided the Player object as an argument to the () operator.

A simplified implementation of the full example we introduced in the Functors lesson is below:

#include <iostream>

struct Player {
  int GetLevel() const { return 45; }
};

struct LevelChecker {
  int MinLevel {50};
  bool operator()(const Player& P) {
    return P.GetLevel() >= MinLevel;
  }
};

int main() {
  Player PlayerOne;
  LevelChecker IsLevel40{40};
  LevelChecker IsLevel50{50};

  if (IsLevel40(PlayerOne)) {  
    std::cout << "Player is level 40 or above";
  }

  if (!IsLevel50(PlayerOne)) {
    std::cout << "\nBut not level 50 or above";
  }
}
Player is level 40 or above
But not level 50 or above

In this lesson, we’ll cover a more direct and flexible way of implementing partial application, using std::bind.

First Class Functions

Learn about first-class functions in C++: a feature that lets you store functions in variables, pass them to other functions, and return them, opening up new design possibilities
Abstract art representing computer programming
Ryan McCombe
Updated
Published

Much of the content of this series relates to a programming concept known as first-class functions.

A programming language supports first-class functions if it allows functions to be treated like any other type of data. For example:

  • Functions can be stored as variables, class members, or within a container such as an array
  • Functions can be passed as arguments to other functions
  • Functions can be returned from other functions

C++ supports first-class functions in several different ways. In this chapter, we will cover the three main options we have: function pointers, function objects, and lambdas

But first, let's see a scenario where first-class functions are a useful feature

Function Pointers

Learn about function pointers: what they are, how to declare them, and their use in making our code more flexible
Abstract art representing computer programming
Ryan McCombe
Updated
Published

In this lesson, we'll show how to create and use function pointers. This is one of the ways C++ implements first-class functions.

First-class functions are functions that can be treated like any other data type. For example, they can be stored in variables and passed around, even as arguments, and return values in other functions.

For function pointers, the key point is that we can imagine functions existing in an area of memory, like any other variable.

We can see this using the address of operator & with the name of one of our functions. The following program will log out a memory address:

Function Objects (Functors)

This lesson introduces function objects, or functors. This concept allows us to create objects that can be used as functions, including state management and parameter handling.
Abstract art representing computer programming
Ryan McCombe
Updated
Published

Another option we have for implementing first-class functions in C++ is function objects. Function objects are sometimes also referred to as functors.

Creating functors involves operator overloading. We’ve previously seen how we can overload operators like ++ in our custom class code.

The syntax we use to call functions, (), is also an operator, and so can be overloaded in the same way:

Creating a Window

Learn how to create and customize windows, covering initialization, window management, and rendering
Abstract art representing computer programming
Ryan McCombe
Updated
Published

With SDL2 successfully installed and added to our project, we should now be able to create and manipulate windows.

This lesson goes over the steps in how we can do this. We break down and explain every function in the process, as well as describe the most common options we have for customizing our window.

Creating the Main Loop with SDL2

Step-by-step guide on creating the SDL2 application and event loops for interactive games
Abstract art representing computer programming
Ryan McCombe
Updated
Published

The first step of implementing any application is writing the foundational code that keeps the program running until we or the user decide it’s time to quit. This code is often called the application loop, main loop, or, if we’re making a game, the game loop.

Typically, every iteration of the loop involves 3 steps:

  1. Process any events, such as user input. This involves a second, nested loop often called the event loop.
  2. Ask all of our objects to update themselves, ready to be rendered to the screen. Any events that occurred in step 1 can influence this process
  3. Render a new frame to the screen, so the user can see the changes.

Double Buffering

Learn the essentials of double buffering in C++ with practical examples and SDL2 specifics to improve your graphics projects
Abstract art representing computer programming
Ryan McCombe
Updated
Published

In the previous lesson, we described a typical game loop:

  1. Get input
  2. Update all our objects
  3. Render the frame

The transition from step 2 to step 3 warrants some further explanation. There is some theory here that is very important to understanding how SDL, and real-time graphics in general, works.

In computer graphics, movement is simulated by quickly showing a sequence of still images, typically called "frames".

A lot of complexity and clever algorithms can go into generating these frames. After all the processing, we are left with a collection of pixel colors that we want to display to the user.

Hashing and std::hash

This lesson provides an in-depth look at hashing in C++, including std::hash, collision strategies, and usage in hash-based containers.
Abstract art representing computer programming
Ryan McCombe
Updated
Published

We covered arrays in some detail in our earlier chapter. We explained how these structures give us a simple way to group data - we can add or remove items to the collection and iterate over them easily.

We can also jump to any item in the collection, using its index. This is a very fast operation - it can be done in constant time, that is, O(1)O(1)

We covered time complexity and "Big O" notation in an earlier lesson:

Module One
3D art showing a progammer setting up a development environment

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 56 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Module Two
A computer programmer

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Coming Soon
sdl2-promo.jpg

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Under Construction
Get Started Now

Intro to C++ Programming

Starting from the fundamentals, become a C++ software engineer, step by step.

Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved