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 Slide Annotated slide Contents Index References |
|
|
Fundamental types i C++ Slide Annotated slide Contents Index References |
|
|
|
Booleans Slide Annotated slide Contents Index References |
|
|
|
|
Structs in C++ Slide Annotated slide Contents Index References |
|
|
|
Declarations and definitions Slide Annotated slide Contents Index References |
|
|
|
Program: Illustration of declarations and definitions - everything is fine. |
|
Program: Illustration of declarations and definitions - several problems. |
|
|
|
The structure of a declaration Slide Annotated slide Contents Index References |
|
|
Program: Examles of declarations. |
|
|
Declaring several names together Slide Annotated slide Contents Index References |
|
|
Program: Examles of declarations of multiple names. |
|
|
Declarations as statements - declarations in conditions Slide Annotated slide Contents Index References |
|
|
|
Program: Declarations before statements - C89 Style. Some words about compilation of these programs: I use g++ to compile C++ programs.
In most examples in this material, C++ files have file extension cc. Alternatively, cpp is also a popular file extension for C++ files.
If gcc is activated on a cc or cpp file, it will activate the C++ compiler. To prevent this, use the compiler option -x c.
Thus, to compile this file as a C89 c program, write gcc -x c -std=c89 -pedantic less-localized.cc |
|
Program: A declaration is a statement in C++. |
|
Program: A declaration in the condition of an if. |
|
Constants Slide Annotated slide Contents Index References |
|
|
|
Program: Examles constants, pointers to constants, and constant pointers. |
|
The general notation of objects in C++ Slide Annotated slide Contents Index References |
|
The concept object: An object is "something in memory" |
|
|
|
Lvalues Slide Annotated slide Contents Index References |
The concept object: An object is "something in memory" | ||
The concept lvalue: An lvalue is an expression that refers to an object | Thus, an lvalue is an expression that refers to a memory location. You can take the address of that memory location with the address operator. |
|
|
Program: Examples of lvalues. |
|
C-style strings Slide Annotated slide Contents Index References |
|
|
|
|
C++ style strings Slide Annotated slide Contents Index References |
|
|
|
Strings - examples Slide Annotated slide Contents Index References |
|
|
Program: The Knold & Tot example from section 20.3.6 of The C++ Programming Language. |
|
Program: The Knold & Tot example with char*. |
|
Program: The Knold & Tot example with char[]. |
|
Program: The Knold & Tot example with strcpy from <cstring>. |
|
|
Program: Illustration of various basic string operations in C++. |
|
Program: Program output. |
|
Exercise 2.2. String functions | Continue the programming from above with a C++ program that uses the find, replace, substr, and size functions from The C++ Programming Language section 20.3.11 - 20.3.14. |
References Slide Annotated slide Contents Index References |
|
|
Program: C++ References. |
|
|
|
|
|
Rules for references Slide Annotated slide Contents Index References |
Program: C++ References. |
|
|
|
|
References - Examples Slide Annotated slide Contents Index References |
|
Program: A variable becomes a reference to another variable. |
|
Program: No operator operates on a reference as such. |
|
Program: A function with reference parameters and reference return type. |
|
Program: Convenient references to long/deep fields. |
|
Exercise 2.4. Another example/exercise with C++ references | On the accompanying slide we have shown a number of examples that illustrate different uses of reference types in C++. Your task in this exercise is to come up with yet another good and convincing example of reference types in C++. (Please use your own fantasy - not some internet fantasy). Your program can be seen as the solution to a programming exercise about references. Please provide a short formulation of this exercise. |
Exercise 2.4. Pointers in combination with references | In this exercise we will explore pointers and references relative to each other. Play and learn! We do this in context of a function that returns the maximum of two doubles. Here is the function with call-by-value parameters: double max(double a, double b){ return a < b ? b : a; } In all cases below, program the max function, call it, make make sure you get the expected result from the call.
Can you imagine other interesting variations? |
|
Constant References Slide Annotated slide Contents Index References |
|
|
Program: Const ref scheme. |
|
It would be undesirable and misleading to be able to modify var, because it is just a temporary variable. |
Program: The initialization of var in a real, but simple context. |
|
Program: A similar program that initializes a user defined struct via a 'functional casting' constructor. |
|
Program: An envelope around the stdlib div function that returns a struct of quotient and remainder. |
|
Exercise 2.5. Ways of returning results from the div function | This exercise works in the context of the divide program from the slide about constant references. Program the struct ldiv_t and the function div yourself, instead of using the definitions from the std namespace. Run the program with your own versions of ldiv_t and div. Experiment with returning a reference ldiv_t& from div. (See page 282-283 in The C++ Programming Language). And similarly, experiment with returning a const reference const ldiv_t& from the div. |
Program: A similar setup - illustrates that it is not good to return a reference to a deallocated local variable. |
|
References versus Pointers Slide Annotated slide Contents Index References |
|
Program: Two versions of swap - with references and with pointers. |
|
|
|
Parameter passing in C++ Slide Annotated slide Contents Index References |
|
|
Value return Slide Annotated slide Contents Index References |
|
|
|
Type conversion - Grand Overview Slide Annotated 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 Annotated slide Contents Index References |
|
|
|
Explicit type conversion Slide Annotated slide Contents Index References |
|
|
Probably the most frequently used, see item 2 of More Effective C++ |
Function Overloading Slide Annotated slide Contents Index References |
|
|
|
Function Overloading - more detailed rules Slide Annotated slide Contents Index References |
|
|
|
Function Overloading - Examples Slide Annotated slide Contents Index References |
|
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. |
|
Vectors in C++ Slide Annotated slide Contents Index References |
|
|
|
|
Exercise 2.6. 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 Annotated slide Contents Index References |
|
|
Program: A simple vector example - similar to first array program. |
|
Program: Another vector example - constructors, insertion, lexicographic comparison. |
|
Program: Program output. |
|
The free store Slide Annotated slide Contents Index References |
|
|
|
Program: Illustration of free store. |
|
|
Input and output in C++ Slide Annotated slide Contents Index References |
|
|
|
Overloaded operators and IO Slide Annotated slide Contents Index References |
|
|
|
Standard streams Slide Annotated slide Contents Index References |
|
|
|
Stream State Slide Annotated slide Contents Index References |
|
|
|
Program: Illustration of implicit stream conversion to a boolean value. |
|
|
Manipulators Slide Annotated slide Contents Index References |
|
|
Program: Output manipulators. |
|
Program: The fragment in a complete program. |
|
Program: Program output. |
|
More manipulators Slide Annotated slide Contents Index References |
|
|
Program: Illustration of more manipulators. |
|
Program: Program output. |
|
Logical program organization Slide Annotated slide Contents Index References |
|
|
|
|
|
Program: Illustration of using declarations and using directives. |
|
More namespaces Slide Annotated slide Contents Index References |
|
|
|
Program: Illustrating that a namespace interface is separated from the namespace implementation. |
|
|
Physical program organization Slide Annotated slide Contents Index References |
|
|
|
|
Example of program organization Slide Annotated 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. |
|
The standard library namespace Slide Annotated slide Contents Index References |
|
|
|
Point Exercise - C++ versus C# Slide Annotated slide Contents Index References |
|
Program: The C# Point class. |
|
Program: A C# client class of class Point. |
|
Program: A C# client class of class Point - with comments that reveal the output. |
|
Exercise 2.7. Use of class Point in C++ | In this exercises we will explore some basic similarities and differences between use of classes in Java/C# and C++. (In the scope of this lecture we will NOT be concerned with class definition details. This belongs to the next lecture). In particular we will study creation/allocation of objects, and parameter passing of objects to a function/method. The starting point is the simple Point class in C# and a sample C# client program that creates and operates on points. Make sure that you familiarize yourself with the correct output of this program. Predict it before you run it. We provide a similar Point class in C++. The C++ definitions of the Point functions are also provided (but they are not important for this exercise). You are supposed to explore various uses of class Point in C++.
Feel free to do other experiments based on the Point classes that help you understand the similarities and differences between use of C# and C++ classes, pointers, references, and stack allocation of C++ objects. |
Program: The C++ Point class - a header file. |
|
Program: The C++ Point class - the cc file. |
|
Program: The C++ client program with pointers - free store allocation. |
|
Program: The C++ client program with pointers - stack allocation. |
|
Program: The C++ client program with references. |
|
Program: The C++ client program with Point value semantics. |
|
Collected references Contents Index |
|
Chapter 2: Basic facilities
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: March 26, 2013, 13:03:15