This chapter will continue the introduction to classes by developing
and explaining a second, rather trivial, application which is intended
to present and explain the concepts of data attributes, class wide actions, instance actions, class constants and constructors,
which were mentioned or briefly introduced in Chapter 1. The
application is described as trivial only in the sense that it does not
do anything useful, its conceptual understanding is anything but
trivial.
The classes presented in the previous chapter
contained only a single action which always returned the same
information. By adding data attributes to a class
declaration it is possible for each instance to maintain information
about its own state and to change this information as its actions are
used. The main() action from the client class in Chapter 1 was described as a class wide
action, which means that it is associated with the entire class and can
be used without having first to declare an instance of the class. The
alternative possibility is to have instance actions where the action can only be used by first declaring an instance of the class.
The distinction between class wide and instance actions is related to the distinction between class wide data attributes and instance data attributes.
There is only one copy of a class wide attribute for the entire class,
and it can be accessed by any instance or class wide action. Instance
attributes have one copy per instance of the class and can only be
accessed by instance actions. A class wide attribute can also be
declared to have a constant value, in which case it is known as a class constant, and unlike a class or instance variable attribute its value cannot be changed by any actions.
All
the advice which was given at the start of Chapter 1 concerning how to
approach that chapter can be repeated for this chapter. A full
understanding of the concepts in this chapter is unlikely to be
obtained when it is followed for the first time. It will take time and
effort to assimilate and accommodate these concepts. It will also be of
benefit to revisit this chapter after Chapter 3 has been completed, in
order to consolidate the initial understanding.
Read More