Static Data Member in C++ Example

By
Last updated:

Having said that each object contains its own separate data, we must now amend that slightly. If a data item in a class is declared as static, only one such item is created for the entire class, no matter how many objects there are. A static data item is useful when all objects of the same class must share a common item of information. A static data member variable defined as static has characteristics similar to a normal static variable: It is visible only within the class, but its lifetime is the entire program.

It continues to exist even if there are no objects of the class. (See before for a discussion of static variables.) However, while a normal static variable is used to retain information between calls to a function, static class member data is used to share information among the objects of a class.

Uses of Static Data Member

Why would you want to use static member data? As an example, suppose an object needed to know how many other objects of its class were in the program. In a road racing game, for example, a race car might want to know how many other cars are still in the race.

In this case a static variable count could be included as a member of the class. All the objects would have access to this variable. It would be the same variable for all of them; they would all see the same count.

An Example of Static Class Data

Here’s an example, STATDATA, that demonstrates a simple static data member:

// statdata.cpp
// static class data
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class foo
{
private:
static int count; //only one data item for all objects
//note: “declaration” only!
public:
foo() //increments count when object created
{ count++; }
int getcount() //returns count
{ return count; }
};
//--------------------------------------------------------------
int foo::count = 0; //*definition* of count
////////////////////////////////////////////////////////////////
int main()
{
foo f1, f2, f3; //create three objects
cout << “count is “ << f1.getcount() << endl; //each object
cout << “count is “ << f2.getcount() << endl; //sees the
cout << “count is “ << f3.getcount() << endl; //same value
return 0;
}

The class foo in this example has one data item, count, which is type static int. The constructor for this class causes count to be incremented. In main() we define three objects of class foo. Since the constructor is called three times, count is incremented three times. Another member function, getcount(), returns the value in count. We call this function from all three objects, and as we expected each prints the same value. Here’s the output:

count is 3 ←—– static data
count is 3
count is 3

If we had used an ordinary automatic variable as opposed to a static variable for count, each constructor would have incremented its own private copy of count once, and the output would have been

count is 1 ←—– automatic data
count is 1
count is 1

Static class variables are not used as often as ordinary non static variables, but they are important in many situations. Figure shows how static variables compare with automatic variables.

Read More Topics
Variables in C
Importance of C
Passing arrays to function in C
User Defined Function

Santhakumar Raja

Hello The goal of this blog is to keep students informed about developments in the field of education. encourages pupils to improve as writers and readers.

For Feedback - techactive6@gmail.com

Leave a Comment