Coupon Accepted Successfully!


static Member Variables

We noticed that each object has a separate set of data member variables in memory. The member functions are created only once and all objects share the functions. No separate copy of function of each object is created in the memory like data member variables.

It is possible to create common member variables like function using the static keyword. Once a data member variable is declared as static, only one copy of that member is created for the whole class. The static is a keyword that is used to preserve the value of a variable. When a variable is declared as static it is initialized to zero. A static function or data element is only recognized inside the scope of the present compile.

In earlier version of Turbo C++, it was not necessary to explicitly define static data members. It was linkers’ responsibility to find undefined static data. The linker would implicitly define the static data and allocate the required memory without showing error message. In new versions of Turbo C++, it is necessary to explicitly define static members.


If a local variable is declared with static keyword, it preserves the last value of the variable. A static data item is helpful when all the objects of the class should share a common data. The static data variable is accessible within the class, but its value remains in the memory throughout the whole program (Figure).

static members in memory

The class and scope of the static member variable is defined outside the class declaration as per the statement (a). The reasons are as follows:

  1. The static data members are associated with the class and not with any object.
  2. The static data members are stored individually rather than an element of an object.
  3. The static data member variable must be initialized otherwise the linker will generate an error.
  4. The memory for static data is allocated only once.
  5. Only one copy of static member variable is created for the whole class for any number of objects. All the objects have common static data member.

8.14 Write a program to declare static data member. Display the value of static data member

Explanation: In the above program, the class number has one static data variable c. The count() is a member functions’ increment value of static member variable c by one when called. The statement int number::c=0 initializes the static member with 0. It is also possible to initialize the static data members with other values. In the function main() a, b, and c are three objects of class number. Each object calls the function count(). At each call to the function count(), the variable c gets incremented and the cout statement displays the value of the variable c. The objects a, b, and c share the same copy of static data member c.

8.15 Write a program to show the difference between static and non-static member variables.

Explanation: This program compares between static and non-static member variables. The class number has two member variables c and k. The variable c is declared as static and k as normal variable. The function zero() is used to initialize the variable k with zero. The static member variable c is initialized with zero as follows:

int number::c=0; // initialization of static member variable

The function count() is used to increment values of c and k. In function main(), A, B, and C are objects of class number. The function zero() is invoked three times by object A, B, and C. Each object has its own copy of variable k, and, hence, each object invokes the function zero() to initialize its copy of k. The static member variable c is common among the object A, B, and C. Hence, it is initialized only once. Figure shows the object and member variables in memory.


static and non-static members

8.16 Write a program to enter a number. Count the total number of digits from 0 to 9 occurring from 1 to entered number.


Explanation: In the above program, the class digit is declared with one static array member num[10] and four member functions check(), show(), input(), and ini(). The function input() reads an integer through the keyboard. The entered number is passed to function check(). The function check() is invoked by function input(). The function check() separates individual digits of the entered number using repetitive modular division and division operation. The separated digits are counted and the count value is stored in the array num[10] according to the element number. The function show() displays the contents of array num[]. The function ini() is declared and when called initializes all array elements with zero. In case the array is not declared as static, this function is useful. Here, in this program the array is static, hence we initialized it with the statement int digit::num[]={0,0,0,0,0,0,0,0,0,0}. If this statement is removed, we need to call the function ini().

Test Your Skills Now!
Take a Quiz now
Reviewer Name