Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY
Introduction to C++
Lecture 06: C++ classes
Roger Wolf17. Mai 2018
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY
Part 1:
My first C++ class
1/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Classes in C++
● The main elements of C++ are objects and classes:
2/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Classes in C++
● The main elements of C++ are objects and classes:
2/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Classes in C++
● The main elements of C++ are objects and classes:
Keyword to define a class
2/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Classes in C++
● The main elements of C++ are objects and classes:
Keyword to define a class
Everything that follows is accessible from outside
2/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Classes in C++
● The main elements of C++ are objects and classes:
Keyword to define a class
Member of the class Person
Everything that follows is accessible from outside
2/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Classes in C++
● The main elements of C++ are objects and classes:
● “At this point” the class Person cannot be distinguished from the C struct Address.
Keyword to define a class
Member of the class Person
Everything that follows is accessible from outside
2/24
(1) Indeed the only difference between a C++ struct and a class is that that for a struct all members and member functions per default are public, while for a class per default they are private (see slide 13).
(1)
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Member functions
● A class usually also has member functions, which are accessible in the same way as its members:
3/24 Back to slide 12
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Member functions
Class members
● A class usually also has member functions, which are accessible in the same way as its members:
3/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Member functions
Members functions
Class members
● A class usually also has member functions, which are accessible in the same way as its members:
3/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● A class automatically defines a namespace with the name of the class (see Lecture04).
Member functions
● Usually you would separate the declaration of the class from the definition of the member functions into a .h and a .cc file (see Lecture02).
● This is how the definition of the member function setAddress would look like outside the class declaration block in a .cc:
4/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● A class automatically defines a namespace with the name of the class (see Lecture04).
Member functions
● Usually you would separate the declaration of the class from the definition of the member functions into a .h and a .cc file (see Lecture02).
● This is how the definition of the member function setAddress would look like outside the class declaration block in a .cc:
Note: the namespace tag
4/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Access to class members
● From outside, class members can be accessed via the •.• operator (for objects) or •>• operator (for object pointers):
Access to public members of an object of type Person.
5/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Access to class members
● From outside, class members can be accessed via the •.• operator (for objects) or •>• operator (for object pointers):
Access to public members of an object of type Person.
Access to public members of a pointer to an object of type Person.
5/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Access to member functions…
● … from outside:
6/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Access to member functions…
● … from inside:
6/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Access to member functions…
● … from inside:
● Note: at least in the past the runtime performance of C++ was throttled by too many (unnecessary) function calls.
6/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
What is this?
● Sometimes an object of your class needs to access or pass itself as a pointer. The construct for this purpose is this:
Note the difference here:
7/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
What is this?
● Sometimes an object of your class needs to access or pass itself as a pointer. The construct for this purpose is this:
Note the difference here:
7/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
What is this?
● Sometimes an object of your class needs to access or pass itself as a pointer. The construct for this purpose is this:
● Note: if you find this notation confusing don’t worry: whenever you need it it will become clear to you. If you find it intuitive – fine :-)…
Note the difference here:
7/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY
Part 2:
C++ construction works
8/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Constructor ↔ Destructor
● The constructor and destructor of a class are special (public) member functions:
Constructor:called when the object is initialized. Several constructors can exist. C++ provides a default constructor in case it is not explicitly declared.
9/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Constructor ↔ Destructor
● The constructor and destructor of a class are special (public) member functions:
called when the object is destroyed and the allocated memory freed. There can only be one destructor. C++ provides a default destructor in case it is not explicitly declared, but you should declare it in case you have allocated memory in own responsibility (see Lecture01).
Destructor:
9/24
Constructor:called when the object is initialized. Several constructors can exist. C++ provides a default constructor in case it is not explicitly declared.
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
The constructor @ work
● The constructor and destructor are special (public) member functions:
10/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● The constructor and destructor are special (public) member functions:
The constructor @ work10/24
Classic implementation of a constructor of this type.
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● The constructor and destructor are special (public) member functions:
Classic implementation of a constructor of this type.
Note: these objects are being created (with default values) at the time when the constructor is entered. These are assignment operators.
The constructor @ work10/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● The constructor and destructor are special (public) member functions:
You can also initialize the members before the constructor is actually called.
The constructor @ work11/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● The constructor and destructor are special (public) member functions:
You can also initialize the members before the constructor is actually called.
Note: you need this construct e.g. for own classes that do not have an assignment operator declared, but also to assign values to const members inside the class at the time of initialization.
The constructor @ work11/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● Usually it is also more runtime efficient to initialize the class members before the constructor is called. Note: in this case you should obey the order in which the members are declared to profit from the performance gain. If you don’t, usually your compiler issues a warning at compilation time.
The constructor in discussion12/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● Usually it is also more runtime efficient to initialize the class members before the constructor is called. Note: in this case you should obey the order in which the members are declared to profit from the performance gain. If you don’t, usually your compiler issues a warning at compilation time.
● Any object in C++ follows the same rules for constructors. Some examples:
Of course built-in types are still somewhat special. The initialization only looks similar.
The constructor in discussion12/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● Usually it is also more runtime efficient to initialize the class members before the constructor is called. Note: in this case you should obey the order in which the members are declared to profit from the performance gain. If you don’t, usually your compiler issues a warning at compilation time.
● Any object in C++ follows the same rules for constructors. Some examples:
Of course built-in types are still somewhat special. The initialization only looks similar.
● Multiple versions of constructors are possible due to function overloading (see Lecture04). So you overload your constructor.
The constructor in discussion12/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● When a class does not have a constructor C++ will initialize its objects with all members initialized to (binary) zero.
The constructor in discussion12/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● When a class does not have a constructor C++ will initialize its objects with all members initialized to (binary) zero.
● When defining a single arbitrary constructor C++ will not call the “built-in” default constructor any more. If you want to have a default constructor in addition you will have to declare it yourself (see our receding example on slide 3).
The constructor in discussion12/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY
Part 3:
Increased privacy
13/24 Back to slide 2
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● You can declare areas of your class such that they will not be accessible from outside the class.
Getting private…
● You do this via the keywords private (or protected).
● The idea is that the public part of the class defines the interface. Everything that happens behind this curtain is irrelevant to the outside user. In the ideal case the user should not even (want to) know what happens there.
Visible to the user
Visible to the user
Visible to the programmer
14/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Getting private…
Here I turned address_ into a pointer. If not known it will not hold empty fields, but just a NULL pointer, which is more space efficient. In turn I take care that this construct cannot be misused, by making it private. I provide an access function that makes sure that a reasonable value is returned. For the other members privacy is indeed irrelevant and from the viewpoint of runtime performance maybe even questionable.
15/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Getting private…
Class members are not accessible any more from outside
15/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Getting private…
Class members are not accessible any more from outside
Access is steered by a public access function
15/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Getting private…
Class members are not accessible any more from outside
Access is steered by a public access function
You can protect your class members even more by use of the keyword const (see Lecture04).
15/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Getting private…
Class members are not accessible any more from outside
Access is steered by a public access function
You can protect your class members even more by use of the keyword const (see Lecture04).
Note: this class now also needs a user-defined destructor to make sure that the allocated space for address_ is freed after use.
15/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● You can declare a class friend of your class to allow access to your classes private area (i.e. member functions and data members).
Making friends… 16/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● You can declare a class friend of your class to allow access to your classes private area (i.e. member functions and data members).
Making friends…
friend class NameParser gets access to private data members.
16/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● You can declare a class friend of your class to allow access to your classes private area (i.e. member functions and data members).
Making friends…
Class NameParser gets access to private data members.Same for the friend function parsePersonNames().
16/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● You can declare a class friend of your class to allow access to your classes private area (i.e. member functions and data members).
Making friends…
Class NameParser gets access to private data members.Same for the friend function parsePersonNames().
● Friendship is not symmetric.
● Friendship is not transitive.
● Friendship cannot be inherited (see Lecture07).
16/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY
Part 4:
Static members and member functions
17/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● Members and member functions can also be static (see Lecture04). In this case they are not bound to an object of type class, but to the class itself.
Static members & static member functions18/24 Back to slide 19
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● Members and member functions can also be static (see Lecture04). In this case they are not bound to an object of type class, but to the class itself.
Static members & static member functions
Static class member
18/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● Members and member functions can also be static (see Lecture04). In this case they are not bound to an object of type class, but to the class itself.
Static members & static member functions
Static member function
Static class member
18/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● Remember: static types are written into the executable during the TU at compile time.
Static members
● Static class members are not bound to a class object, but part of the class as a whole. Consequently they are accessible by any object of a given class.
● Typical use cases:
● Count instances of a given class in your program (see example on slide 18).
● Administrate a flag that indicates that a certain initialization has been made.
● Keep a pointer to a certain object in a collection of objects.
19/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Static members: initialization
● Since they are not associated to objects of a class, static members cannot be initialized via an object or constructor:
Do this in the .cc file of your implementation, as you would do for a normal static type (see Lecture04).
20/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Access to static members & static member functions
● … from inside:
Here shown for a class member
21/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Access to static members and member functions
● … from outside:
Access is possible via an arbitrary object
Or without association to any object
22/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Access to static members and member functions
● … from outside:
Access is possible via an arbitrary object
Or without association to any object
● Note: since the static member (function) is not associated to any class object, a class object is never evaluated in the example above. It is only used to identify the class.
22/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Access to static members and member functions
● … from outside:
Access is possible via an arbitrary object
Or without association to any object
● Note: for the same reason a static member function can access members of objects only via objects.
22/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
● Your design is most likely suboptimal if:
Object oriented program design
● The role of your class is difficult to describe.
● The identification of classes and objects is the major step in an object oriented design (see Lecture05). It can be difficult and should not be underestimated.
● Your class has too many member functions without clear goal.
● Your class works together with many other classes.
● Your class has “many friends”.
● Don’t be shy to change your class structure if you envisage flaws in your original design.
23/24
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.unikarlsruhe.de/~rwolf/
Summary
● Classes in C++.
● Member and member functions.
● The constructor and destructor.
● Access restriction, privacy.
● The friend declaration.
● Static members & member functions.
24/24