• Now Online : 30
  • admin@codemyne.net


In the previous section, we had mentioned that objects of class maintain their own copy of member data. In some cases, it is imperative that all objects of a class have access to the same copy of a single variable. Take the word class for instance. Assume that the number of words existing at any given point of time i.e., the number of objects created for that class must be available. This can be made possible by making use of static variables. Consider the code given below:

        Class word
            char *str_word;
            static int wordcount;
            /* a static member keeps count of the number of objects created for this class */
            word(char *);
            int getlen(void);
            char *getword(void);
            void init(void);
        word: :word(char *s)
            str_word=new char[strlen(s)+1];
            strcpy(str_word, s);
            cout<<”\n the wordcount is ”<<wordcount;
        int word: :getlen()
            return strlen(str_word);
        char *word: :getword()
            return str_word;
        void word: :init()
            word: :wordcount=1;
        word: :~word()
            delete str_word;
            wordcount= - - wordcount;
        void main()
            word word1(“This is a test”);
            /* after the first object is created, the word count is initialized to 1 */
            word word2(“This is another test”); 
            /* wordcount is incremented to 2 */
            word word3 (“This is the third test”);
            //wordcount is 3

In the above program, as soon as the first object is created, the init() function is invoked, initializing the wordcount to 1. After that, whenever an object is created, wordcount is incremented by 1.

Static Functions:

Static functions are member functions that access only the static data in a class. For example, the init() function in the word class discussed above accesses only the static member wordcount. This function can be declared as static.

Static functions can be accessed either through an object, or directly, without creating an instance of the class (using the scope ‘: :’ operator). Since in example, the static variable wordcount must be initialized before creating any object, the init() function must be invoked through the scope resolution operator. The following code snippet illustrates this:

        void main()        
            word: :init(); 
            /*before the 1 object in cronted, the word count must be initialized to 0 */
            word word1 (“This is a test”); //wordcount is incremented to 1
            word word2(“This is another test”); /* wordcount is incremented to 2*/
            word word3(“This is the third test”); //wordcount is 3

As the function init() is invoked even before the first object is created, the function will be rewritten as follows:

    void word: :init()

and the declaration for the function within the class will be:

static void init(void);

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment