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 |
|
|
|
Fundamental types i C++ Slide Contents Index References |
|
|
|
Booleans Slide Contents Index References | Intern kommentar til forelæsningen om denne slide: Hurtig hen over |
|
|
|
|
|
Structs in C++ Slide Contents Index References | Intern kommentar til forelæsningen om denne slide: Hurtig hen over |
|
|
|
|
|
Declarations and definitions Slide Contents Index References | Intern kommentar til forelæsningen om denne slide: Vigtig sondring - eksempel programmet gennemgås omhyggeligt. Ikke svært. |
|
|
|
|
Program: Illustration of declarations and definitions - everything is fine. |
|
Program: Illustration of declarations and definitions - several problems. |
|
|
|
The structure of a declaration Slide Contents Index References | Intern kommentar til forelæsningen om denne slide: Erfaringsmæssigt er dette nogle ting, som gør det svært for folk at læse C++ programmer. Allerede berørt i C delen af kurset. Frisk op, og vær sikker på at folk er med. |
|
|
|
Program: Examles of declarations. |
|
|
Program: More examples of declarations. |
|
Declaring several names together Slide Contents Index References |
|
|
|
Program: Examles of declarations of multiple names. |
|
|
Declarations as statements - declarations in conditions Slide Contents Index References | Intern kommentar til forelæsningen om denne slide: Ganske ligefrem stof... |
|
|
|
|
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. |
|
The general notation of objects in C++ Slide Contents Index References |
|
The concept object: An object is "something in memory" |
|
|
|
|
|
Lvalues Slide Contents Index References |
The concept object: An object is a contiguous region of storage - "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. |
|
Rvalues Slide Contents Index References |
The concept rvalue: An rvalue is an expression that is not an lvalue - an expression whose value does not have a fixed position in memory, such as a temporary object | An rvalue can be the result returned by a function, or the value of a literal expression. |
|
|
Program: Examples of rvalues. |
|
Program: The rvalue example - in a bit of context. |
|
|
|
C++ References |
References Slide Contents Index References |
|
|
Program: C++ References. |
|
|
|
|
|
|
|
|
|
Rules for references Slide Contents Index References | Intern kommentar til forelæsningen om denne slide: ... |
Program: C++ References. |
|
|
|
|
References - Examples Slide Contents Index References |
|
Program: A variable becomes a reference to another variable. |
|
Program: No operator operates on a reference as such. |
|
Program: A typical use of references: A number of results passed back from vector_min_max via references. |
|
Program: A function that attempts to return references to local variables. |
|
Program: Ditto - handling the problem by making the local variable static. |
|
Program: A function with reference parameters and reference return type. |
|
Program: Convenient references to long/deep fields. |
|
|
Exercise 2.3. 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.3. 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. Please be aware that the cases become increasingly strange...
Can you imagine other interesting variations? |
Constant References 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 const reference var from rvalue, with type conversion. |
|
Program: A similar program that initializes a user defined struct via a 'functional casting' constructor. |
|
Program: Use of a const reference in the vector_min_max function (alread encountered in this lecture). |
|
Program: Illustrates that it is not good to return a const reference to a deallocated local variable. |
|
Lvalue References versus Pointers Slide Contents Index References |
|
Program: Two versions of swap - with references and with pointers. |
|
|
|
|
Parameter passing in C++ Slide Contents Index References |
|
|
|
Rvalue references Slide Contents Index References |
|
|
|
|
Program: Illustration of Rvalue references - in contrast to other kinds of references. |
|
Program: The compilable parts of the program from above. |
|
|
Value return Slide Contents Index References |
|
|
|
|
Three kinds of references Slide Contents Index References |
|
|
|
Lvalue references versus Rvalue references Slide Contents Index References |
|
Program: Two versions of swap - with Lvalue references and Rvalue references. |
|
|
|
|
Deduction of types with auto Slide Contents Index References |
|
|
|
Program: Sample use of auto in C++11. |
|
Program: Same - without std:: - with using namespace std. This example is not really important for the discussion of auto. It illustrates the burden of using fully quallified names with namespace prefix. |
|
Program: Same - illustrates deduction of a functions return type in C++14. |
|
|
Deduction of types with decltype Slide Contents Index References |
|
|
|
Program: Trivial uses of decltype. |
|
|
|
Program: A sample use of decltype in a function template: matrix plus. |
|
Program: Another sample use of decltype in a function template: container example. |
|
Initialization Slide Contents Index References |
|
|
|
Program: Four forms of initialization. |
|
|
|
Initialization examples Slide Contents Index References |
|
Program: Default initialization. |
|
Program: Initialization and narrowing. |
|
Program: Initialization and auto - problems. |
|
Program: Initialization and auto - OK. |
|
Program: Initialization - ambiguities. |
|
C++ ISSUE: Constness Slide Contents Index References |
|
|
|
Constants Slide Contents Index References | Intern kommentar til forelæsningen om denne slide: Vær sikker på at få detaljerne helt på plads i programmet. Ikke mindst de to (dobbelt) erklæringer af w, hvor Point const og const Point er ækvivalente. |
|
|
|
|
|
|
Program: Examles constants, pointers to constants, and constant pointers. |
|
Program: Only the valid parts of the program from above - compiles. Intern kommentar til forelæsningen om denne slide: Do not spend time on this during the lecture |
|
Program: point.h - just for reference. Intern kommentar til forelæsningen om denne slide: Don't go into this... |
|
Program: point.cc - just for reference. |
|
Point Exercise - C++ versus C# 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.4. Contrasting use of C# and C++ classes: Class Point | 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 a forthcoming 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. On purpose, I do not use C# properties - but simple accessor methods (ala Java). 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. |
Collected references Contents Index |
|
Chapter 2: Basic facilities, Part 1
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:25:38