In a C++ programming we can create static function and variable by using the “static” keyword.
Static Variables
In C++ programming we can create a static variable by placing the “static” keyword before the variable declaration.
Once a variables declared as static, only one copy of that variable is created for the whole class.
When a variable is declared as static, by default it is initialize to zero.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> using namespace std; class Example { static int x; int y; public: void show() { y = 0; } void count() { ++x; ++y; cout << "\n value of x=" << x; cout << "\n value of y=" << y; } }; int Example::x = 0; //initialization of static member variable int main() { Example a, b, c; a.show(); b.show(); c.show(); a.count(); b.count(); c.count(); } |
Output
1 2 3 4 5 6 | value of x=1 value of y=1 value of x=2 value of y=1 value of x=3 value of y=1 |
Note: In the above figure we have a two object (object 1 & object 2) we object occupies a separate memory for variable(for ex: for variable 1 , object1 and object2 both will occupy the memory) .
But when a variable declared as a static then only one memory location is occupy for all object.
Static function
In C++ programming we can create a static function by placing the “static” keyword before the function declaration .
A static function can only assess static member variable and member function.
In a C++ it is also possible to call a static member function without object.
Static public member function:
Example: WAP to declare public member function and call them from main().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; class Smember { static int a; public: static void count() { a++; } static void show() { cout << a<<endl; } }; int Smember::a = 2; int main() { Smember::show(); // call show() without object Smember::count(); // call count() without object Smember::count(); Smember::show(); } |
Static function can’t assess normal variable
A static function can only assess static variable .
A static function can’t access normal function.
If we try to access normal function within the static function then we will get an error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> using namespace std; class Smember { int a; // normal variable public: static void count() { a = 10; } // error, static function can’t access normal variable static void show() { cout << a; // error, static function can’t access normal variable } }; int main() { Smember::show(); // call show() without object Smember::count(); // call count() without object } |
Output
1 | error, static function can’t access normal variable |
Static Object
In a C++ programming like a static variable & function we can also declare a static object.
The object is a composition of one or more member variable of the class.
When we declare a static object by default it initializes all variables of class with zero.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> using namespace std; class Smember { int a; public: void show() { cout << a << endl; } void increment() { a = 10; } }; int main() { static Smember obj; // by default, static object initialize all variable of class with zero obj.show(); obj.increment(); obj.show(); } |
Output
1 2 | 0 10 |