Week 8 Class 13 Tue Mar 02 Posted: Mar 02

Class session:

  1. Announcements:
  1. Screencast: Callbacks: Function Pointers

    We will be looking at callbacks using lambda functions next class. They clean up a lot of the overhead of using function pointers.

  2. Exercise 34: Callbacks: Function Pointers Crossword
  3. Exercise 35: convert.cpp

    The following screencasts are a series of changes to the program convert.cpp that use callbacks with function pointers. The hexadecimal number in the title of each screencast is the commit id in the demonstration that I perform in the screencasts.

    The commit id can be (as it is here) a 6-character abbreviation of the full commit id, which is the SHA1 of the commit. For example, the full SHA1 of the first screencast of the series is bba4c0c802f5820039541dc746f3324527c96307.

    Git generates the commit id from the files’ contents, the commit message, author, date, and the commit id of the parent commit. In other words, your commit id will not be the same even if you make the same changes to the code.

    Code along and commit what I show in the screencast. The GitHub Classroom link is on Brightspace.

  4. Screencast: convert.cpp bba4c0c Extract output() for string

    Repository shown in the screencasts: convert-mlcollard

    No callbacks yet, just setting up.

  5. Screencast: convert.cpp d83006b Extract general myforeach() to replace output()

    Use a callback to replace specific output() with general myforeach().

    Note: There is already a std::for_each(). The myforeach() in this screencast is for demonstration purposes.

  6. Screencast: convert.cpp 6d3984c Extract myforeach() for changing strings

    Now we can use callbacks for myforeach(), which has multiple uses in the code.

  7. Screencast: convert.cpp 171ffef Apply conversion function pointer to separate option handling from conversion

    A function pointer variable allows more flexibility in the design.

  8. Screencast: convert.cpp 3f78fd6 Extract typedef for conversion function pointer

    Function pointer syntax for declaring a function is seldom used. In general, we use a typedef.

  9. Screencast: convert.cpp 032b044 Extract map for conversion from option to conversion function

    Now we get a data structure where we can map from a string to the appropriate function. Replaces the nested-if statement. For large nested-if statements, this is more time efficient as the find() of the map is a constant-time operation, i.e., O(1).

    In the screencast, there is a typo in the commit message. I fixed it after the screencast.

Screencast Folder: Class 13 Tue Mar 02

Exercise Due Date: 4 pm on Wednesday, Mar 03