C++ Program using static variable


In this program, you will learn how to implement static variables in C++.


int a;

static int b;

Example: How to implement static variables in C++.

#include<iostream>

using namespace std;

class Test {
  public:
    int a;
  static int b;

  Test() {
    b = 10;
  }
  void msg() {
    cout << "static variable value is:" << b;

  }
};

int Test::b = 0;

int main() {
  Test obj;
  obj.msg();

  return 0;
}

Output:

static variable value is:10