O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Static Member Functions

Static member functions are like static member variables: They exist not in an object but in the scope of the class. Therefore, they can be called without having an object of that class, as illustrated in Listing 20.2.

Listing 20.2. Static Member Functions
 0: //Listing 20.2 static data members 1: #include <iostream> 2: 3: class Cat 4: { 5: public: 6: Cat(int age = 1):itsAge(age){HowManyCats++; } 7: virtual ~Cat() { HowManyCats--; } 8: virtual int GetAge() { return itsAge; } 9: virtual void SetAge(int age) { itsAge = age; } 10: static int GetHowMany() { return HowManyCats; } 11: private: 12: int itsAge; 13: static int HowManyCats; 14: }; 15: 16: int Cat::HowManyCats = 0; 17: 18: void TelepathicFunction(); 19: 20: int ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required