This repo documents the transition of the third CS3 lab exercises from Java to C++.
This repo documents the transition of the third Computer Science lab exercises from Java to C++.
The Course is about programming concepts rather than a specific language.
The Course is about programming concepts rather than a specific language.
## Changes
## Changes
### Worksheet 2
**Background**
-`commandlib.h` was introduced as a stand in for `hsrt.mec.controldeveloper.core.com.command`. For simplicity everything is defined in one header.
- Diverging from the class diagram the other interfaces dont inherit from `ICommand` as that would leed to weird inheritance loops where e.g. `Gear` inherits from `Command` which inherits from `ICommand`, but also from `IDirection` which also already inherits from `ICommand`. Maybe the class diagram could be simplified if `ICommand` would be replaced by the partially virtual `Command`.
**Commands**
-`Gear`, `Pause` and `Direction` use multiple inheritance to allow the use of interfaces.
This is fine, but it should be good practice to inherit from one base class only and only virtual classes otherwise.
**CommandList:**
-`CommandList` now needs a default constructor for `Element` because `Command` is now virtual and can no longer be use for the root `Element`.
-`CommandList` and `Element` use template functions to allow adding multiple types of `Command`s. This sort of is compile-time polymorphism.
- There is no checking if the template got the right type though. Possibly there is a solution using `static_assert` and `std::is_...`.
- For variable types templates should use `typename` rather than `class`.
**Class vs Member variables**
- To call a static function without an object it is necessary to use the namespace operator `MyClass::foo()` rather than `MyClass.foo()`.
**Doxygen**
- see Trivia
### Worksheet 1
### Worksheet 1
**Element:**
**Element:**
- In Element every mention of Element is replaced with `std::shared_ptr<Element>`.
- In Element every mention of Element is replaced with `std::shared_ptr<Element>`.
...
@@ -20,21 +37,60 @@ The Course is about programming concepts rather than a specific language.
...
@@ -20,21 +37,60 @@ The Course is about programming concepts rather than a specific language.
-`getPos()` now returns `std::make_shared<Command>`.
-`getPos()` now returns `std::make_shared<Command>`.
- Where appropriate the funtion parameter `int` has been replaced with `unsigned int` to prohibit passing negative values.
- Where appropriate the funtion parameter `int` has been replaced with `unsigned int` to prohibit passing negative values.
- In `add(Command cmd)``std::make_shared<Element>(cmd)` is used to create a new `Element`.
- In `add(Command cmd)``std::make_shared<Element>(cmd)` is used to create a new `Element`.
Here is easily visible how std::make_shared<T>() is the C++ equivalent to Java`s `new` operator.
Here is easily visible how std::make_shared<T>() is the C++ equivalent to Java's `new` operator.
- The `std::bad_alloc`-Exception possibly thrown by `std::make_shared<Element>()` is not catched in `add()`, to not introduce `try{} catch{}` too early.
- The `std::bad_alloc`-Exception possibly thrown by `std::make_shared<Element>()` is not catched in `add()`, to not introduce `try{} catch{}` too early.
Thus add() wont return `nullptr`, but crash the program if an error occures.
Thus add() wont return `nullptr`, but crash the program if an error occures.
## Trivia
## Trivia
### inline keyword
Although `inline` can be used to influence performance, but this is
[not how it should be used](https://www.youtube.com/watch?v=GldFtXZkgYo).
Much rather it can be used to deal with linking declarations over multiple translation units.
For variables it is a way to give the compiler ["a place and a time"](https://www.youtube.com/watch?v=m7hwL0gHuP4) to initialize a static variable.
### smart pointers
### smart pointers
- smart pointers point to NULL on construction.
- smart pointers point to NULL on construction.
- They can easily be checked for content with
- They can easily be checked for content with
```
if (mySmartPointer)
if (mySmartPointer)
//has content
//has content
else
else
//is empty
//is empty
```
- A unique pointer will be destroyed at the end of it's scope.
- A unique pointer will be destroyed at the end of it's scope.
- A smart pointer will be destroyed when all of it's references are overwritten or the scope of the last reference ends.
- A smart pointer will be destroyed when all of it's references are overwritten or the scope of the last reference ends.
\ No newline at end of file
### std::optional or "return a value or null"
One task when implementing the linked list is to return the Command modified Element.
Or, if for whatever reason the operation fails to return null to signal an error to the user of the list.
This isnt possible right away in C++ as null cant be casted to any type.
To solve this problem in a concise and easy way `std::optional<T>` was introduced in C++17.
It contains not only its given type T, but also a boolean to keep track if it is empty.