Simple Program for Static Members Functions Using C++ Programming


The member functions are two types:

  • static member functions
  • non-static member functions

The static member functions don't have the 'this' pointer and static member functions can access only static data members whereas non-static members functions can access both static and non-static data members.

Simple Program for Static Members Functions Using C++ Programming

#include<iostream> using namespace std; class Demo_Static{ static int count; public: Demo_Static(){ count++; } static int getCount(){ return count; } }; int Demo_Static :: count; // int main(){ Demo_Static s1,s2,s3; cout<<"The value of a is :"<<Demo_Static::getCount(); return 0; }

Output:-

The value of a is :3

Explanation of the program

In this program, count is declared as a static variable. This variable is accessed by all objects and the value of count is the same when each object accesses it since only one copy of it is created and maintained.