Programmingoneonone is known for sharing best programming knowledge.

Static data member in c++ programming

In this post, you will learn about what is a static data member in c++ programming and learn how to declare a static data member and why we use it in programming.


static data member in c++ programming, learn c++ programming
learn c++ programming

static data member:

the data members which is common for all objects and class is called static data members. by default, the initialization value for static data member is 0 zero.

we use static keyword to declare a static data member.

Example:
               class yash
               {
                  static int var;
               };

Static = a static keyword for declaring static data.
int     = return type of the var data member.
var    = var is the name of the data member.

Note: we can declare a static data member only one time, but use as many time. after declaring a static data member in a class, to use the static data member in the program we need to declare the static data member one time from outside the class. 

syntext:
             <return-type> <class-name>::<variable-name>;

Example:
at the above, we declare a static data member of var name. so to define the var static variable from outside the class we define like this.

               int Yash:: var;

Note: to define the static data member from outside the class is always needed in the program. you can also declare the value to the static variable at the declaration time from outside the class.

Example:
                int yash::var=10;

Note: by default, the initial value for the static var data member is 0. but now the initial value for the var static member is 10.

Example program to define a static member inside the class.

        #include<iostream>

        class test
        {
             int a;
             static int count; // define a static count data member.

             public:  // access speicifer public

               void getdata(int);
               void dispstatic(void);
         };

        void test::getdata(int x)
        {
            a=x;
            std::cout<<"\n Number="<<a;
            count++;      /* here the initial value for the static count                                                     variable is incremented by one. */
        }

         void test::dispstatic()
         {
              std::cout<<"\n value for static counter is:"<<count;
         }

         int test::count; 
          
        // here I declare a static data member outside the class 

         int main()
         {
            test t1, t2; 

            t1.dispstatic();       
       /* here the initial value for static variable is                                                   printed for the t1 object. */                                                                       
            t2.dispstatic(); 
      /* here the initial value for the static                                                                variable is printed for the  t2 object.*/   
                  
            t1.getdata(10);   
            t2.getdata(20);
            t1.dispstatic(); 
       /* here the incrememt value for the static                                                      variable is printed for the t1 object.*/
            t2.dispstatic(); 
      /* here the increment value for the static                                                        variable is printed for the t2 object.*/ 

            return 0;
         }


Note: in the above example program here the two objects are created for one class so here the two copy is created of class test for the object t1 and t2. but here only one copy is created of count variable, because of it's a static data member. 

"Please share this knowledge as much possible as you can and also comment me your queries and questions related to this topic. I will really grateful to help you."

No comments:

Post a Comment