Ticker

6/recent/ticker-posts

STATIC DATA MEMBER | STATIC MEMBER FUNCTION

    

STATIC DATA MEMBERS

1) A data member of a class can be qualified as static. A static member variable has certain special characteristics these are. 

i) It is initialized to zero when the first object of its class is crated. No other initialization is permitted. 

ii) 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. 

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

2) Static variables are normally used to maintain values common to the entire class for Ex - A static data member can be used as a counter that records the occurrences of all the objects. 

# include 
class item 

        static int count; 
        int number;
    public : 
        void getdata(int a) 
        
                number = a; 
                count ++; 
        
        void get count (void) 
        
                cout << "count :"; 
                cout << count << "\n"; 
        
}; 
int item : : count; 
int main ( ) 

        item a, b, c; 
        a.getcount() ; 
        a.getcount() ; 
        a.getcount() ; 
        a.getdata(100); 
        a.getdata(200); 
        a.getdata(300); 
        cout <<"After reading data"; 
        a.getcount(); 
        b.getcount(); 
        c.getcount(); 
        return 0; 

the output would be 
    count : 0 
    count : 0 
    count : 0 
After reading data 
    count : 3 
    count : 3 
    count : 3 

The static variable count is initialized to zero when the objects are created. 

The count is incremented whenever the data is read into an object. Since the data is read into objects three times, variable count is incremented three times. 

3) Static variables are like non inline member functions in that they are declared in a class declaration & defined in the source file.

STATIC MEMBER FUNCTION

1) A static member function has following properties.

i) A static function can have access to only other static members declared in the same class.

ii) A static member function can be called using the class name as follows:

classñname:: functionñname;

2) Following program illustrates the implementation of these characteristics. 

#include<iostream.h>
class test
{
            int code;
            static int count;
        public:
            void setcode(void)
            {
                    code = ++count;
            }
            void showcode(void)
            {
                    cout <<îobject No ì << code;
            }
            static void showcount(void)
            {
                cout<< ìcount : ì<<count;
            }
}
int test:: count;
main()
{
            test t1, t2;
            t1.setcode();
            t2.setcode();
            test:::showcount();
            test t3;
            t3.setcode();
            test:::showcount();
            t1.showcode() ;
            t2.showcode();
            t3.showcode();

Output of the program 
        count : 2 
        count : 3 
        object No : 1 
        object No : 2 
        object No : 3

The static function showcount displays the number of objects created till that moment. A count of number of objects created is maintained by the static variable count

ALSO SEARCH:

"static data member and static member function"

"discuss the static data member and static member function in detail"

"explain static data member and static member function with example"

"difference between static data member and static member function"

"static data member and static member function program in C++"

"difference between static data member and static member function in C++"

"demonstrate static member function with static data member in one class"

"explain static data member and static member function with example in C++"

"static data member vs static member function in C++"

"we can use the static member functions and static data member"

"what do you mean by static data member and static member functions explain it with example"