C++ Programming Blog

 
 
 

Image

 #include<iostream.h>
 #include<conio.h>
 #include<stdio.h>


 //------------------------------  check  --------------------------------//


 class check
    {
       private:
        int data1;
        static int data2;

       public:
        void setvalue(int);
        void showdata();
    };


 //---------------------  Static data initialization  --------------------//


 int check :: data2=0;



 //---------------------------  setvalue(int)  ---------------------------//


 void check :: setvalue(int value)
    {
       data1=value;
       data2=value;
    }


 //---------------------------  showdata( )  -----------------------------//


 void check::showdata()
    {
       cout<<\"\\t ordinary data = data1 = \"<<data1<<endl;
       cout<<\"\\t static data = data2 = \"<<data2<<endl;
    }


 main( )
    {
       clrscr();

       check obj1;
       check obj2;

       cout<<\"\\n Before initilization of data1\"<<endl;
       obj1.showdata();

       cout<<\"\\n After initialization of data1\"<<endl;
       obj1.setvalue(5);
       obj1.showdata();

       cout<<\"\\n Data2 is \"<<endl;
       obj2.showdata();

       getch();
       return 0;
    }

 
 
Didn't find what you were looking for? Find more on Program to illustrate static class data