CppForJavaProgrammers 1
CppForJavaProgrammers 1
Felix Friedrich
Malte Schwerhoff
D-INFK 2022
2
This Course
This Course
Who Is Involved?
Dr. Felix Friedrich Dr. Malte Schwerhoff Prof. Dr. Ralf Hiptmair
Why C++
Important language in computational science,
engineering, high-performance computing
NumCS uses C++ to discuss implementations
of numerical methods
Often combined with Python (not this course):
user-friendly Python interface to a
high-performance C++ implementation
8
https://en.cppreference.com/w/cpp/types/integer
15
Differences: Initialisation
C++ default-initialises variables iff the type declares a default constructor
- Applies to local variables, and member variables (object fields)
Undefined Behaviour
Compile- or runtime checks might be performed, but no guarantees
Keep existence in mind
- To avoid common problems (previous slides)
- During debugging (undefined behaviour often results in nondeterministic behaviour)
If interested: https://en.cppreference.com/w/cpp/language/ub
Demo of what may happen: https://gcc.godbolt.org/z/MsjPnWs5Y
19
int main() {
int x = 1;
int y = 2;
swap(x, y);
std::cout << x << ", " << y; // 2, 1
}
32
Constants
Java’s final is const in C++
const unsigned speed_of_light = 299792458;
...
speed_of_light *= 2; // Compiler error
Const-References
Consider the code below. Can it be improved?
bool is_sorted(std::vector<int> v) {
for (unsigned i = 1; i < v.size(); ++i) {
if (v[i] < v[i-1])
return false;
}
return true;
}
Const-References
Yes: const-reference for efficiency (no copy) and safety (no mutation)
bool is_sorted(const std::vector<int>& v) {
// ... as before ...
}
std::vector<int> v(10'000'000);
...
if (is_sorted(v)) ...
35
Functions
Core syntax as in Java: R name(T1 p1, …, Tn pn) { body }
Functions
Variadic functions
double average(double values...) {
// compute average of given values
}
Operator Overloading
In contrast to Java, C++ supports overloading of operators
struct Rational { Rational r1(1, 3);
int n; int d; Rational r2(5, 7);
Rational(int _n, int _d) { n = _n; d = _d; }
std::cout << (r1 + r2);
};
https://en.cppreference.com/w/cpp/language/operators
39
Separate Compilation
40
Semantics of #include
main.cpp main.cpp (as compiled)
#include <file.cpp>
#include <rational.cpp> struct Rational {
int n; is replaced with the
int main() { compile int d; content of file.cpp
Rational r1 = ...;
... main.cpp Rational(int a, int b) {
}
n = a;
d = b;
Possible
rational.cpp } disadvantages?
};
struct Rational {
int n; ...
int d;
int main() {
Rational(int a, int b) { Rational r1 = ...;
n = a; ...
d = b; }
}
};
...
41
Preventing Re-inclusion
If rat.h is (transitively) included multiple times, symbols are redeclared
Avoid by using pre-processor Alternative using preprocessor
macros (not discussed further) directive (not discussed further)
to establish include guards: - Certain advantages over include guards
rat.h - Not supported by all compilers
#ifndef RAT_H
rat.h
#define RAT_H
#pragma once
struct Rat {
int n; struct Rat {
int d; int n;
int d;
Rat(int a, int b);
}; Rat(int a, int b);
};
// more declarations ...
// more declarations ...
#endif // RAT.H
44