NOTE : -
THIS CONTENT IS NOT ORIGINAL
24. Static variables inside a class definition
One or more variables in a classcan be declared static. In which case, only one instance of those variables exist, shared by all instances of the class. It must be initialised outside the class declaration :
One or more variables in a classcan be declared static. In which case, only one instance of those variables exist, shared by all instances of the class. It must be initialised outside the class declaration :
using namespace std;
#include
class vector
{
public:
double x;
double y;
static int count;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
count++;
}
~vector()
{
count--;
}
};
int vector::count = 0;
int main ()
{
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;
return 0;
}
| Output |
| 1 2 3 4 3 2 |
BIBILOGRAPHY / REFERENCE : - http://www.4p8.com/eric.brasseur/cppcen.html

No comments:
Post a Comment