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
- C++ Architecture, Toolchain & ISO Standards
- Structure of a C++ Program & Preprocessor Directives
- Standard Streams: std::cout, std::cin, std::cerr & Buffer Flushing
- Namespaces, Scope Resolution Operator & Header Cleanliness
Module 2: Variables, Types & Modern Specifiers
- Primitive Data Types, Integer Widths & sizeof Operator
- Type Inference with auto & decltype in Modern C++
- Constant Correctness: const, constexpr & consteval
- C++ Explicit Casting: static_cast, const_cast, reinterpret_cast & dynamic_cast
Module 3: Operators & Expressions
- Arithmetic, Increment & Compound Assignment Operators
- Relational, Logical Operators & The Spaceship Operator <=>
- Bitwise Operations, Bit Manipulation & the <bit> Header
Module 4: Control Flow & Selection Statements
- Conditionals & Selection Statements with Initializers (C++17)
- Switch Statements, [[fallthrough]] & Scoped Enums
- Ternary Operator, Short-Circuit Logic & Guard Clauses
Module 5: Loops & Range-Based Iteration
- While & Do-While Loops: Invariants & Sentinel Controls
- Standard Indexed For Loops, Iteration Strides & Loop Scope
- Range-Based For Loops & Structured Bindings (C++17)
Module 6: References & Pointers
- Lvalue References, Aliasing & Pass-by-Reference
- Raw Pointers, Address-of Operator & nullptr Safety
- Pointers to Const, Const Pointers & Pointer Arithmetic
Module 7: Modern Strings & Memory Slices
- C-Style Strings vs Modern std::string
- std::string Operations & Small String Optimization (SSO)
- std::string_view: Zero-Copy String Slices (C++17)
Module 8: Arrays, std::array & std::vector
- Raw Fixed Arrays vs std::array<T, N> (Safe Stack Buffers)
- Dynamic Arrays with std::vector: push_back vs emplace_back
- Vector Memory Internals: size, capacity, reserve & Growth
- STL Iterators: Categories, Traversal & Iterator Invalidation
Module 9: Functions, Lambdas & Callables
- Function Declarations, Parameter Passing & RVO
- Function Overloading, Name Mangling & Default Arguments
- Inline Functions & Thread-Safe Static Local Variables
- Lambda Expressions: Captures, Closures & Generic Lambdas
Module 10: Classes, Encapsulation & RAII
- Classes, Structs & Access Specifiers (Encapsulation)
- Constructors & Member Initializer Lists
- Destructors & The RAII (Resource Acquisition Is Initialization) Idiom
- The this Pointer, Const Member Functions & Method Chaining
Module 11: Inheritance & Dynamic Polymorphism
- Inheritance & Access Control in Class Hierarchies
- Virtual Functions, Dynamic Dispatch & The vtable Internals
- Modern Polymorphic Overrides: override & final Specifiers
- Abstract Classes, Pure Virtual Functions & Interfaces
Module 12: Operator Overloading & Rule of 0/3/5
- Operator Overloading: Arithmetic, Streams & Equality
- Copy Semantics, Deep Copying & The Rule of Three
- Move Semantics, Rvalue References (&&) & The Rule of Five
Module 13: Templates, Metaprogramming & Concepts
- Function Templates, Type Deduction & Specialization
- Class Templates & Non-Type Template Parameters
- C++20 Concepts & Constraints: The requires Clause
Module 14: STL Containers & Algorithms
- Associative Containers: std::map vs std::unordered_map
- Heterogeneous Data: std::pair, std::tuple & Structured Bindings
- Standard STL Algorithms: sort, find, transform & accumulate
Module 15: Smart Pointers, File I/O & Exceptions
- Modern Smart Pointers: unique_ptr, shared_ptr & weak_ptr
- 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 totalCheck 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 passedAdd 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
No comments:
Post a Comment
Please do not enter any spam link in the comment box.