Static Keywords in C++
Static keyword in C++: Static Variables, Static Member of Calss
In C++, static keyword can be used in a number of ways as explained below. Static variables, Static Data Members of a Class and Static Members Functions of a Class.
‘Static’ keyword is used to make use of static memory. Static memory remains live till the end of the program execution. If the element of a program is declared as static then it will use this special type of static memory and will keep its value available as long as the program in executing.
In C++, static keyword can be used in a number of ways as explained below.
If a variable in declared in a function then it will occupy a memory during the execution of the function and will release the memory as soon as the function ends as shown below.
The output of the above code is as below. It is important to note that variable ‘I’ is declared as non-static at line 8 and thus will be re-initialized every time the function abc() is called.
But in contrast if the variable in above function is declared as static then it will retain its value till the program lifetime and will not be re-initialized every time on function call as shown in the following example.
The output of the above code is as below. It is important to note that variable ‘I’ is declared as static this time at line 8 and will retain its last value when the function abc() is called.
Static Data Members of a Class
Static data members of a class are shared among all the objects of a class. All the objects of the same class access these variables from the static memory of the class and thus individual copies of these variables are not given to each object of the class. It also means that if an object of the class changes the value of a static data member then this change is also obtained by all the objects of the same class as shown in the following example.
It is important to note that static data members of a class are not initialized by the constructors of the class rather they are initialized outside the class as mentioned at line 14 in the following code.
In the above example, static data member is declared at line 9 and initialized at line 14. We can see that the static data member is assigned a value ‘1’ at line 18 by obj1 of the class but when we display the same variable by obj2 at line 22 then the same value is printed as assigned by obj1 which means that both obj1 and obj2 share the same values of variable a. The output of the above code is as below.
Static Members Functions of a Class
Static member functions of a class can be accessed directly without defining object of the class or through the object of the class. For instance display() is declared as static in the following example and called at lines 21 and 22. At line 21, it is called by using the object of the class while at line 22 it is called directly without using the object of the class. If the same method is declared as non-static then we will get an error at line 22 as non-static functions are not allowed directly.
The output of the above code is as below.
More Related Articles For You
- What is C++
- C++ and Object Oriented Programming OOPS concepts
- Syntax and Structure of C++ program
- Data Types in C++
- C++ Variables
- Types of operators in C++
- Decision making in C++
- C++ Loop Types
- Storage Classes in C++
- Functions in C++
- Classes and Objects in C++
- Access controls in C++ Classes
- Defining Class and Object in C++
- Accessing Public and Private Data Members in C++
- Member Functions in Classes
- Types of Member Functions in C++
- Inline Functions in C++
- Namespaces in C++
- Function Overloading in Classes in C++
- Constructors and Destructors in C++
- Const Keyword in C++ Programming
- References in C++
- Copy Constructor in C++
- Pointer to Members in C++ Classes
- Introduction to Inheritance
- Types of Inheritance
- Order of Constructor Call in Inheritance
- Upcasting in C++
- Function Overriding in C++
- Virtual Functions in C++
- Abstract Class and Pure Virtual Functions in C++
- Virtual Destructors in C++
- Operator Overloading in C++