Add exercises
This commit is contained in:
parent
3ef3f4957e
commit
a0d503e8da
@ -15,6 +15,15 @@ This includes things like:
|
|||||||
- Unnecessary waste of time or space (memory leaks)
|
- 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
|
## C++ Related Questions
|
||||||
|
|
||||||
Break code specific questions down to a minimal example.
|
Break code specific questions down to a minimal example.
|
||||||
|
45
exercises/README.md
Normal file
45
exercises/README.md
Normal file
@ -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.
|
14
exercises/images/task03_dependencies.dot
Normal file
14
exercises/images/task03_dependencies.dot
Normal file
@ -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;
|
||||||
|
}
|
BIN
exercises/images/task03_dependencies.png
Normal file
BIN
exercises/images/task03_dependencies.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.3 KiB |
6
exercises/task01/hello.cpp
Normal file
6
exercises/task01/hello.cpp
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::cout << "Hello World" << std::endl;
|
||||||
|
}
|
11
exercises/task01/hello_boost.cpp
Normal file
11
exercises/task01/hello_boost.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <boost/algorithm/string/replace.hpp>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::string s = "foo bar";
|
||||||
|
boost::replace_all(s, "foo", "Hello");
|
||||||
|
boost::replace_all(s, "bar", "World");
|
||||||
|
std::cout << s << std::endl;
|
||||||
|
}
|
23
exercises/task02/vec.cpp
Normal file
23
exercises/task02/vec.cpp
Normal file
@ -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};
|
||||||
|
}
|
10
exercises/task03/app/app.cpp
Normal file
10
exercises/task03/app/app.cpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "bar.hpp"
|
||||||
|
#include "baz.hpp"
|
||||||
|
#include "foo.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
foo();
|
||||||
|
bar();
|
||||||
|
baz();
|
||||||
|
}
|
8
exercises/task03/libBar/bar.cpp
Normal file
8
exercises/task03/libBar/bar.cpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include "bar.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void bar()
|
||||||
|
{
|
||||||
|
std::cout << "Function bar called" << std::endl;
|
||||||
|
}
|
6
exercises/task03/libBar/bar.hpp
Normal file
6
exercises/task03/libBar/bar.hpp
Normal file
@ -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
|
11
exercises/task03/libBaz/baz.cpp
Normal file
11
exercises/task03/libBaz/baz.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "baz.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "bar.hpp"
|
||||||
|
|
||||||
|
void baz()
|
||||||
|
{
|
||||||
|
bar();
|
||||||
|
std::cout << "Function baz called" << std::endl;
|
||||||
|
}
|
6
exercises/task03/libBaz/baz.hpp
Normal file
6
exercises/task03/libBaz/baz.hpp
Normal file
@ -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
|
8
exercises/task03/libFoo/foo.cpp
Normal file
8
exercises/task03/libFoo/foo.cpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include "foo.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void foo()
|
||||||
|
{
|
||||||
|
std::cout << "Function foo called" << std::endl;
|
||||||
|
}
|
6
exercises/task03/libFoo/foo.hpp
Normal file
6
exercises/task03/libFoo/foo.hpp
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user