Beginner's Guide
    First Steps
    Input & Output
    Custom Types – Part 1
    Diagnostics
    Standard Library – Part 1
    Function Objects
    Standard Library – Part 2
    Code Organization
    Custom Types – Part 2
    Generic Programming
    Memory Management
    Software Design Basics

    Hello World Hello World Hello World

    Source File hello.cpp : File hello.cpp : hello.cpp

    #include <iostream> #include
    • this line will be replaced with the content of the file iostream
    • iostream is a header file in the compiler directory that provides input and output functionalities
    • #include "path/to/filename" ⇒ inserts the content of a file
    • #include <filename> ⇒ same, but searches for file in all include directories
    • happens before compilation ⇒ compiler does only see the already preprocessed file
    // our first program Comments

    Comments are ignored by the compiler.


    // single line comment

    /* C-style 
       multi-line comment */
    int main () main()
    • defines a function called "main"
    • every program starts by executing the main function
    • int is the only allowed return type for the main function (int refers to an integer (whole) number)
    • () is an empty parameter list
    {
    • blocks of statements are enclosed in curly braces {}
    • statements are terminated by a semi-colon ;
     std::cout << "Hello World\n"; std::cout
    • this statement writes text to the console
    • std is the namespace of the standard library
    • cout (short for "character out") refers to the standard (console) output
    • "Hello World\n" is a string literal – a sequence of characters
    • \n is a special new line/line break character
    }
    • the program terminates after executing the main function
    • it will automatically return 0 (indicating success) if no return statement is given
    • return codes other than 0 are interpreted as an error by the operating system executing the program

    Compiling hello.cpp : Compiling

    • source code can't be run directly
    • code is written to an abstract machine model (more on that later)
    • compiler translates source code into binary machine code understood by the CPU
    • program that can be run = binary executable file containing machine code
    $ g++ hello.cpp -o sayhello
    $ ./sayhello
    Hello World!
    compile & link 
    run executable "sayhello"
    program output
    example of how to compile a single file

    Terminology

    • Compiler Error = program not compilable, compiler will stop
    • Compiler Warning = program compilable, compiler will continue, but there is a problematic piece of code that might lead to runtime bugs

    • static = fixed at compile time (baked into the executable file, not changeable at runtime)
    • dynamic = changeable at runtime (possibly by user input)

    Compiler Flags

    g++ -std=c++20 -Wall -Wextra -Wpedantic -Wshadow input.cpp -o output

    -std=c++20 Sets compiler to the C++20 standard. Highly Recommended.
    • -Wall
    • -Wpedantic
    • -Wextra
    • -Wshadow
    Enable compiler warnings. Highly recommended. These don't really activate all warnings, but rather the most important ones that don't produce too much (false positive) noise.
    -o <filename> Sets the name of the output (executable) file.

    It's 2023 – set your compiler to C++20 (or at least to C++17 if you have to use an older compiler).

    Don't Use  using namespace std;! using namespace std; Don't!

    Many code examples / tutorials show something like this:

    using namespace std;
    int main () {
      cout << "bla\n";
    }

    … to avoid qualifying cout with std::.

    using a namespace drags all symbols from that namespace into the global namespace. This can lead to name conflicts and ambiguities and in some cases even bugs that will only manifest at runtime and are very hard to detect.

    Polluting the global namespace with all symbols from other namespaces is a serious liability in any production code base and you should avoid this anti-pattern from the very start.