انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Static Data Members

الكلية كلية العلوم للبنات     القسم قسم الحاسبات     المرحلة 2
أستاذ المادة مهدي عبد سلمان المسلماوي       3/29/2011 10:14:53 AM

Static Data Members

 

A data member of a class can be qualified as static. A static member variable has certain special characteristics:

 

·         It is initialized to zero when the first object of its class is created. No other initialization is permitted.

 

·         Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.

 

·         It is visible only within the class, but its lifetime is the entire program.

 

Static variable are normally used to maintain values common to the entire class. For example, a static data member can be used as a counter that records the occurrences of all the objects. Program below illustrates the use of a static data member. First, notice the following statement  in the program:

 

                                    int item : : count;       //definition of static data member

 

Note that the type and scope of each static member variable must be defined outside the class definition. This is necessary because the static data members are stored separately rather than as a part of an object. Since they are associated with the class itself rather than with any class object, they are also known as class variables.

 

/////////////////////[ static class members] //////////////////////////////

 

//------------------------------------------ Include Section ----------------------------------------------------

 

#include<iostream.h>

 

 

 

//----------------------------------------------- Class Declaration --------------------------------------------

 

class item

 

{

 

         static int count ;           // count is static

 

         int number;

 

  public:

 

         void getdata(int a)

 

          {

 

                       number = a;

 

                       count ++;

 

           }

 

          Void getcount(void)

 

           {

 

                      cout << " count :  ";

 

                      cout <<  count <<"\n";

 

            }

 

};

 

//---------------------------------------------- Member Function Definition-------------------------------

 

 

int item: : count;                //count DEFIENED

 

 

int main(void)

 

{

 

          item a, b, c;                    // count is initialized to zero

 

          a.getcount();                  // display count

 

          b.getcount();                 

 

          c.getcount();

 

 

          a.getdata(100);                // getting data into object a

 

          b.getdata(200);                // getting data into object b

 

          c.getdata(300);                // getting data into object c

 

    

 

          count <<"After reading data" << "\n";

 

          a.getcount();                  // display count

 

          b.getcount();

 

          c.getcount();

 

}

 

///////////////////////< program 5.4 > ////////////////////////////////

 

The output would be

 

                        count : 0

 

                        count : 0

 

                        count : 0

 

After reading data

 

                        count : 3

 

                        count : 3

 

                        count : 3

 

100

100

100

3

 Object 1                                        Object 2                                            Object 3

 

number                                        number                                               number

      

count

 

( common to all three objects)

 

While defining a static variable, some initial value can also be assigned to the variable. for example:

 

 

int item : : count =10;

 

 

Static Member Functions

 

A member function that is declared static has the following properties:

 

  • A static function can have access to only other static members ( functions or variables) declared in the same class.

     

  • A static member function can be called using the class name (instead of its objects) as follows:

     

class-name : : function-name;

 

Program below illustrates the implementation of these characteristics. The static function showcount( ) displays the number of objects created till that moment.

 

 

//////////////////////[ STATIC MEMBER FUNCTION]/////////////////////

 

#include <iostream.h>

 

 

class test

 

{

 

          int code;

 

          static int count;                             //static member variable

 

      public:

 

           void setcode(void)

 

           {

 

                      code=++count;

 

           }

 

           void showcode(void)

 

           {

 

                       cout << "object number : "<< code << "\n";

 

           }

 

            static void showcount (void)  // static member  function

 

            {

 

                          cout << "count : " << count << "\n";

 

            }

 

};

 

int test : : count;

 

int main(void)

 

{

 

         test t1, t2;

 

 

         t1.setcode( );

 

         t2.setcode( );

 

 

         test: : showcount( );            // accessing static  function

 

 

        test t3;

 

        t3.setcode( );

 

 

       t1.showcode( );

 

       t2.showcode( );

 

       t3.showcode( );

 

}

 

////////////////////////// < program 5.5>///////////////////////////////

 

Output of program 5.5

 

                                    count : 2

 

                                    count : 3

 

                                   

 

                        object number : 1

 

                        object number : 2

 

                        object number : 3

 

Remember,  the following function definition will not work

 

                        static void showcount( )

 

                        {

 

                                    cout <<code;      // code in not static

 

                        }

 


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .