Académique Documents
Professionnel Documents
Culture Documents
- Vertraulich / Confidential -
Session 6 – 6.09.2017 .........................................................................................................24
1. Multiple inheritance .......................................................................................................24
2. Initializer – table (old/new) ............................................................................................24
Session 7 – 3.11.2017 .........................................................................................................26
1. Pointer to functions (C-style) .........................................................................................26
2. Auto ..............................................................................................................................26
3. Polymorphism ...............................................................................................................26
3.1. Pointers to base class – pointer to derived class type-compatible with a pointer to its
base class. ....................................................................................................................27
3.2. Virtual members .....................................................................................................28
3.3. Abstract base classes .............................................................................................28
3.4. Override .................................................................................................................28
4. Lambda expressions .....................................................................................................29
5. Static members .............................................................................................................29
- Vertraulich / Confidential -
Session 1 – 1.09.2017
Prerequisite: Visual Studio 2015
In New Project Window, choose Visual C++, Empty Project. You may choose the location and the
name of the project also.
After clicking ok, the project is created. Now we need to add the first source file. For that, right click
on “Source Files” in Solution Explorer and then Add-> New File. Create the file Main.cpp.
- Vertraulich / Confidential -
2. Create the first C++ program, “Hello World!”.
- Vertraulich / Confidential -
Then we have the body of the main method, which is designated start of the program. We
may have several declarations for main, we used it the one without parameters. The method
is returning 0 which means that no error occurred.
In the end we display “Hello world!” in the console.
First build the solution (Menu -> Build -> Build Solution – F6), then run the project:
- Vertraulich / Confidential -
We may observe that the method is not defined inside a class. It is defined as free function.
The function has a global scope, meaning it can be accessed from anywhere, if you know its
signature.
4. Pointers
C++ offers you the power to manipulate the data in the memory of the computer. You may
allocate or de-allocate memory as you wish. This is done using Pointer variables.
Pointers are variables that points to a specific address in the memory, pointed by another
variable.
Examples discussed:
o Pointer is like shortcut – pointing to an executable of the file. You may change the
“.exe” that is pointing to.
o Pointer is like an address card.
How to declare a pointer:
- Vertraulich / Confidential -
This is a pointer to a variable of type int. We may also declare one of type char:
Allocate memory for the pointer (in this example 5 integers – 5 * sizeof(int) bytes)
Display the address of the pointer variable, the address of the first pointed element in the
vector:
5. Other debates
We may print the entire charPointer without loop into it:
Why the delete method doesn’t put the member to nullptr directly. Maybe one reason may
be const pointers.
- Vertraulich / Confidential -
Differences between struct type and class type
The members and base classes of a struct are public by default, while in class, they default to
private. Note: you should make your base classes explicitly public, private, or protected,
rather than relying on the defaults.
Struct and class are otherwise functionally equivalent.
“using”:
o using just one method from the entire namespace: using std::cout;
o inside a function block:
void function()
{
using namespace std;
}
- Vertraulich / Confidential -
Session 2 – 8.09.2017
2. Addresses
- Vertraulich / Confidential -
The incrementation of the pointer will move sizeof(T) bytes. For instance if you point to an
int which is 4 bytes in size the incrementation will move with 4 bytes.
For testing you may print the addresses:
- Vertraulich / Confidential -
int * ptr = (int*)malloc(10*sizeof(int));
You need to increase the size of the ptr from 10 to 20, without losing the
content.
o Free- free the memory allocated using malloc, calloc and realloc.
Signature: void free(void* ptr);
if ptr is NULL, then free does nothing and returns
Behavior is undefined on memory which is not on heap.
5. Struct:
- Vertraulich / Confidential -
Allocate memory for a struct:
- Vertraulich / Confidential -
Session 3 – 15.09.2017
o We can have a forward declaration to a strongly typed enum (by doing this the
possible enum values may be changed without forcing all dependent files to
recompile).
- Vertraulich / Confidential -
std::nullptr_t
o nullptr – C++ (11) has a keyword that designates a null pointer constant.
o nullptr replaces the NULL macro that have been used as null pointer substitutes for
many years.
o Example:
void f(int) // #1
void f(char *) // #2
f(0) // which f is called
// c++ 11
f(nullptr) // calls #2
- Vertraulich / Confidential -
3. Overloads
In C++ - two different functions can have the same name if their parameters are different
(different number or a different type).
4. Double pointers
5. Using new/delete
We saw in the last session how may we allocate or deallocate the memory using the
C-style : malloc/realloc/calloc/free.
Allocating and deallocating memory with new and delete (C++ style).
The memory is dynamically allocated using the operator new. new is followed by the data
type specified and for more than 1 element brackets are used [].
pointer = new type
pointer = new type [number_of_elements]
- Vertraulich / Confidential -
Example:
int *x = new int;
// C-Style
int *x = (int *) malloc (sizeof(int));
delete x;
x = new int[10];
The system dynamically allocates space for 10 elements of type int and returns a pointer to
the first element of the block, which is assigned to x (the pointer).
You may access the first element using *x or x[0].
New may also fail and to prevent that (well not really our case):
- you may handle the exception bad_alloc (when allocation fails);
- you may use the method known as nothrow and when memory allocation fails, the pointer
returned will be a null pointer:
x = new (nothrow) int [10];
6. Classes
Classes are an expanded concept of data structures: like data structures, they can
contain data members, but they can also contain functions as members.
An object is an instantiation of a class. In terms of variables, a class would be the
type, and an object would be the variable.
Classes are declared in header, with the keyword “class”. In header the variables but
also the methods are declared. The definition of the methods are written in a *.cpp
file. When the methods are defined in cpp, we should specify the class followed by
the resolution operator “::”.
6.1. Objects
To instantiate a class you need to declare an object of its type.
- Vertraulich / Confidential -
The access to the members is realized (same as structures) with the “.” operator for
the objects static defined and operator “->” for pointers.
6.3. Constructor
A constructor is called automatically when an object of the class is created. It is used
for initialize variables or allocate memory.
- Vertraulich / Confidential -
From C++11
ClassName() = delete;
This is inhibiting the automatic generation of a default constructor by the compiler.
Example: static class – no instantiation needed
ClassName() = default;
Explicitly forcing the automatic generation of a default constructor by the compiler.
Explicit
http://en.cppreference.com/w/cpp/language/explicit
6.4. Destructor
A destructor is called automatically when an object of the class is deleted, or it is out
of scope (lifetime of an object ends). It is used to free the resources of the object.
Syntax:
- Vertraulich / Confidential -
Session 4 – 22.09.2017
1. Topics from last discussion:
Once a reference is set to refer to a particular variable, you cannot, during its lifetime,
"rebind" it to refer to a different variable.
We may easily observed that the value of the firstPointer now is 20. Why?
Once again, you cannot “rebind” the reference to a different variable. It will assign the value of the
second pointer to the value referenced by ref, the first pointer.
We may compare the reference with a const pointer.
Example:
Declaration in header.
- Vertraulich / Confidential -
Definition in cpp file:
The copy constructor is called whenever an object is initialized (by direct-initialization or copy-
initialization) from another object of same type.
Examples (direct initialization and copy-initialization).
Function return (where the return type is type of the class and the class has no move constructor!)
- Vertraulich / Confidential -
3. Classes – assignment operators
Example
- Vertraulich / Confidential -
Session 5 – 29.09.2017
1. Classes’ inheritance
Classes in C++ may be extended. The process is called inheritance. This process involves a base class
and a derived class. The derived class inherits members of the base class and on top of these
members it may add its own members.
- Vertraulich / Confidential -
The same things is happening also with the assignment operator:
- Vertraulich / Confidential -
Session 6 – 6.09.2017
1. Multiple inheritance
C++ supports multiple inheritance. That means that a class may inherit members/methods from
multiple classes.
If the multiple inheritance is not properly used, it may lead to problems.
- Vertraulich / Confidential -
struct Person struct Person
{ {
string name; string name;
int age; int age;
}; };
Exchange e; Exchange e { 1, 2, 3 };
e.count = 1;
e.rates[0] = 2;
e.rates[1] = 3;
- Vertraulich / Confidential -
Session 7 – 24.11.2017
1. Pointer to functions (C-style)
2. Auto
int
float
const wchar *
Initializer list
3. Polymorphism
- Vertraulich / Confidential -
3.1. Pointers to base class – pointer to derived class type-compatible with a pointer to its base class.
- Vertraulich / Confidential -
3.2. Virtual members
3.4. Override
- Vertraulich / Confidential -
https://www.youtube.com/watch?v=iWvcoIKSaoc
Make examples.
Session 8 – 08.12.2017
4. Lambda expressions
5. Static members
- Vertraulich / Confidential -
Next:
inline/ coding guidelines (cpp/header) – Adrian?
const/ static
classes – move constructor, pointer this
casts (static_cast, dynamic_cast, const_cast, reinterpret_cast)
lambda expressions
smart pointers
Interesting concepts
Rule of three
If a class requires a user-defined destructor, a user-defined copy constructor, or a user-
defined copy assignment operator, it almost certainly requires all three.
Rule of five
Because the presence of a user-defined destructor, copy-constructor, or copy-assignment
operator prevents implicit definition of the move constructor and the move assignment
operator, any class for which move semantics are desirable, has to declare all five special
member functions.
Rule of zero
Classes that have custom destructors, copy/move constructors or copy/move assignment
operators should deal exclusively with ownership (which follows from the Single
Responsibility Principle). Other classes should not have custom destructors, copy/move
constructors or copy/move assignment operators.
- Vertraulich / Confidential -
Good references:
http://en.cppreference.com
http://www.cplusplus.com/doc/tutorial/
https://www.tutorialspoint.com/cplusplus/
Sources used:
https://en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/Pointers
http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html
http://www.cplusplus.com/doc/tutorial/dynamic/
http://en.cppreference.com/w/cpp/language/rule_of_three
http://en.cppreference.com/w/cpp/language/copy_constructor
http://www.cplusplus.com/doc/tutorial/polymorphism/
http://en.cppreference.com/w/cpp/language/override
- Vertraulich / Confidential -