Back
Close

Computing with Data

elgeish
33.3K views
Previous: Virtual Functions Next: Static Variables and Functions - Part II

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
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants