balluff2019-07hu
Monday July 22
- Initial Example to Introduce Online Compilation with "CoLiRu" (= Compile, Link, Run)
- TO BE DONE as exercise:
- Add a call to `Counter_increment` and check results
- Add a call to `Counter_reset` and check results
- Introduce another (second) `Counter` variable with different initialization and according checks for `Counter_increment` and `Counter_reset`
- See: http://coliru.stacked-crooked.com/a/548c99448daeaffb
- TO BE DONE as exercise:
- Demonstrate compiling with C by changing
- g++ -std=c++98 ... (or whatever) to
- gcc -xc -std=c89
- Show that "struct tag names" (i.e. the identifier after `struct` in C is only valid when preceeded by `struct` but in C++ is a type name on its own.
- Show that in C that effect (struct tag names are type names on their own) can also be achieved by adding a `typedef` for them.
- See: http://coliru.stacked-crooked.com/a/8a12bcae9f450b89 (full C89 compatibilty)
- Demonstrate compiling with C by changing
- See: http://coliru.stacked-crooked.com/a/5844291522a662e5 (full C90 compatibility)
- Also, the different include files (<cassert> vs. <assert.h>) would not be absolutely necessary. Currently C++ still accepts the C header files names too: http://coliru.stacked-crooked.com/a/f3009e850f4bae04
- TO BE DONE as an exercise:
- Turn the `struct Counter` into a `class Counter`
- Make the member data `private`
- Turn the current global functions into member functions of that class.This affects:
- `Counter_init`
- `Counter_increment`
- `Counter_reset`
- Besides using different names for them, you can get the idea from OOPCPP-12 (F7) and OOCPP-13 (F8).
- Turn the current global functions into member functions of that class.This affects:
- Because `current_value`now is not any longer accessible, you need another function. Maybe use the name `get` or `get_value` for it and simply hve it return the `current_value`.
- Adapt the test pogram with the assert statements accordingly.
You can eliminate tests for the `start_value` as any problems in this respect would turn up when testing `reset`. - Turn the `init`-function into a constructor. This requires
- Change its name to be the same as the class (i.e. `Counter`)
- Supply its argument directly where the object is defined,
- instead of:
Counter test_counter(123);
Counter::init(123); - use this:
Counter test_counter(123);
- instead of:
- For the constructor just added, make the argument optional by suplying `0` as default value. To test it change
- change: Counter z(0);
- into: Counter z;
No round parentheses here! (Explanation follows)
- Add a second `increment` function which takes an argument of type int.
- Use its value for how much a `Counter` object is incremented.
- Adapt the test program by using that function in the place where `z` is currently incremented in a loop.
- See: http://coliru.stacked-crooked.com/a/da72fe203e79d0c5
- A little Demo-Program to understand WHEN constructors and destructors are called depending on where objects are located:
http://coliru.stacked-crooked.com/a/420b2251257c6ad8- global outside any function
- local inside a function
- static inside a function
- with new on the heap
- TRY ALL THIS ONCE MORE YOURSELF NOW
- The main point to understand is this:
- Each object that gets constructed at some point ...
- ... will be destructed at a specific other point
- except for objects on the heap
- created with `new` (always held via a pointer)
- that must be explicitely destructed
- with `delete` (argument is the pointer holding the object)
- Use the following example to
http://coliru.stacked-crooked.com/a/b37e03f20ce9ffca- Understand the syntactical differences between pointers and references with respect to handing over an object of `MyClass` as argument to the function `test`.
- The various uses of `const` for
- a pointer argument (keeping what is pointed-to read-only)
- a reference argument (keeping what it refers to read-only)
- as a "marker" that a member function doesn't modify an object
- ADVANCED aspect:
Demonstrate that in the implementation of a member function marked as `const` (i.e. it promises not to change any data of its class) it is still possible to change data members qualified with the keyword `mutable`.
(One possible use of this is to "cache" data members calculated only when needed but then store the result for further use without re-calculation.)
- Short comparison of "OOP with C" versus"Classes in C++"
https://godbolt.org/z/foeC2C - Example extended to show automatic and explicit type conversions
http://coliru.stacked-crooked.com/a/bfa202e6fb49c72d - Support functions necessary to compile examples from chapters 03...
http://cpp.sh/97viq - Full example for object relation: Association:
http://cpp.sh/6gcrf
Wednesday July 24
- Considerations on "Proper Inhertiance"
https://isocpp.org/wiki/faq/proper-inheritance - Virtual in public interface
http://coliru.stacked-crooked.com/a/784ef655cf3a3056 - vs. NVI (Non-virtual interface = using extension points)
http://coliru.stacked-crooked.com/a/8ec5045f16050bdf - as before with many more (unused) extension points
http://coliru.stacked-crooked.com/a/4e0c2883ac1c7498/
Thursday July 25
- Exercise 3 from Chapter 13:
- (follow the instructions there)
- Optional Exercise for implementing state machines:
http://coliru.stacked-crooked.com/a/2ce5547b389e0960- Analyse how the above CoLiRu example implements a mechanism to recognize comments in C source code read character by character.
Verify the state machine- correctly handles .... /* .... */ .... as comment except
- when it is part of a string literal: ... "hello /*world*/" ...
- Which changes and/or addidtions are necessary to also correctly recognize and handle C++-style comments, beginning with // up to the end of line?
- Is there still a problem that needs fixing?
(HINT: what if a double quote in single quotes as character literal?)
- Analyse how the above CoLiRu example implements a mechanism to recognize comments in C source code read character by character.
- For comparison here is the same state machine implemented with switch-case:
http://coliru.stacked-crooked.com/a/ed748fdf8d2d3e3c - And here is the example from the presettation (chapter 9) with a little bit of re-formatting and also showing the new "using"-style type definiions from C++11.
coliru.stacked-crooked.com/a/b21c483ccdee8652
-
Which class implements the Interrupt Callback-Inerface that is then used to advance the two Clocks (one Clock and one AlamClock) via their common Interface `I_Clock`)
- Which structure (in which header file) describes the (low-level) register layout which is then accessed to
- configure the hardware timer (with respect to the frequency divider)
- start and stopp this timer
- At which adress in memory are the bits located, controlling the timer hardware?
- Which function is called every second directly from the interrup ttimer?
- Motivating the use of Singletons
http://coliru.stacked-crooked.com/a/e84b80cc365d144a (showing the problem)
TBD (and the solution) - http://coliru.stacked-crooked.com/a/c1153f4a4ac8df67
Friday July 26
- Using address arithmetic and casts as available in C to
- change private member data and
- call a virtual member function
http://coliru.stacked-crooked.com/a/e0d7df60988b0e20
- Template Class for Fixed Strings wth length set at compile time
- Class similar to std::vector but with storage NOT allocated on the heap
- starting point: http://coliru.stacked-crooked.com/a/f2472b51d2701903
- http://coliru.stacked-crooked.com/a/bd6020e003d8dfa7
- http://coliru.stacked-crooked.com/a/dcfc93ba1e3e7a57
- http://coliru.stacked-crooked.com/a/17905995a08db4b4