diff --git a/README.md b/README.md index 4e1ec1b..1033944 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,15 @@ This includes things like: - Unnecessary waste of time or space (memory leaks) - … +## Exercises + +While the previous iterations of this course leveraged weekly exercise sheets, this one does not. +However, I would like to keep these exercises around as they support the lecture and explore certain auxiliary topics relevant to C++. +You are encouraged to solve (or at least think about) these exercises and participate in the discussions during the stream. +You will not submit a solution as these exercises are optional, but feel free to ask questions when running into problems. + +The [`exercises`](exercises) folder will be updated throughout the semester. + ## C++ Related Questions Break code specific questions down to a minimal example. diff --git a/exercises/README.md b/exercises/README.md new file mode 100644 index 0000000..a391f3d --- /dev/null +++ b/exercises/README.md @@ -0,0 +1,45 @@ +# Exercises + +## Task 01 + +Install G++ and Clang, then compile the provided file [`hello.cpp`](task01/hello.cpp). +Use the following flags when compiling: + + -std=c++17 -Wall -Wextra -O2 + +Next, set up [Boost](http://www.boost.org/) on your system and compile the provided file [`hello_boost.cpp`](task01/hello_boost.cpp). +Boost is quite common and provides you a set of useful C++ libraries. +Some of its content is even promoted into the C++ standard library. + +## Task 02 + +Run Clang on the provided file [`vec.cpp`](task02/vec.cpp) using the following command: + + clang -std=c++17 -Xclang -ast-dump -fsyntax-only -Wno-vexing-parse vec.cpp + +Clang will parse the input file and display its abstract syntax tree (AST). +In the bottom half of the output you'll find the function declaration of `main` followed by its `CompoundStmt`. +Take a close look at its children and compare the resulting AST with the input code. +Notice any oddities — something that looks counter intuitive? + +As you can see, there are multiple different ways of initialisation in C++. +Check out the [corresponding section at cppreference](https://en.cppreference.com/w/cpp/language/initialization). + +## Task 03 + +The directory [`task03`](task03) hosts four subdirectories, `libFoo`, `libBar`, `libBaz`, and `app`. + +Each folder prefixed with `lib` represents a library and contains a header plus a source file. +Furthermore, the library `libBaz.so` depends on `libBar.so`. + +`app` contains a single source file providing a `main` function. +It depends on all three libraries. + +![Dependency Graph](images/task03_dependencies.png) + +- Model this project structure using [CMake](https://cmake.org/) +- Be sure to set the C++ standard to C++17 and enable warnings (`-Wall -Wextra`) +- The default build type should be *Release* + +CMake itself is a build system generator. +You can choose from a variety of target build systems. diff --git a/exercises/images/task03_dependencies.dot b/exercises/images/task03_dependencies.dot new file mode 100644 index 0000000..b2fbc58 --- /dev/null +++ b/exercises/images/task03_dependencies.dot @@ -0,0 +1,14 @@ +digraph { + node [shape=box style=filled fillcolor=lightblue fontname="Roboto Mono" fontsize=12 ]; + edge [style=dashed arrowhead=onormal]; + + app; + foo [label="libFoo.so"]; + bar [label="libBar.so"]; + baz [label="libBaz.so"]; + + app -> foo; + app -> bar; + app -> baz; + baz -> bar; +} diff --git a/exercises/images/task03_dependencies.png b/exercises/images/task03_dependencies.png new file mode 100644 index 0000000..a8ccbea Binary files /dev/null and b/exercises/images/task03_dependencies.png differ diff --git a/exercises/task01/hello.cpp b/exercises/task01/hello.cpp new file mode 100644 index 0000000..8b27019 --- /dev/null +++ b/exercises/task01/hello.cpp @@ -0,0 +1,6 @@ +#include + +int main() +{ + std::cout << "Hello World" << std::endl; +} diff --git a/exercises/task01/hello_boost.cpp b/exercises/task01/hello_boost.cpp new file mode 100644 index 0000000..398e532 --- /dev/null +++ b/exercises/task01/hello_boost.cpp @@ -0,0 +1,11 @@ +#include + +#include + +int main() +{ + std::string s = "foo bar"; + boost::replace_all(s, "foo", "Hello"); + boost::replace_all(s, "bar", "World"); + std::cout << s << std::endl; +} diff --git a/exercises/task02/vec.cpp b/exercises/task02/vec.cpp new file mode 100644 index 0000000..28003cf --- /dev/null +++ b/exercises/task02/vec.cpp @@ -0,0 +1,23 @@ +class Vec3 { + public: + Vec3() = default; + Vec3(int x, int y, int z) : x(x), y(y), z(z) {} + + private: + int x = 0; + int y = 0; + int z = 0; +}; + +int main() +{ + Vec3 v0; + Vec3 v1(); + Vec3 v2(1, 2, 3); + Vec3 v3{1, 2, 3}; + Vec3 v4 = {1, 2, 3}; + + auto v5 = Vec3{}; + auto v6 = Vec3(1, 2, 3); + auto v7 = Vec3{1, 2, 3}; +} diff --git a/exercises/task03/app/app.cpp b/exercises/task03/app/app.cpp new file mode 100644 index 0000000..4100c9a --- /dev/null +++ b/exercises/task03/app/app.cpp @@ -0,0 +1,10 @@ +#include "bar.hpp" +#include "baz.hpp" +#include "foo.hpp" + +int main() +{ + foo(); + bar(); + baz(); +} diff --git a/exercises/task03/libBar/bar.cpp b/exercises/task03/libBar/bar.cpp new file mode 100644 index 0000000..affe187 --- /dev/null +++ b/exercises/task03/libBar/bar.cpp @@ -0,0 +1,8 @@ +#include "bar.hpp" + +#include + +void bar() +{ + std::cout << "Function bar called" << std::endl; +} diff --git a/exercises/task03/libBar/bar.hpp b/exercises/task03/libBar/bar.hpp new file mode 100644 index 0000000..b697005 --- /dev/null +++ b/exercises/task03/libBar/bar.hpp @@ -0,0 +1,6 @@ +#ifndef UIBK_703807_EX01_TASK3_BAR_HPP +#define UIBK_703807_EX01_TASK3_BAR_HPP + +void bar(); + +#endif // UIBK_703807_EX01_TASK3_BAR_HPP diff --git a/exercises/task03/libBaz/baz.cpp b/exercises/task03/libBaz/baz.cpp new file mode 100644 index 0000000..747c81f --- /dev/null +++ b/exercises/task03/libBaz/baz.cpp @@ -0,0 +1,11 @@ +#include "baz.hpp" + +#include + +#include "bar.hpp" + +void baz() +{ + bar(); + std::cout << "Function baz called" << std::endl; +} diff --git a/exercises/task03/libBaz/baz.hpp b/exercises/task03/libBaz/baz.hpp new file mode 100644 index 0000000..95e97e3 --- /dev/null +++ b/exercises/task03/libBaz/baz.hpp @@ -0,0 +1,6 @@ +#ifndef UIBK_703807_EX01_TASK3_BAZ_HPP +#define UIBK_703807_EX01_TASK3_BAZ_HPP + +void baz(); + +#endif // UIBK_703807_EX01_TASK3_BAZ_HPP diff --git a/exercises/task03/libFoo/foo.cpp b/exercises/task03/libFoo/foo.cpp new file mode 100644 index 0000000..b7f6ada --- /dev/null +++ b/exercises/task03/libFoo/foo.cpp @@ -0,0 +1,8 @@ +#include "foo.hpp" + +#include + +void foo() +{ + std::cout << "Function foo called" << std::endl; +} diff --git a/exercises/task03/libFoo/foo.hpp b/exercises/task03/libFoo/foo.hpp new file mode 100644 index 0000000..d90e74f --- /dev/null +++ b/exercises/task03/libFoo/foo.hpp @@ -0,0 +1,6 @@ +#ifndef UIBK_703807_EX01_TASK3_FOO_HPP +#define UIBK_703807_EX01_TASK3_FOO_HPP + +void foo(); + +#endif // UIBK_703807_EX01_TASK3_FOO_HPP