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 {
double x;
double y;
static int num_objects;
Point() {
// increments counter when a new object is created
// 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;
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content