C++ Static Keyword3 min read

Static is a keyword in C++ that is when used with different types gives different meanings. static keyword can be used with variables in a function or class or with the static members of a class such as Class objects and Functions in a class.

When is used with any of the mentioned above, it cannot be modified in the future. static elements are allocated storage only once in a program lifetime.

Static Keyword can be used with the following in c++.

  1. Static variables in a functions
  2. Static Class Objects
  3. Static member Variable in class
  4. Static Methods in a class

1. Static variables in a Function

A variable declared static in a program is allocated to a static storage area. The static variables are only initialized only once in a program even if it is called multiple times in a program. It is useful when you do need to reset the counter even after multiple calls of the function.

Example of a static variable inside a function in c++

Output:


2. Static class Objects

The class objects are declared as static works the same in the same way as static variables. The objects declared static are allocated to a static storage area and have a scope till the lifetime of the program.

Example of a static class object in c++

Output:

As you can see the destructor is called after the end of the main function because the scope of static object is throughout the lifetime of the program.


3. Static Data Members inside a class:

Static variables of a class are shared by all the objects and these members are initialized once and allocated a separate single static storage. Each object does not have a separate copy of those variables.

Also, static data members are not initialized using the constructor as they are not dependent on variable initialization.

Example of a Static Data Members inside a class in c++

Output:

As you can see, it has to be explicitly initialized outside the class with a class name and scope resolution operator.


4. Static function in a class

Just like static data variables in a class, the static function also does not depend on the object of the class. We cannot call or invoke the static function of a class using object or ‘.‘ operator. But can be invoked using the class name and the scope resolution operator.

However, these functions can only access the static data members and static member functions as it does not have ‘this‘ keyword.

Example of a Static function in a class in c++

Output:


MORE

Implement Insertion sort Program in C

In this tutorial, we will learn and write a C program to implement insertion sort. Before that you may go through the following topics in …
Read More

C Program for implementation of Selection sort

In this tutorial, we will learn and write a C program to implement selection sort using function. Before that you may go through the following …
Read More

C Program to Delete a Particular element from an Array

In this tutorial, we will write a C program to delete an element in an array by specifying the value. Before that, you should have …
Read More

C Program to sort a String in Ascending order

In this tutorial, we will write a c program to sort a string array in ascending order. Before that, you may go through the following …
Read More

C Program to find Maximum occurring Character in a String

In this tutorial, we will write a program in C to find the maximum occurring character in a string. Before that, you may go through …
Read More

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More