Explore C++ functions, classes, templates and resource management.

Before starting: Compiler use and basic variables, conditions, and functions.

Practice project: Build a command-line catalog using standard containers and RAII.

Module 1: C++ Foundations & Toolchain

  1. C++ Architecture, Toolchain & ISO Standards
  2. Structure of a C++ Program & Preprocessor Directives
  3. Standard Streams: std::cout, std::cin, std::cerr & Buffer Flushing
  4. Namespaces, Scope Resolution Operator & Header Cleanliness

Module 2: Variables, Types & Modern Specifiers

  1. Primitive Data Types, Integer Widths & sizeof Operator
  2. Type Inference with auto & decltype in Modern C++
  3. Constant Correctness: const, constexpr & consteval
  4. C++ Explicit Casting: static_cast, const_cast, reinterpret_cast & dynamic_cast

Module 3: Operators & Expressions

  1. Arithmetic, Increment & Compound Assignment Operators
  2. Relational, Logical Operators & The Spaceship Operator <=>
  3. Bitwise Operations, Bit Manipulation & the <bit> Header

Module 4: Control Flow & Selection Statements

  1. Conditionals & Selection Statements with Initializers (C++17)
  2. Switch Statements, [[fallthrough]] & Scoped Enums
  3. Ternary Operator, Short-Circuit Logic & Guard Clauses

Module 5: Loops & Range-Based Iteration

  1. While & Do-While Loops: Invariants & Sentinel Controls
  2. Standard Indexed For Loops, Iteration Strides & Loop Scope
  3. Range-Based For Loops & Structured Bindings (C++17)

Module 6: References & Pointers

  1. Lvalue References, Aliasing & Pass-by-Reference
  2. Raw Pointers, Address-of Operator & nullptr Safety
  3. Pointers to Const, Const Pointers & Pointer Arithmetic

Module 7: Modern Strings & Memory Slices

  1. C-Style Strings vs Modern std::string
  2. std::string Operations & Small String Optimization (SSO)
  3. std::string_view: Zero-Copy String Slices (C++17)

Module 8: Arrays, std::array & std::vector

  1. Raw Fixed Arrays vs std::array<T, N> (Safe Stack Buffers)
  2. Dynamic Arrays with std::vector: push_back vs emplace_back
  3. Vector Memory Internals: size, capacity, reserve & Growth
  4. STL Iterators: Categories, Traversal & Iterator Invalidation

Module 9: Functions, Lambdas & Callables

  1. Function Declarations, Parameter Passing & RVO
  2. Function Overloading, Name Mangling & Default Arguments
  3. Inline Functions & Thread-Safe Static Local Variables
  4. Lambda Expressions: Captures, Closures & Generic Lambdas

Module 10: Classes, Encapsulation & RAII

  1. Classes, Structs & Access Specifiers (Encapsulation)
  2. Constructors & Member Initializer Lists
  3. Destructors & The RAII (Resource Acquisition Is Initialization) Idiom
  4. The this Pointer, Const Member Functions & Method Chaining

Module 11: Inheritance & Dynamic Polymorphism

  1. Inheritance & Access Control in Class Hierarchies
  2. Virtual Functions, Dynamic Dispatch & The vtable Internals
  3. Modern Polymorphic Overrides: override & final Specifiers
  4. Abstract Classes, Pure Virtual Functions & Interfaces

Module 12: Operator Overloading & Rule of 0/3/5

  1. Operator Overloading: Arithmetic, Streams & Equality
  2. Copy Semantics, Deep Copying & The Rule of Three
  3. Move Semantics, Rvalue References (&&) & The Rule of Five

Module 13: Templates, Metaprogramming & Concepts

  1. Function Templates, Type Deduction & Specialization
  2. Class Templates & Non-Type Template Parameters
  3. C++20 Concepts & Constraints: The requires Clause

Module 14: STL Containers & Algorithms

  1. Associative Containers: std::map vs std::unordered_map
  2. Heterogeneous Data: std::pair, std::tuple & Structured Bindings
  3. Standard STL Algorithms: sort, find, transform & accumulate

Module 15: Smart Pointers, File I/O & Exceptions

  1. Modern Smart Pointers: unique_ptr, shared_ptr & weak_ptr
  2. Exception Handling & Stream File I/O (fstream)

Trace changing state and check loop boundaries

A trace table records the state after each statement. It helps distinguish an incorrect algorithm from a syntax mistake. Write the input contract first: this example accepts a list of numbers and returns their sum. The initial total is zero, so an empty list returns zero.

Paper trace first; pseudocode is not executable. Use your chosen language compiler or interpreter for the implementation.

PSEUDOCODE — translate before running
total ← 0
FOR EACH value IN values
    total ← total + value
RETURN total
Check the output
[2, -1, 4] → 5
[] → 0
[7] → 7
The loop must process each element exactly once.

Trace [-3, 3] without running code. Then implement and test it. Explain why starting at the second element loses data. Reject non-number input or explicitly document that it is outside the input contract.

Check C input and file errors

Library calls can fail even when the program compiles. Inspect a return value before using the resource it represents. A failed fopen returns a null pointer; passing it to file-reading functions is invalid. This example reports the operating-system error and exits unsuccessfully. It closes the stream on the successful path.

Hosted C17 environment with GCC or Clang

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    FILE *file = fopen("score.txt", "r");
    if (file == NULL) {
        perror("score.txt");
        return EXIT_FAILURE;
    }
    if (fclose(file) == EOF) {
        perror("closing score.txt");
        return EXIT_FAILURE;
    }
    puts("File opened and closed");
    return EXIT_SUCCESS;
}
Check the output
With a readable file: File opened and closed. Without it: an error message on stderr; wording depends on the operating system.

Use fgets to read one line. Distinguish end-of-file from a read error with ferror before deciding which message to display.

Check a C++ collection calculation

Keep a function small enough that its expected result can be calculated by hand. Explicit checks in this example remain active regardless of NDEBUG; they do not rely on assert being enabled. The example checks behavior, not a particular container implementation or number of loop iterations.

C++17 compiler such as GCC or Clang

#include <iostream>
#include <vector>

int total(const std::vector<int>& values) {
    int sum = 0;
    for (int value : values) sum += value;
    return sum;
}

int main() {
    if (total({4, 6}) != 10 || total({}) != 0) {
        std::cerr << "Check failed\n";
        return 1;
    }
    std::cout << "Checks passed\n";
}
Check the output
Checks passed

Add a negative value. State the numeric range your calculation supports; signed integer overflow must not be treated as a valid result.

Open the C++ learning path on DevStudyHub · Play coding games on this blog

logoblog

No comments:

Post a Comment

Please do not enter any spam link in the comment box.