Wednesday, February 6, 2019

static member function in c++ programming

In this post, you will learn about the static member function in c++ programming and learn why we use static data member and member function in programming. 

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

static member function:

the function which can only access the static data member is called the static member function. 

to declare a static data member we just need to use the static keyword at the beginning of the initialize time of the member function. 

                class test

                   static int function(void);

Note: static function only access the static data members. it does not access the usual members. 

calling the static member function:

we can also call the static member function using the class name. 
                in the above example, we declare a static member function name function. so to call the static function by class we call like this.



Example program for declaring a static data member or a static member function. 


          class test
               int var;

               static int count; 
           // declare a static variable count


                      void setval(void);
                      void printval(void);
                      static void printsta(void); 
              // declare a static member function.

          int test::count;

          void test::setval()
             var= ++count;

         void test::printval()
              std::cout<<"object number:"<< var;

         void test::printsta() //define a static member funtion. 
           std::cout<<"\ncounter"<<count; //only use static variable 

         int main()
             test t1,t2;


             test t3;


             return 0;

Note: here in the above example we can only access the static data member in the static member function.

"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."
Previous Post
Next Post

Yash is a Full Stack web developer. he always will to help others. and this approach takes him to write this page.