Lec 01
Lec 01
Alexis Engelke
Winter 2024/25
1
Module “Concepts of C++ Programming” (CIT323000)
Goals Non-Goals
▶ Write good and modern C++ code ▶ Become experts in C++
▶ Apply widely relevant C++ constructs ▶ Fancy language features
▶ Understand some advanced language concepts ▶ Apply involved
optimizations
Prerequisites
▶ Fundamentals of object-oriented programming EIDI, PGdP
▶ Fundamentals of data structures and algorithms GAD
▶ Beneficial: operating systems, computer architecture GBS, ERA
2
Lecture Organization
▶ Lecture: Mon 14:30 – 17:00, MW 0001
▶ Lecturer: Dr. Alexis Engelke engelke@in.tum.de
▶ Live stream and recording via RBG: https://live.rbg.tum.de/
▶ Tweedback for questions during lecture
▶ Exercises: Tue 14:15 – 15:45, Interims II HS 3
▶ Florian Drescher, Mateusz Gienieczko
▶ Material: https://db.in.tum.de/teaching/ws2425/cpp/
▶ Zulip-Streams: #CPP, #CPP Homeworks, #CPP Random/Memes
1
We may add extra cases to prevent hard-coding of test cases. 4
Literature
Primary
▶ C++ Reference Documentation. (https://en.cppreference.com/)
Supplementary
▶ Aho, Lam, Sethi & Ullman, 2007. Compilers. Principles, Techniques &
Tools (2nd edition).
▶ Tanenbaum, 2006. Structured Computer Organization (5th edition).
5
What is C++?
▶ Key characteristics
▶ Compiled
▶ Statically typed
▶ Facilities for low-level programming
6
Some C++ History
Initial development
▶ Bjarne Stroustrup at Bell Labs (since 1979)
▶ Originally “C with classes”, renamed in 1983 to C++
▶ In large parts based on C
▶ Inspirations from Simula67 (classes) and Algol68 (operator overloading)
▶ Initially developed as a C++-to-C converter (Cfront)
7
C++ Standard vs. Implementations
2
https://clang.llvm.org/docs/LanguageExtensions.html 8
Why Study C++?
▶ Performance
▶ Very flexible level of abstraction
▶ Direct mapping to hardware capabilities easily possible
▶ Zero-overhead rule: “What you don’t use, you don’t pay for.”
▶ Scales to large systems (with some discipline)
▶ Interoperability with other languages, esp. C
9
This Lecture
10
Hello World!
#include <print>
int main() {
std::println("Hello␣World!");
return 0;
}
11
Hello World, explained3
// End program with exit code 0. (zero = everything ok, non-zero = error)
return 0;
}
3
A bit hand-wavy, but we have to start somewhere. 12
Program Arguments
▶ main can take two paramters to hold command-line arguments
▶ int argc: number of arguments
▶ char** argv: the actual arguments, ∼array of strings
▶ First argument is the program invocation itself (e.g., ./hello2)
#include <print>
int main(int argc, char** argv) {
std::println("Hello␣{}!", argv[1]); // DON’T DO THIS
return 0;
}
14
Debugging 102
▶ Print debugging.
#include <print>
int main(int argc, char** argv) {
std::println("argc={}", argc);
std::println("Hello␣{}!", argv[1]);
return 0;
}
15
Program Arguments, attempt 2
#include <print>
int main(int argc, char** argv) {
if (argc >= 2)
std::println("Hello␣{}!", argv[1]);
else
std::println("Hi␣there!");
return 0;
}
16
Compiler Flags
Compiler invocation: clang++ [flags] -o output inputs...
17
Build Systems: CMake
▶ Reference: https://cmake.org/cmake/help/latest/
18
CMake Example
CMakeLists.txt:
# Require a specific CMake version, here 3.20 for C++23 support
cmake_minimum_required(VERSION 3.20)
# Set project name, required for every project
project(hello2)
# We use C++23, basically adds -std=c++23 to compiler flags
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Compile executable hello2 from hello2.cpp
add_executable(hello2 hello2.cpp)
▶ set(CMAKE_CXX_COMPILER clang++)
Set C++ compiler to clang++
▶ set(CMAKE_BUILD_TYPE Debug)
Set “build type” Debug (other values: Release, RelWithDebInfo);
affects optimization and debug info
21
Overview and Hello World – Questions
22