Kurt Nørmark
Department of Computer Science, Aalborg University
Abstract Previous lecture Next lecture Index References Contents | In this lecture we discuss selected, basic facilities in C++, as covered in Part 1 of The C++ Programming Language (version 3). |
Basic facilities - in this lecture Slide Contents Index References |
|
|
|
Lambda expressions Slide Contents Index References |
|
|
|
Program: Different forms of lambda expressions. |
|
|
Program: The examples from above in context. |
|
Program: More forms - mutable and explicit return type. |
|
Program: An immediate call of a lambda expression. |
|
Program: Lambda expressions and recursive calls. |
|
Program: A function that returns a function - closure - problems. |
|
Program: Same in Scheme - where this kind of stuff works out of the box. |
|
Program: A function that returns a function - OK. |
|
Exercise 3.2. Get some experience with lambda expressions in C++ | This is an open exercise about lambda expressions in C++. Overall, the purpose is to get some practical experience with lambda expressions in C++, based on our knowledge of lambda expressions in functional programming languages. One way to go: Write some small C++ programs that illustrate the various forms of capture as described in section 11.4.3 (page 293) in The C++ Prog. Lang. (4. edition). Another way to go: Make some experiments with mapping, filtering, and accumulation supported by higher-order C++ functions and C++ lambda expressions. Feel free to use vectors, or another kind of container for your experiments. You may find inspiration in the PP lecture about higher-order functions. A third way to go: Functions cannot be nested in C++. Do some research on nesting of lambda expression into each other. Feel free to choose your own experiments! |
Type conversion - Grand Overview Slide Contents Index References |
|
|
|
|
|
Item 2 of More Effective C++ has some basic arguments in favor of 'the new C++ casts' in contrast to C-style casts. |
Implicit type conversions Slide Contents Index References |
|
|
|
Explicit type conversion Slide Contents Index References |
|
|
|
Probably the most frequently used, see item 2 of More Effective C++ |
Examples of casting Slide Contents Index References |
|
Program: Examples of static casting in C++. |
|
Program: The compilable parts of the program from above. |
|
Program: Example of dynamic casting in C++. |
|
Program: The compilable parts of the program from above. |
|
|
Program: dynamic_cast is a predicate - it tests if a pointer is of the appropriate type. |
|
|
Program: Examples of reinterpret_cast. |
|
Program: The compilable parts of the program from above. |
|
Function Overloading Slide Contents Index References | Intern kommentar til forelæsningen om denne slide: Dybest set ikke et særligt interessant emne. Teknikaliteterne bør ikke tage for meget tid. Koncentrer dig om det overordnede. Gå dog gennem eksemplerne to slides længere fremme. |
|
|
|
|
Function Overloading - more detailed rules Slide Contents Index References |
|
|
|
|
Function Overloading - Examples Slide Contents Index References | Intern kommentar til forelæsningen om denne slide: Illustrerer mulige tvetydigheder ved overloading. Ret så ligefrem. Tag dig tid til at illustrere. |
|
Program: Exact matches - a trivial example. |
|
Program: Simple examle of an ambiguity. |
|
Program: An ambiguity between 'float to int' and 'float to long int'. |
|
Program: 'Float to double' conversion prefered over 'float to int' and 'float to long int'. |
|
Program: Point.h. |
|
Program: double to Point via Point(double) constructor. |
|
Program: 'double to char' instead of 'double to Point'. |
|
Program: Now in an ambiguity situation. |
|
Program: A single best match again - slightly surprising perhaps. |
|
Program: A trivial example with overloading of a function of two parameters. |
|
C-style text strings Slide Contents Index References |
|
|
|
|
|
C++ style text strings Slide Contents Index References |
|
|
|
|
|
Strings - examples Slide Contents Index References | Intern kommentar til forelæsningen om denne slide: Knold og tot eksemplerne illustrerer værdisemantik på C++ strenge - og det modsatte på C strenge Det sidste store eksempel viser en del C++ string funktioner. Tal hurtigt hen over den. |
|
|
Program: The Knold & Tot example from section 20.3.6 of 3rd version of the C++ book. |
|
Program: The Knold & Tot example with char*. |
|
Program: The Knold & Tot example with char[]. |
|
Program: The Knold & Tot example with strcpy from <cstring>. |
|
Program: The Knold & Tot example with char* aliasing. |
|
|
Program: Illustration of various basic string operations in C++. |
|
Program: Same - with C++ initializations. |
|
Program: Program output. |
|
Exercise 3.3. String functions | Continue the programming from above with a C++ program that uses the find, replace, substr, and size functions from The C++ Prog. Lang. (4. edition) page 1045 - 1048. |
Vectors in C++ Slide Contents Index References |
|
|
|
|
|
|
Exercise 3.4. Understand Vectors | Take a look at the documentation of the (template) class vector at www.cplusplus.com Familiarize yourself with the vector member functions, and read some of the (numerous) small examples which are available |
Vectors - examples Slide Contents Index References |
|
|
Program: A simple vector example - similar to first array program. |
|
Program: A variant of the simple vector example - non-executable. |
|
Program: A variant of the simple vector example - executable. |
|
Program: Another vector example - constructors, insertion, lexicographic comparison. |
|
Program: Program output. |
|
The free store Slide Contents Index References |
|
|
|
|
Program: Illustration of free store. |
|
|
Input and output in C++ Slide Contents Index References |
|
|
|
|
|
Overloaded operators and IO Slide Contents Index References |
|
|
|
|
Standard streams - and files Slide Contents Index References |
|
|
|
|
|
Program: Opening, working on, and closing a file - as simple as possible. |
|
Stream State Slide Contents Index References |
|
|
|
|
Program: Illustration of implicit stream conversion to a boolean value - and stream states. |
|
Manipulators Slide Contents Index References |
|
|
|
Program: Output manipulators. |
|
|
Program: The fragment in a complete program. |
|
Program: Program output. |
|
Program: A variant of the program above - without showbase. |
|
Program: Program output from the variant. |
|
More manipulators Slide Contents Index References |
|
|
Program: Illustration of more manipulators. |
|
Program: Program output. |
|
Program: Programming our own manipulator - with two parameters. |
|
Program: Program output. |
|
Exercise 3.5. Understand the way we program and use output stream manipulators | Study the program on the accompanying slide in which we program our own stream manipulator. First, read through the program (do it together with fellow students) and understand what happens. Next, use the pattern to write a couple of new stream manipulators. If possible, make some which are more useful than the one I wrote. Maybe manipulators that affect the state of the stream, instead putting characters to the stream. I recommend that you take a look at page 1087 in The C++ Prog. Lang. (4. edition). |
Logical program organization Slide Contents Index References | Intern kommentar til forelæsningen om denne slide: Programmet på denne side illustrerer rigtig fint hvad forskellen er på using directives and declarations. Gennemgå den blot detaljeret - navn for navn. Derved bliver forskellen mellem de to gjort klar. |
|
|
|
|
|
Program: Illustration of using declarations and using directives - by example. |
|
Program: Compilable variant of the program from above. |
|
|
More namespaces Slide Contents Index References |
|
|
|
|
Program: Illustrating that a namespace interface is separated from the namespace implementation. |
|
|
Namespace resolution Slide Contents Index References |
|
|
|
Physical program organization Slide Contents Index References |
|
|
|
|
|
|
Example of program organization Slide Contents Index References |
|
Program: The header file point.h. |
|
Program: The header file tripple.h. |
|
Program: The header file point.cpp. |
|
Program: The header file tripple.cpp. |
|
Program: The header file main.cpp. |
|
Program: Compilation - README. |
|
Example of program organization - with namespace Slide Contents Index References |
|
Program: The header file point.h. |
|
Program: The header file tripple.h. |
|
Program: The header file point.cpp. |
|
Program: The header file tripple.cpp. |
|
Program: The header file main.cpp. |
|
Collected references Contents Index |
|
Chapter 3: Basic facilities, Part 2
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: August 1, 2017, 13:26:42