C++ Class Static Members

C++ Classes & Objects

We can usestaticThe keyword is used to define class members as static. When we declare a class member as static, it means that no matter how many objects of the class are created, there is only one copy of the static member.

Static members are shared among all objects of the class. If no other initialization statements exist, all static data will be initialized to zero when the first object is created. We cannot place the initialization of static members inside the class definition, but we can do it outside the class using the scope resolution operator.::To re-declare the static variable in order to initialize it, as shown in the following example.

The following example helps to better understand the concept of static member data:

Example

#include <iostream> using namespace std; class Box { public: static int objectCount; // Constructor definition Box(double l=2.0, double b=2.0, double h=2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; // Increment by 1 each time an object is created objectCount++; } double Volume() { return length * breadth * height; } private: double length; // Length double breadth; // Width double height; // Height }; // Initialize the static member of class Box int Box::objectCount = 0; int main(void) { Box Box1(3.3, 1.2, 1.5); // Declare box1 Box Box2(8.5, 6.0, 2.0); // Declare box2 // Output the total number of objects cout << "Total objects: " << Box::objectCount << endl; return 0; }

When the above code is compiled and executed, it produces the following results:

Constructor called.
Constructor called.
Total objects: 2

static member functions

If a function member is declared as static, the function can be made independent of any particular object of the class. Static member functions can be called even when no class object exists,Static FunctionsSimply use the class name plus the scope resolution operator::then we can access it.

Static member functions can only access static member data, other static member functions, and other functions outside the class.

Static member functions have class scope; they cannot access the this pointer of the class. You can use static member functions to determine whether certain objects of the class have been created.

Differences between static member functions and ordinary member functions:

  • Static member functions do not have a this pointer and can only access static members (including static member variables and static member functions).
  • Ordinary member functions have a this pointer and can access any member of the class; static member functions do not have a this pointer.

The following example helps to better understand the concept of static member functions:

Example

#include <iostream> using namespace std; class Box { public: static int objectCount; // Constructor definition Box(double l=2.0, double b=2.0, double h=2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; // Increment by 1 each time an object is created objectCount++; } double Volume() { return length * breadth * height; } static int getCount() { return objectCount; } private: double length; // Length double breadth; // Width double height; // Height }; // Initialize the static member of class Box int Box::objectCount = 0; int main(void) { // Output the total number of objects before creating objects cout << "Inital Stage Count: " << Box::getCount() << endl; Box Box1(3.3, 1.2, 1.5); // Declare box1 Box Box2(8.5, 6.0, 2.0); // Declare box2 // Output the total number of objects after creating objects cout << "Final Stage Count: " << Box::getCount() << endl; return 0; }

When the above code is compiled and executed, it produces the following results:

Inital Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2

C++ Classes & Objects

other extensions