Static Data Member in C++ with Example

(Last Updated On: April 2, 2021)


Static Data member like static variables, we can also define static data members of a class. The keyword static is used before the data member of a class. The characteristics of a static data member are the same as ordinary static variable. It scope is within the class in which it is defined. Its lifetime starts when the object of the class (that contains the data member) is created and ends when the program is terminated.

For many object of a class, there in only one static variable, which is created in the memory. If value of static data member is changed in one object of the class, this value can be accessed from other objects. So data of static data member can be shared among all the objects of the same class. the static data members must  also be declared and initialized after defining the class.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Example: write a program that explains the concept of static data member of a class:

static data member

In this program, class A is defined which contains a static data member ‘SS’. This class has two member functions ‘add’ and ‘print’. The ‘add’ function uses one argument. The value of argument is added to the previous values of data members. The ‘print’ function is defined to print the values of data members.

The class has one constructor. The value 0 is initialized to data member ‘M’ and ‘N’ through this constructor. The statement “int A::ss= 0;” is written after the class definition. This statement must be written to declare and initialize the static data member of the specified class.

Three objects ‘aa’, ‘bb’ and ‘cc’ are declared of the class A. the ‘add’ function is called for the object ‘aa’ to add value 2 to all data members of the class. the values of data members are printed through ‘print’ function. The ‘add’ function (with different data value) and ‘print’ function are also called for the objects ‘bb’ and  ‘cc’. the data values of data members in memory are shown in the following figure.

static data member

Please note that the contents of “SS” are incremented with a specified value. The data member “SS” is created only in one memory location and all objects of the class can share its data. The positions of objects ‘aa’, ‘bb’ and ‘cc’ and data member ‘SS’ in memory is shown in the following figure.

static data member

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply