Static Members in C++
We can use the static keyword to define class members as static. When we declare a member of a class 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 there are no other initialization statements, all static data is initialized to zero when the first object is created. We cannot initialize static members inside the class definition, but we can redeclare them outside the class using the scope resolution operator :: to initialize them, 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 1 every time an object is created
objectCount++;
}
double Volume()
{
return length * breadth * height;
}
private:
double length; // Length
double breadth; // Breadth
double height; // Height
};
// Initialize 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
// Print total number of objects
cout << "Total objects: " << Box::objectCount << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Constructor called.
Constructor called.
Total objects: 2
Static Member Functions
If a member function is declared as static, it can be independent of any particular object of the class. Static member functions can be called even if no objects of the class exist, and they can be accessed using only the class name and the scope resolution operator ::.
Static member functions can only access static member data, other static member functions, and functions outside the class.
Static member functions have a class scope and they cannot access the this
pointer of the class. You can use static member functions to determine whether any objects of the class have been created.
Difference between Static Member Functions and Ordinary Member Functions:
Static member functions do not have a
this
pointer and can only access static members (static member variables and static member functions).Ordinary member functions have a
this
pointer and can access any member of the class; while static member functions do not have athis
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 1 every time an object is created
objectCount++;
}
double Volume()
{
return length * breadth * height;
}
static int getCount()
{
return objectCount;
}
private:
double length; // Length
double breadth; // Breadth
double height; // Height
};
// Initialize static member of class Box
int Box::objectCount = 0;
int main(void)
{
// Print total number of objects before creating object
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
// Print total number of objects after creating object
cout << "Total objects: " << Box::getCount() << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Inital Stage Count: 0
Constructor called.
Constructor called.
Total objects: 2
class Box {
public:
double height; // Height
};
// Initialize static member of class Box
int Box::objectCount = 0;
int main(void)
{
// Output the total number of objects before creating any
cout << "Initial 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 them
cout << "Final Stage Count: " << Box::getCount() << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Initial Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2