Dipl.-Ing. Martin Weitzel, 64380 Roßdorf bei Darmstadt

infineon2017

C++ Training in Villach (AU), May 2017

Latest Version of Presentation Including Workbook and Solutions

http://tbfe.de/WeTrain-Infineon_2017-05-26.zip

Links to Online Compiler Examples

Recommendable Online Compilers are (as of May 2017):

The links below may lead to a prepared example. It may be followed by a suggestion how to continue from there and which goal you might try to achieve. Then there is typically another link showing the fnal result.

Small Example for typedef, namespace and using Directives

Start with: http://coliru.stacked-crooked.com/a/e0a2e45bbc2ed2fa

Suggested continuation: Try some variations.

Demonstrate Helper Macro PX to Print Arbitrary Expressions

Start with: http://coliru.stacked-crooked.com/a/588e26da3f94f471

Suggested continuation: Try some variations, also with more complex expressions to print.

(Note: and extension to this was demonstrated on request Thursday Morning, solving the problem on commata contained in the edxpression handed over: http://coliru.stacked-crooked.com/a/442628ffdb2ebd09)

Demonstrate References and Pointers are the same at the Hardware Level

Using Compiler Exploerer: https://godbolt.org

You need to click on the link above to access the example - the actual target goes deeper as the URL shows!

Some Smaller Live Demos Used Tuesday Morning

  1. References may be checked for validity like pointers (= actually referring to some object) though this is against the spirit of references (at least if they are viewed as alias for existing variables or objects), not as "pointers in disguise":
    http://coliru.stacked-crooked.com/a/b7a82af8d26d9380
  2. Using references in a way that they always refer to existing variables or objects, may require to provide dummy variables at the border of an interface going from pointers (which may be NULL) to references:
    http://coliru.stacked-crooked.com/a/cd8dfde7db59203b
  3. Various forms and limits of overloading ased on function arguments (of different type): http://coliru.stacked-crooked.com/a/f73b5c3d4771fe4f

Commented Example for Various Typical Syntactical Constructs

  • Constructor
  • Member-Initialization List
  • Getters (with const)
  • Setters (without const)

Final result: http://coliru.stacked-crooked.com/a/308bb308267a9167

Demonstrate Necessity of Copy Constructor for Classes with Pointers

Start with: http://coliru.stacked-crooked.com/a/27010d323c9356f9

Suggested continuation: Demonstrate by enabling(disabling the already contained conditional compilations) that:

  • The copy c'tor created automatically will not do the right thing with the pointer member name;
  • The c'tor without arguments (aka. default c'tor) will not initialize the pointer member nam.

Apply Copy Constructor and (C++11) Move Constructor to Named Points

Start with: http://coliru.stacked-crooked.com/a/ec272a9c629dcd96

Suggested continuation: Add the yet missing implementation of the Move Assigment

Final result: http://coliru.stacked-crooked.com/a/1c6e93b15a7d6eff

Note: the final result also splits the class point originally just directly extended to hold also a name into a base class point (without a name) and a derived class named_point (adding the name).

Modify virtual print Member Function of Class point

Start with: http://coliru.stacked-crooked.com/a/fbfd8f806ce2683f

Suggested continuation: in member function print of the base class point call another (new) virtual member function to provide an "extension point" for derived classes. The base class implementation should just return an empty string (as const char*).

In the derived class named_point override that (new) function with one that returns the name member. 

Final goal: There should be only one non-virtual print member function (in class point), so that e.g. if you want to change that is common to all printing of a point (like replace curly braces that enclose the x and y value with round parentheses) you can do in in a single location of the code.

Final result: http://coliru.stacked-crooked.com/a/d48f9b67ab45e9cc

Generically Printing Native Arrays with a Template Function

Start with: http://coliru.stacked-crooked.com/a/b51b282f3946cb19

Suggested continuation: Complete the print_helper so that the function print works for arrays of any size and element type. 

Final result: http://coliru.stacked-crooked.com/a/63685f1ea3f74547

Simplify Code by Turning a Native Pointer into an std::un​ique_ptr

Start with: http://coliru.stacked-crooked.com/a/ddaeca979ba2938b

Instead of const char* for the name data member use:

  std::unique_ptr<const char[]> name;

Remove the destructor of class named_point completely, replace the current implementation of the move constructur and move assignment with =default and make all other necessary adaptions (which mainly means turning name into name.get() where it is still used as char pointer).

Intermediate result: http://coliru.stacked-crooked.com/a/4e8de510cee3ea56

If C++11 is not an option, a similar functionality (as offered by std::unique_ptr) is available with

  • boost::scoped_ptr and
  • boost::scoped_array

There are little changes to the previous code to support this alternatively.

Next intermediate result: http://coliru.stacked-crooked.com/a/3f06457f5f07df2f

Next sugested continuation: Change type of member name (of class named_point) to std::string.

Combining getline with istringstream-s to Process Text Files

(Note: the idea was demonstrated in a live demo Thursday afternoon: http://coliru.stacked-crooked.com/a/59e377d79da11cfe)

Easily Extendable Parser for Simple Syntax Analysis

(Note: the idea is based on a simpler version of this principle as has been developed in a live demo Thursday afternoon: http://coliru.stacked-crooked.com/a/ba196431db86e7f2)

Start with: http://coliru.stacked-crooked.com/a/076a4c15d62eec36

Suggested continuation: Locate the lines containing

  • syntax_is(s >> … )

and further analyze the program from here, then add some other syntax forms (e.g. for adding two values).

Using Different Format Settings for Same Output Stream

(Note: the idea is based on a simpler version of this principle as has been developed in a live demo Thursday afternoon: http://coliru.stacked-crooked.com/a/d6bcf02d25921bba)

Start with: http://coliru.stacked-crooked.com/a/7794c546bac3a704

Suggested continuation: Try some other calls of function ascii_table as already contained in main (in cmments).

Some Algorithms by Demonstrated Live

Start with: http://coliru.stacked-crooked.com/a/2caf2c16161a474f#

A Nice Video on Using (some) Algorithms

https://channel9.msdn.com/Events/GoingNative/2013/Cpp-Seasoning

The interresting part - with respect to some algorithms - starts at minute 8:40 and takes about 15 minutes and is named: No Raw Loops