Phase 1 · Modern C++ FoundationsModule 1~42 min read

Introduction to Programming & Modern C++

Discover what Modern C++ is, how source becomes a native program, set up a C++20 compiler, and build your first executable.

What you'll learn

C++ combines direct control over native resources with high-level abstractions that can be both expressive and efficient. In this lesson, you'll establish the modern mental model, install a C++20 toolchain, and compile your first native program.

By the end, you'll be able to:

  • Explain what Modern C++ is and how it relates to—but differs from—C
  • Describe preprocessing, compilation, linking, and execution
  • Compile a warning-clean C++20 program with GCC or Clang
  • Read the essential structure of a small C++ source file

How to use this course

Type the examples, compile with the warning flags shown, and make one controlled change at a time. The editor's Run button shows expected output; your local compiler provides the real diagnostics and execution environment.

What is Modern C++?

C++ is a general-purpose compiled language designed for software where performance, predictable resource use, portability, and rich abstraction all matter. It supports procedural, object-oriented, generic, functional, and low-level programming styles.

C++ grew from C and retains substantial compatibility, but it is its own language. Modern C++ code normally favors std::string, containers, algorithms, value types, and scope-bound resource owners over raw arrays and manual allocation.

Key idea

We will learn C++ as C++, not as “C with classes.” Safe standard-library types and automatic lifetime management are the defaults; lower-level mechanisms are introduced when their purpose and risks can be explained precisely.

Modern C++ principles

PrincipleMeaning
Strong typesMake invalid operations difficult and intent visible
Value semanticsObjects behave like independent values when that matches the domain
RAIITie every resource lifetime to an object's lifetime
Zero-cost abstractionsUseful abstractions should not impose unnecessary runtime cost
Standard library firstReuse specified containers, algorithms, strings, and utilities
Defined behaviorAvoid code whose meaning the language does not guarantee

This course uses C++20 as its baseline. Later lessons identify selected C++23 facilities. Selecting a standard matters because the compiler should reject accidental reliance on unavailable language or library features.

Where C++ is used

C++ is strongest when an application needs control over latency, memory, hardware, or a large cross-platform native codebase while still benefiting from reusable abstractions.

Common Modern C++ domains

Games & graphics

Engines · rendering · simulation

Systems software

Browsers · databases · compilers

Embedded & robotics

Devices · vehicles · real-time control

Creative tools

Audio · video · 3D applications

Finance & science

Low-latency · modeling · research

Cross-platform apps

Desktop · mobile · shared native cores

The language spans hardware-facing code and large user-facing applications.

Note

“Fast” is not automatic. C++ gives engineers the tools to design efficient software, but algorithms, data layout, allocation, I/O, measurement, and correctness still determine the result.

How source becomes a program

A .cpp file is human-readable source. A toolchain translates it into machine instructions and combines it with the standard library and your other compiled files.

From main.cpp to a native executable
{ }
main.cppSourceYour C++ code
→
#
preprocessorExpandHeaders & directives
→
⚙
compilerTranslateObject code
→
↔
linkerCombineObjects & libraries
→
▶
programExecuteNative instructions
A compiler driver coordinates preprocessing, compilation, assembly, and linking.
  1. The preprocessor handles directives such as #include.
  2. The compiler checks types and translates one translation unit.
  3. The assembler emits an object file containing native code.
  4. The linker resolves names across object files and libraries.
  5. The operating system loads and executes the resulting program.

Tip

Compile errors and link errors happen at different stages. Later, this distinction will make missing declarations, missing definitions, and duplicate symbols much easier to diagnose.

Compiler & editor setup

Install a current GCC, Clang, or Microsoft Visual C++ toolchain. On Windows, Visual Studio, LLVM, MSYS2, or WSL are common choices. macOS provides Clang through Xcode Command Line Tools; Linux distributions provide GCC and Clang through their package managers.

Terminal
g++ --version
clang++ --version

# In a Visual Studio Developer Command Prompt:
cl
You need only one working compiler; exact version output differs.

Use an editor with C++ diagnostics and navigation—VS Code, Visual Studio, CLion, or another capable editor—but learn the terminal command too. It makes the real inputs to the compiler visible.

Your first program: Hello, World!

Create main.cpp and enter the following program:

main.cpp
#include <iostream>

int main() {
    std::cout << "Hello, World!\n";
    return 0;
}
Compile locally to execute the code; the displayed result is the expected output.
Terminal
# GCC
g++ -std=c++20 -Wall -Wextra -Wpedantic main.cpp -o hello
./hello

# Clang uses the same core options
clang++ -std=c++20 -Wall -Wextra -Wpedantic main.cpp -o hello

Keep strict warnings enabled

-std=c++20 selects the course baseline. -Wall -Wextra -Wpedanticrequests useful diagnostics. Warning names differ in MSVC, but the principle is the same.

Anatomy of a C++ program

  • #include <iostream> makes standard stream declarations available.
  • int main() defines the program's entry point.
  • std::cout is the standard character-output stream.
  • << inserts a value into an output stream.
  • \n writes a newline without forcing an unnecessary flush.
  • return 0; reports successful completion to the environment.

C++ is case-sensitive

main, Main, and MAIN are different names. Thestd:: prefix states that cout belongs to the standard namespace. Avoid a global using namespace std;; explicit qualification prevents collisions.

Development workflow

  1. Edit one small, understandable change
  2. Compile with the selected standard and strict warnings
  3. Fix the first diagnostic before interpreting later cascaded errors
  4. Run normal, boundary, and invalid-input cases
  5. Save a working checkpoint in version control

A diagnostic normally includes a file, line, column, severity, and explanation. Read the exact source location and nearby context; one missing quote, brace, or semicolon can cause several later messages.

Comments

C++ supports line and block comments. Prefer clear names and small functions; use comments to preserve intent, constraints, units, ownership, or a non-obvious design decision.

comments.cpp
#include <iostream>

int main() {
    // Explain intent or a constraint—not obvious punctuation.
    std::cout << "C++ rewards clear ownership.\n";

    /* Block comments can span several lines,
       but they do not nest. */
    return 0;
}

Watch out

Comments are not verified by the compiler. Update them with the code, and never leave commented-out implementations in place of version-control history.

Recap & quick check

Key takeaways

  • Modern C++ combines native control with strong types, value semantics, RAII, and standard-library abstractions.
  • The build pipeline preprocesses, compiles, assembles, and links translation units.
  • C++20 is this course's explicit baseline; later features are labeled when introduced.
  • std::cout writes standard output, and main returns a process status.
  • Strict diagnostics and short edit-build-run cycles are everyday engineering tools.

Quick check

1. Which stage resolves names across object files and libraries?

2. What does std:: identify in std::cout?

3. Why select -std=c++20 explicitly?

4. Which principle ties resource cleanup to object lifetime?

Your Modern C++ toolchain is ready. Next: Module 2 — Variables, Fundamental Types & Operators, where programs begin representing and transforming real values.