
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Static Keyword in C++
When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name.
Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function. They are local to the block. The default value of static variable is zero. The static variables are alive till the execution of the program.
Here is the syntax of static keyword in C++ language,
static datatype variable_name = value; // Static variable static return_type function_name { // Static functions ... }
Here,
datatype − The datatype of variable like int, char, float etc.
variable_name − This is the name of variable given by user.
value − Any value to initialize the variable. By default, it is zero.
return_type − The datatype of function to return the value.
function_name − Any name to the function.
Here is an example of static variable in C++ language,
Example
#include <bits/stdc++.h> using namespace std; class Base { public : static int val; static int func(int a) { cout << "\nStatic member function called"; cout << "\nThe value of a : " << a; } }; int Base::val=28; int main() { Base b; Base::func(8); cout << "\nThe static variable value : " << b.val; return 0; }
Output
Static member function called The value of a : 8 The static variable value : 28
In the above program, a static variable is declared and a static function is defined in the class Base as shown below −
public : static int val; static int func(int a) { cout << "\nStatic member function called"; cout << "\nThe value of a : " << a; }
After the class and before main(), the static variable is initialized as follows −
int Base::val=28;
In the function main(), object of Base class is created and static variable is called. The static function is also called without using object of Base class as follows −
Base b; Base::func(8); cout << "\nThe static variable value : " << b.val;
- Related Articles
- static Keyword in Java
- static keyword in C#
- Java static keyword
- static Keyword in Java programming
- static keyword in C++ vs Java
- The static keyword and its various uses in C++
- Golang Program to Show Usage of Static keyword in Class
- Can we use "this" keyword in a static method in java?
- Is it possible to use this keyword in static context in java?
- Does static factory method internally use new keyword to create object in java?
- Can a "this" keyword be used to refer to static members in Java?\n
- Why can't we use the "super" keyword is in a static method in java?
- Static vs. Non-Static method in C#
- Static and non static blank final variables in Java
- “extern” keyword in C
