Loading....
Coupon Accepted Successfully!

 

static Member Functions

Like member variables, function can also be declared as static. When a function is defined as static, it can access only static member variables and functions of the same class. The not-static members are not available to these functions. The static member function declared in public section can be invoked using its class name without using its objects. The static keyword makes the function free from the individual object of the class and its scope is global in the class without creating any side effect for other part of the program. The programmer must follow the following points while declaring static function:

  1. Just one copy of a static member is created in the memory for entire class. All objects of the class share the same copy of static member.
  2. static member function can access only static data members or functions.
  3. static member function can be invoked using class name.
  4. It is also possible to invoke static member functions using objects.
  5. When one of the objects changes the value of data member variables, the effect is visible to all the object of the class.

8.17 Write a program to declare static member functions and call them from the main() function.

Explanation: In the above program, the member variables c and functions of class bita are static. The function count() when called increases the value of static variable c. The function display() prints the current value of the variable c. The static function can be called using class name and scope access operator from the following statements:

bita::count(); // invokes count() function

bita::display(); // invokes display() function

static Private Member Function

A static member function can also be declared in private section. The private static function must be invoked using static public function. The following program illustrates the point.


8.18 Write a program to define private static member function and invoke it.

Explanation: In the above program, count() is a private static member function. The public static function display() invokes the private static function count(). The function display() also displays the value of static variable c.

static Public Member Variable

The static public member variable can also be initialized in function main() like other variables. The static member variable using class name and scope access operator can be accessed. The scope access operator is also used when variables of same name are declared in global and local scope, which is illustrated in the following program:
 

8.19 Write a program to declare static public member variable, global and local variable with the same name. Initialize and display their contents.

 

 

Explanation: In the above program, the variable c is declared and initialized in three different scopes such as global, local, and inside the class. The variable c declared inside is static variable and initialized to 22. The global variable c is initialized to 11 and local variable c is initialized to 33.

static member variable: The value of static variable is displayed using variable name preceded by class name and scope access operator as per the statement cout<<“\nClass member c = ”<<bita::c;.

Global variable: The global variable can be access using variable name preceded by scope access operator from the statement cout<<“\nGlobal variable c = ”<<::c;.

Local variable: The local variable can be access only by putting its name as per the statement cout<<“\nLocal variable c = ”<<c;.





Test Your Skills Now!
Take a Quiz now
Reviewer Name