C++ Programming

Class Structure

Description

This is a foundation course on learning modern C++ language (C++ 11 and beyond).

This course covers object-oriented programming using modern C++. Because most automation, embedded applications, gaming, and many large data processing applications are written in C++, it is essential that software developers understand and master it. Hardware engineers are increasingly using C++ and OOP in system verification tasks, and as of the current decade, there has been a noticeable shift from C to C++ on microcontroller systems.

This course offers an excellent foundation in developing optimized modern C++ applications. The participants will learn to write faster and modular code, and the debugging techniques, for real-world applications. There will be assignments and exercises to accompany the lectures.

Learning Outcomes
At the conclusion of the course, you should be able to:

  • Modern C++ (post C++11)
  • Apply object oriented concepts to software development problems using modern C++
  • Understand and use the basic programming constructs of C++
  • Manipulate various C++ data types, such as arrays, strings, and pointers
  • Write C++ code using principles of object-oriented programming
  • Understand design patterns in C++ using singleton pattern
  • Manage memory appropriately, including proper allocation/deallocation procedures using unique and smart pointers
  • Best practices (dos and don'ts)
  • Utilize best practices from C++ GSL or Google Abseil libraries

1. Introduction

  • Setup development environment
  • Review class structure
  • Operating System Fundamentals: Linux, Mac, POSIX, RTOS
  • Modern approach to "manpages"
  • SSH and password-less login
  • System Monitoring: topdf
  • Homework: Operating Systems and POSIX

2. Basic Syntax & Functions

  • Basics
    • Types, strings
    • Control structures
  • Declare and define functions
    • Call by value, reference, pointer
    • Overloading
    • Default parameters

3. Object Oriented Basics

  • Classes and objects
  • Constructors and destructors
  • Inheritance
  • Rule of 3
  • Introduction to unique_ptr

4. Operators & Templates

5. Midterm

  • Review Questions
  • Feedback
  • Streams
  • Chrono
  • Exam

6. Object Oriented Advanced

  • OO Inheritance
  • Abstract classes
  • Virtual and pure virtual functions
  • Virtual destructors
  • Polymorphism

7. Smart Pointers

  • Unique pointer
  • Shared pointer
  • Weak pointer

8. More Advanced Topics

  • Lambdas
    • How it can be used
  • Concurrency
    • Threads
    • Mutexes
    • Condition Variables
  • Deadlock Empire

9. Powerful Libraries

  • Libraries
    • Boost
    • Guideline support library (GSL)
    • Abseil
  • Design patterns
    • Singleton

10. Final Examination

  • Review Questions
  • Collect Feedback
  • Exam

 

Development Environment

Development Environment

C++ Development Environment (Legacy)


There is a more modern version of the C++ development environment. See this article.

This article provides more direct instructions to compile C++ code. More elaborate information can be read at this Visual Studio Code page which is a highly recommended read for knowledge sake.

Setup Docker

The first thing is to download Docker; please visit https://www.docker.com/products/docker-desktop/ to download Docker for your machine.

Install and Start Docker

After you start the Docker Desktop, you should land on a screen like the following. Feel free to poke around and learn Dockers.

Setup Visual Studio Code

We will use Visual Studio Code as an IDE. There has been tremendous amount of support and plugins to serve as a really good code editor. And more importantly for us, it provides docker container for C++ development out-of-the-box.

Install Visual Studio Code

Please go to https://code.visualstudio.com/ to install Visual Studio Code for your machine. After installation, go ahead and fire up the program.

Install Dev Containers

After installing and starting up Visual Studio Code, it is time to install a "devcontainers" extension to be able to fire up your C++ development environment.

Much of the documentation is captured here a the devcontainers reference page. But, we will provide brief details to get you going.

Go to "Extensions", search for "dev containers" and install it. You can also read this page for more elaborate information.

Setup Dev Container

After the Dev Container extension is installed, it is now time to start the C++ container. Click on the green arrows on the bottom left side of Visual Studio Code.

This will bring up a menu, select New Dev Container.

Type "C++", and select the C++ container:

Grab a cup of coffee because building and starting this "Dev Container" will take a while. Click on "show log" to view details.

Checkpoint

Let's ensure that everything is running as expected. If you go back to Docker Desktop, you should see a container running.

Visual Studio code attaches to this container and you should be able to add code and compile it at this point.

Hello World

From the left-side of Visual Studio Code, create a new folder `hello_world` (click on +folder icon).

Add Source Files

Add these two files (thanks to ChatGPT):

CMakeLists.txt
# CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(HelloWorld)

# Define the source files
set(SOURCES
    hello_world.cpp
    #another_file.hh
)

# Add executable target
add_executable(${PROJECT_NAME} ${SOURCES})
hello_world.cpp
#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Build and Run

Create a build directory, and right click it and choose Open in Integrated Terminal.

Type the following commands to validate that you can build and run the C++ code.

cmake ..
make
./HelloWorld

 

Rerun

If you have closed Docker and Visual Studio code, here is the process to resume:

  1. Go to Docker Desktop and restart your CPP Container
  2. Open Visual Studio Code and attach to the running container