Page 101 - DCAP404 _Object Oriented Programming
P. 101

Object-oriented Programming




                    Notes          You should get the following output from this program.
                                          The present value of count is 1
                                          The present value of count is 2
                                          The present value of count is 3

                                          The present value of count is 4
                                          The present value of count is 5



                                     Did u know?  What is the use of static members?
                                     A typical use of static members is for recording data common to all objects of a class. For
                                     example, you can use a static data member as a counter to store the number of objects of a
                                     particular class type that are created.

                                   5.1.1  Static Member Functions

                                   Just as data members can be static, member functions can also be static. A static function is a
                                   member function of a class and it manipulates only static data member of the class. It acts as
                                   global for members of its class without affecting the rest of the program. Static members serve
                                   the purpose of reducing the need for global variables by providing alternatives that are local to
                                   a class.

                                   A static member function is not a part of objects of class. It is instance dependent and can be
                                   accessed directly by using the class name and scope resolution operator. If it is declared and
                                   defined in a class, the keyword static should be used only in declaration part.

                                   The following program demonstrates the use of static member function which accesses static
                                   data. The data member count and the function disp( ) are declared static in the class counter. But
                                   the variable count is initialized to 0 outside the class.
                                          #lnclude  <iostream.h>
                                          #lnclude  <conin.h>
                                          class  counter
                                          {

                                          private:
                                                 static  int  count;
                                          public:
                                                 counter()
                                                 {
                                                        ++count;
                                                 }

                                                 static  void  disp();
                                          };
                                          int  counter::count=0;
                                          void  counter::disp()




          94                                LOVELY PROFESSIONAL UNIVERSITY
   96   97   98   99   100   101   102   103   104   105   106