When a variable is declared and if not initialized, it contains garbage value. The programmer needs to assign appropriate value to the variable. The compiler itself cannot carry out the process of initialization of a variable. The programmer needs to explicitly assign a value to the variable. Initialization prevents the variable from containing garbage value.
In the above example, height is a variable of float type. It holds any garbage value before initialization. In the next statement, variable height is initialized with 5.5.
C++ handles abstract data type, which is the combination of one or more basic data types. An object holds copies of one or more individual data member variables. When an object is created, its data member contains garbage value.
We learnt in the previous chapter that declaring static member variables facilitates the programmer to initialize member variables with desired values. The drawback of static members is that only one copy of the static member is created for the entire class. All objects share the same copy, which does not provide security.
Another approach is to define an object as static. When an object is declared as static, all its member variables are initialized to zero. This is a useful approach. Declaring static object does not create common copies of member variables. Every static object has its own set of member variables. The drawback of static object is that the contents of static object remain throughout the program occupying more memory space. The following program illustrates this point.
9.1 Write a program to declare static objects and display their contents.
Explanation: In the above program, objects A and B are declared as static. Their data member variables are initialized to zero. The member function show() increments value of member variables by one and displays them. Both the objects A and B invoke the function show(). The contents displayed are same for both the objects. Therefore, we can say that individual copies are created for each static object.
The main drawback of static object is that its value remains in the memory throughout the program. The following program illustrates that the static object remains in the memory even if it goes out of scope.
9.2 Write a program to demonstrate that static object persists its values.
Explanation: In the above program, function display() is a normal function. The member function show() performs the same task as described in the previous program. The static object K is declared in the function display(). The object K is local object of function display(). The function main() invokes function display() two times. In the first call, the contents displayed are one and one. In the second call, the contents displayed are two and two. It means in second call, the previous values are not cleared. Hence, we can say that static objects are intact or persist their values. The static object K remains throughout the program and is not destroyed even if execution of display() terminates.