C++ Programs‎ > ‎

Simple Program for Static Data and Member Function Using C++ Programming


C++ Simple Example Programs For Beginners

To count the object value using the storage keyword static.

ALGORITHM: 

STEP 1:  Start the program.
STEP 2:  Declare the class name as Stat with data member s and member functions.
STEP 3:  The constructor Stat() which is used to increment the value of count as 1 to to assign the variable code.
STEP 4:  The function showcode() to display the code value.
STEP 5:  The function showcount() to display the count value.
STEP 6:   Stop the program.

PROGRAM: 

#include<iostream.h>
#include<conio.h>
 
class stat
{
    int code;
    static int count;
 
   public:
    stat()
    {
      code=++count;
    }
    void showcode()
    {
      cout<<"\n\tObject number is :"<<code;
    }
    static void showcount()
    {
              cout<<"\n\tCount Objects :"<<count;
    }
};

int stat::count;
 
void main()
{
   clrscr();
   stat obj1,obj2;
 
   obj1.showcount();
   obj1.showcode();
   obj2.showcount();
   obj2.showcode();
   getch();
}
 

Output: 

Count Objects: 2
Object Number is: 1
Count Objects: 2
Object Number is: 2