1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#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;
}
Enter to Rename, Shift+Enter to Preview