Chp~14 - C++ PROGRAMMING Chapter 14 Special Classes and...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C++ PROGRAMMING Chapter 14 Special Classes and Functions Static Member Data Listing 14.1. Static member data. Listing 14.2. Accessing static members without an object . Listing 14.3. Accessing static members using non-static member functions . Static Member Functions Listing 14.4. Static member functions . Static Member Functions Pointers to Functions Listing 14.5. Pointers to functions . Pointer to Function Why Use Function Pointers? Listing 14.6. Rewriting Listing 14.5 without the pointer to function . Shorthand Invocation Arrays of Pointers to Functions Listing 14.7. Demonstrates use of an array of pointers to functions . Passing Pointers to Functions to Other Functions Listing 14.8. Passing pointers to functions as function arguments. Using typedef with Pointers to Functions Listing 14.9. Using typedef to make pointers to functions more readable . Pointers to Member Functions Listing 14.10. Pointers to member functions . Arrays of Pointers to Member Functions Listing 14.11. Array of pointers to member functions . Summary Q&A Quiz Exercises Chapter 14 Special Classes and Functions C++ offers a number of ways to limit the scope and impact of variables and pointers. So far you've seen how to create global variables, local function variables, pointers to variables, and class member variables. ToChapter you learn What static member variables and static member functions are. How to use static member variables and static member functions. How to create and manipulate pointers to functions and pointers to member functions. How to work with arrays of pointers to functions. Static Member Data Until now, you have probably thought of the data in each object as unique to that object and not shared among objects in a class. For example, if you have five Cat objects, each has its own age, weight, and other data. The age of one does not affect the age of another. There are times, however, when you'll want to keep track of a pool of data. For example, you might want to know how many objects for a specific class have been created in your program, and how many are still in existence. Static member variables are shared among all instances of a class. They are a compromise between global data, which is available to all parts of your program, and member data, which is usually available only to each object. You can think of a static member as belonging to the class rather than to the object. Normal member data is one per object, but static members are one per class. Listing 14.1 declares a Cat object with a static data member, HowManyCats . This variable keeps track of how many Cat objects have been created. This is done by incrementing the static variable, HowManyCats , with each construction and decrementing it with each destruction....
View Full Document

This note was uploaded on 01/13/2012 for the course CS 131 taught by Professor Clayton during the Spring '08 term at Bethune Cookman.

Page1 / 26

Chp~14 - C++ PROGRAMMING Chapter 14 Special Classes and...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online