From a0d503e8dacf7cf9f6c47fb2c3b612ce7bcec734 Mon Sep 17 00:00:00 2001 From: Alex Hirsch Date: Tue, 6 Oct 2020 12:34:06 +0200 Subject: [PATCH] Add exercises --- README.md | 9 +++++ exercises/README.md | 45 +++++++++++++++++++++++ exercises/images/task03_dependencies.dot | 14 +++++++ exercises/images/task03_dependencies.png | Bin 0 -> 8503 bytes exercises/task01/hello.cpp | 6 +++ exercises/task01/hello_boost.cpp | 11 ++++++ exercises/task02/vec.cpp | 23 ++++++++++++ exercises/task03/app/app.cpp | 10 +++++ exercises/task03/libBar/bar.cpp | 8 ++++ exercises/task03/libBar/bar.hpp | 6 +++ exercises/task03/libBaz/baz.cpp | 11 ++++++ exercises/task03/libBaz/baz.hpp | 6 +++ exercises/task03/libFoo/foo.cpp | 8 ++++ exercises/task03/libFoo/foo.hpp | 6 +++ 14 files changed, 163 insertions(+) create mode 100644 exercises/README.md create mode 100644 exercises/images/task03_dependencies.dot create mode 100644 exercises/images/task03_dependencies.png create mode 100644 exercises/task01/hello.cpp create mode 100644 exercises/task01/hello_boost.cpp create mode 100644 exercises/task02/vec.cpp create mode 100644 exercises/task03/app/app.cpp create mode 100644 exercises/task03/libBar/bar.cpp create mode 100644 exercises/task03/libBar/bar.hpp create mode 100644 exercises/task03/libBaz/baz.cpp create mode 100644 exercises/task03/libBaz/baz.hpp create mode 100644 exercises/task03/libFoo/foo.cpp create mode 100644 exercises/task03/libFoo/foo.hpp 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 0000000000000000000000000000000000000000..a8ccbeab1c4f0f1ab5bdc90ce34122c4835caa9c GIT binary patch literal 8503 zcmbt)1yq&ow(df@Q-MV)NT-0H2ndS?Y3WAkR=R5e0@5HzExHk zeeXGA+<)&o&N(m!wLaeO{pS13IiKg5D^y8A3Kt8C1%W_tWu(PbAP^*6@be`G8u)5o z+FAp?p&83bi9>GhJ~JBgVj&P(h>WSYv?Ax0c^ZBsam``>mp2%uB>~4UC%cw^e4BCg+R*Oiv#~!R4m7@ z+_lXVeQL6mRT1oLhK~}9*sUy0upvD|gJNM5HWHQKUlGcYR6iS$Vcm_$)i-5oZ2jN+ ziofo*FB|8_$yUB(8W=)`Oz~Ee&VAhCOiLS6^{37FHA2_(>eYAx&0J;4%&FN&N)??1 z&vV-3X}(~+e)Y_s%GaI4`FU@ikh}k;3b_`sZ8_;DJ8~B~qhzL1&4k_H>y1*wbp$^3z z4F*&6AIq|891qqDQMNBVO5cE+l#b!GNkE!NhvflFT$+V{aB(?-3IQjudmO<#x`acl+B8Ri~JDd9>c5Tz#n;vD|zYOV#jOW zot`&|A$;W-U@9sqsTbuH6&-tX^$z>o#C_oS%s@t z_kQHh`NYJ8y`y8{59U7A679;ik5q!R823Oe;Zp*e@MT%8~GeE;6r zJIxnfJc5d1GoStBouZDFq4$+JfWbM-KleC9t8m#6**&wjd@Z zrl>j+84?nb#m2Mwm3kKUa0J2(oD3bC)>o8*f&!JqN1<(PVt4y$YHCs{P*X{Sy*4!8 zh~C&RXVa;|QC3#o7%xg3Tb0hV`B^0`FW)heB}GWqBj|HN2#$YeuAWp>R8+IVoUkjB zhSTX;jN`j^FkM~Bi4vXQ;bEnMeuUWpyM~bc;j+Z<%^&D=QXe~JDy?!j)@$3^+a0zi z*gX$BNwujMpFf8pAt7njJF{P1UspbVFiy7M7_Y6a{-B|uVYt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PiEpYdyg1=_0 kpCDWtNjf3Yc?wq|-XXE#VT3G0;8`Iu5(?s_Vut?z0nUOuXaE2J literal 0 HcmV?d00001 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