23 Intermediate class
The class construct has many ramifications and extensions, a few of which are
introduced in this chapter.
Section 23.1 looks at the problem of data that need to be shared by all instances of a
class. Shared data are quite common. For example, the air traffic control program in
Chapter 20 had a minimum height for the aircraft defined by a constant; but it might be
reasonable to have the minimum height defined by a variable (at certain times of the
day, planes might be required to make their approaches to the auto lander somewhat
higher say 1000 feet instead of 600 feet). The minimum height would then have to be a
variable. Obviously, all the aircraft are subject to the same height restriction and so
need to have access to the same variable. The minimum height variable could be made
a global; but that doesn't reflect its use. If really is something that belongs to the
aircraft and so should somehow belong to class Aircraft. C++ classes have "static"
members; these let programmers define such shared data.
Section 23.2 introduces "friends". One of the motivations for classes was the need
to build privacy walls around data and specialist housekeeping functions. Such walls
prevent misuse of data such as can occur with simple structs that are universally
accessible. Private data and functions can only be used within the member functions of
the class. But sometimes you want to slightly relax the protection. You want private
data and functions to be used within member functions, and in addition in a few other
functions that are explicitly named. These additional functions may be global
functions, or they may be the member functions of some second class. Such functions
are nominated as "friends" in a class declaration. (The author of a class nominates the
friends if any. You can't come along later and try to make some new function a "friend"
of an existing class because, obviously, this would totally defeat the security
mechanisms.) There aren't many places where you need friend funct