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

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …