Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are...

57
Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni-karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY Introduction to C++ Lecture 06: C++ classes Roger Wolf 17. Mai 2018

Transcript of Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are...

Page 1: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Introduction to C++

Lecture 06: C++ classes

Roger Wolf17. Mai 2018

Page 2: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 1:

My first C++ class

1/24

Page 3: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Classes in C++

● The main elements of C++ are objects and classes:

2/24

Page 4: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Classes in C++

● The main elements of C++ are objects and classes:

2/24

Page 5: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Classes in C++

● The main elements of C++ are objects and classes:

Keyword to define a class

2/24

Page 6: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 7: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 8: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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)

Page 9: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 10: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 11: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 12: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

● A class automatically defines a namespace with the name of the class (see Lecture­04).

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 Lecture­02).

● This is how the definition of the member function setAddress would look like outside the class declaration block in a .cc:

4/24

Page 13: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

● A class automatically defines a namespace with the name of the class (see Lecture­04).

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 Lecture­02).

● 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

Page 14: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 15: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 16: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Access to member functions…

● … from outside:

6/24

Page 17: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Access to member functions…

● … from inside:

6/24

Page 18: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 19: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 20: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 21: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 22: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 2:

C++ construction works

8/24

Page 23: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 24: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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 Lecture­01).

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.

Page 25: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The constructor @ work

● The constructor and destructor are special (public) member functions:

10/24

Page 26: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

● The constructor and destructor are special (public) member functions:

The constructor @ work10/24

Classic implementation of a constructor of this type.

Page 27: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 28: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 29: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 30: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 31: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 32: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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 Lecture­04). So you overload your constructor.

The constructor in discussion12/24

Page 33: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 34: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 35: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 3:

Increased privacy

13/24 Back to slide 2

Page 36: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 37: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 38: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Getting private…

Class members are not accessible any more from outside

15/24

Page 39: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Getting private…

Class members are not accessible any more from outside

Access is steered by a public access function

15/24

Page 40: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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 Lecture­04).

15/24

Page 41: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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 Lecture­04).

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

Page 42: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 43: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 44: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 45: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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 Lecture­07).

16/24

Page 46: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 4:

Static members and member functions

17/24

Page 47: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

● Members and member functions can also be static (see Lecture­04). 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

Page 48: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

● Members and member functions can also be static (see Lecture­04). 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

Page 49: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

● Members and member functions can also be static (see Lecture­04). 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

Page 50: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 51: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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 Lecture­04).

20/24

Page 52: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Access to static members & static member functions

● … from inside:

Here shown for a class member

21/24

Page 53: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 54: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 55: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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

Page 56: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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 Lecture­05). 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

Page 57: Introduction to C++ Lecture 06: C++ classesekp€¦ · Classes in C++ The main elements of C++ are objects and classes: “At this point” the class Person cannot be distinguished

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.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