Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Static Variables and Functions - Part I

#include <iostream>
using namespace std;
class Point {
private:
double x;
double y;
static int num_objects;
public:
Point() {
// increments counter when a new object is created
++num_objects;
}
// even though this method does not modify any members,
// static member functions cannot be qualified as const
static int get_num_points() {
return num_objects;
}
};
// defines the static field num_objects and initializes it to 0
int Point::num_objects = 0;
int main() {
Point p1;
Point p2;
Point p3;
cout << "number of point objects: "
<< Point::get_num_points() << endl;
return 0;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content