Like static member variable, we can also have static member functions in a class. A member function that is declared static has the following properties:
- A static member function can have access to only other static members (functions or variables) declared in the same class
- A static member function can be called using the class name (instead of its objects) as follows:
Program below illustrates the implementation of these characteristics:
Output of the above program would be:
In the above program:
- The static member function
showcount()
displays the number of objects created till that moment - A count of number of objects created is maintained by the static variable
count
- The function
showcode()
displays the code number of each object - Note that the statement:
code=++count;
is executed wheneversetcode()
function is invoked and current value ofcount
is assigned tocode
. Since each object has its own copy ofcode
, the value contained incode
represents a unique number of its object
0 comments:
Post a Comment