How to make Static Data Members in C++


Consider the following program which demonstrates the use of static data member count. 
The variable count is declared static in the class but initialized to 0 out side the class .


#include <iostream.h>

#include <conio.h>

Class counter
{
private:

static int count;
public:
          void disp ();
};

int counter::count=0;

void counter::disp()

{
count++

cout<< "The present value of count is" <<count<<"/n";

}

Main()

{
counter cntl;

for (int i=0; i<5; i++)

cnt1.disp();

getche();

}

you should get the following output from this program.

The present value of count is 1

The present value of count is 2

The present value of count is 3

The present value of count is 4

The present value of count is 5





















0 comments:

Post a Comment