Special Classes and Functions 509
22: int main()^15
23: {
24: const int MaxCats = 5; int i;
25: Cat *CatHouse[MaxCats];
26:
27: for (i = 0; i < MaxCats; i++)
28: {
29: CatHouse[i] = new Cat(i);
30: TelepathicFunction();
31: }
32:
33: for ( i = 0; i < MaxCats; i++)
34: {
35: delete CatHouse[i];
36: TelepathicFunction();
37: }
38: return 0;
39: }
40:
41: void TelepathicFunction()
42: {
43: cout << “There are “;
44: cout << Cat::HowManyCats << “ cats alive!” << endl;
45: }
There are 1 cats alive!
There are 2 cats alive!
There are 3 cats alive!
There are 4 cats alive!
There are 5 cats alive!
There are 4 cats alive!
There are 3 cats alive!
There are 2 cats alive!
There are 1 cats alive!
There are 0 cats alive!
Listing 15.2 is very much like Listing 15.1 except for the addition of a new func-
tion,TelepathicFunction(). This function, which is defined on lines 41–45,
does not create a Catobject, nor does it take a Catobject as a parameter, yet it can
access the HowManyCatsmember variable. Again, it is worth reemphasizing that this
member variable is not in any particular object; it is in the class, where it is accessible to
any member function. If public, this variable can be accessed by any function in the pro-
gram, even when that function does not have an instance of a class.
OUTPUT
LISTING15.2 continued
ANALYSIS