Sams Teach Yourself C++ in 21 Days

(singke) #1
8: virtual ~Cat() { HowManyCats--; }
9: virtual int GetAge() { return itsAge; }
10: virtual void SetAge(int age) { itsAge = age; }
11: static int GetHowMany() { return HowManyCats; }
12: private:
13: int itsAge;
14: static int HowManyCats;
15: };
16:
17: int Cat::HowManyCats = 0;
18:
19: void TelepathicFunction();
20:
21: int main()
22: {
23: const int MaxCats = 5;
24: Cat *CatHouse[MaxCats]; int i;
25: for (i = 0; i < MaxCats; i++)
26: {
27: CatHouse[i] = new Cat(i);
28: TelepathicFunction();
29: }
30:
31: for ( i = 0; i < MaxCats; i++)
32: {
33: delete CatHouse[i];
34: TelepathicFunction();
35: }
36: return 0;
37: }
38:
39: void TelepathicFunction()
40: {
41: std::cout <<”There are “ << Cat::GetHowMany()
42: <<” cats alive!” << std::endl;
43: }

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!

OUTPUT


512 Day 15


LISTING15.4 continued
Free download pdf