Chapter 2
- The complexity referred to is not
the structural complexity of the computation,
that is the space and time needed
to solve a problem,
but the conceptual complexity of programming,
the organization of the software.
See slide
[2-devices].
- Constructors describe what needs
to be done when an instance of the class
is created.
Leaving the initialization of an object
to the user may lead to many errors
that are easily avoided by defining appropriate
constructors.
Destructors play a complementary role,
by taking care of cleaning up before
the object is destroyed.
- The type modifier const is used
to declare a variable referring to a constant
of a particular type.
See slide
[2-const].
In addition const may be used to indicate
that a member function does not affect
the instance variables of an object.
- Automatic type conversion
from a source to a target type may
take place, when (a) the target type
has a constructor with the source type
as a single argument, or when (b)
the source type defines a type operator
of the target type.
Type conversions may be enforced by using a cast.
See slide
[2-conversions].
- Friends may be classes or functions.
They are allowed access to the private parts
of an object.
They may be necessary for reasons of efficiency.
Friends are a relatively safe feature,
since they must explicitly be declared
by the class itself.
They are not inherited.
Neither is it possible for a class or
function to declare itself as a friend
of a class.
Nevertheless, friends may jeopardize
the integrity of an object.
Treat friends with care.
- Generic types allow the programmer to define
functionality in a general way,
with respect to a variety of types.
For instance, a generic container may contain
items of arbitrary type.
C++ supports generic types by means of
inheritance and template classes.
See slides
[sli-2-void] and
[sli-2-template] for some examples.
- A canonical class is a class realizing
a concrete data type, that is defined to behave
as a built-in type.
It must include the definition of
a default constructor,
a copy or reference constructor,
a destructor and assignment.
An example is given in slide
[sli-2-string].
- The handler/body idiom describes
how to separate the interface of an object
class from its implementation.
An example is given in
slides
[sli-2-handler]
and
[sli-2-body].
- When derived classes have unlimited access
to instance variables of ancestor classes,
the class hierarchy defined by the inheritance
relation may collapse when the implementation
of an ancestor class is changed.
The solution is to distinguish between
private instance variables
that are invisible even to derived classes
and protected variables that are
invisible only for external clients.
- This is the sneaky reference example
given in slide
[sli-2-sneaky].
- Inheritance allows for an elegant
conceptual organization of the class
types employed in a library or program.
Some potential disadvantages are
listed in slide
[sli-2-cost].
- The most important extensions are
meta classes, which support runtime type
information,
persistence,
to allow for the storage of objects,
and active objects,
to allow for concurrency.
See slide
[2-extensions].