A class variable can be declared static


Then only one instance of that variable exists, shared by all instances of the class: 

#include <iostream.h>

class vector
{
public:

double x;
double y;
static int count;

vector (double a = 0, double b = 0)
{
x = a;
y = b;
count = count + 1;
}

~vector()
{
count = count - 1;
}
};

void main ()
{
vector::count = 0;

cout << "Number of vectors:" << endl;

vector a;
cout << vector::count << endl;

vector b;
cout << vector::count << endl;

vector *r, *u;

r = new vector;
cout << vector::count << endl;

u = new vector;
cout << a.count << endl;

delete (r);
cout << vector::count << endl;

delete (u);
cout << b.count << endl;
}




Added on June 5, 2007 Comment
}