Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
// Program to illustrate static member function

#include<iostream.h>
#include<iomanip.h>
class example
{
     private:
          int x;
          int y;
     public:
          static int number;
          void setvalue(int p,int q)
          {
               x=p;
               y=q;
               number++;
          }

          static int count()
          {
               return number;
          }

          void putvalue()
          {
               cout<<"\n Values of x and Y :"<<setw(2)<<x<<setw(2)<<y;
          }
};


int example::number=0;


void main()
{
     example obj1;
     obj1.setvalue(3,2);
     obj1.putvalue();
     cout<<"\n Number of objects created:"<<example::count();

     example obj2;
     obj2.setvalue(5,6);
     obj2.putvalue();
     cout<<"\n Number of objects created:"<<example::count()<<endl;
}



Test data

Values of x and y : 3  2
Number of objects created : 1
Values of x and y : 5  6
Number of objects created : 2