static variable in c++

/*
this example show where and how
static variables are used
*/

#include <iostream>
#include <string>

//doing "using namespace std" is generally a bad practice, this is an exception
using namespace std;

class Player
{
  int health = 200;
  string name = "Name";
  
  //static keyword
   static int count = 0;
public:
  //constructor
  Player(string set_name)
    :name{set_name}
  {
    count++;
  }
  
  //destructor
  ~Player()
  {
    count--;
  }
  
  int how_many_player_are_there()
  {
    return count;
  }
  
};

int main()
{
  Player* a = new Player("some name");
  cout << "Player count: " << *a.how_many_player_are_there() << std::endl;
  
  Player* b = new Player("some name");
  cout << "Player count: " << *a.how_many_player_are_there() << std::endl;
  
  delete a;
  
  cout << "Player count: " << *b.how_many_player_are_there() << std::endl;
}

/*output:
1
2
1
*/

3.67
9
Phoenix Logan 186120 points

                                    #include&lt;iostream&gt;
//Singleton class is a class having only one instance
class SingleTon {

public:
	static SingleTon&amp; Get() {
		static SingleTon s_Instance;
		return s_Instance;
	}//there is only one instance of static functions and variables across all instances of class
	void Hellow() {}
};
void Increment() {
	int i = 0;//The life time of variable is limited to the function scope
	i++;
	std::cout &lt;&lt; i &lt;&lt; std::endl;
};//This will increment i to one and when it will reach the end bracket the lifetime of var will get  destroyed
void IncrementStaticVar() {
	static int i = 0;//The life time of this var is = to program
	i++;
	std::cout &lt;&lt; i &lt;&lt; std::endl;
}//This will increment i till the program ends
int main() {
	
	Increment();//output 1
	Increment();//output 1
	Increment();//output 1
	IncrementStaticVar();// output 2
	IncrementStaticVar();// output 3
	IncrementStaticVar();// output 4
	IncrementStaticVar();// output 5
	SingleTon::Get();
	std::cin.get();

}

3.67 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
static meaning in c++ benefit of using static keyword in c++ static keyword in c__ static examples in cpp static variable in cpp class public void static in c++ what is static type in c++ what is static keyword in cpp keyword static in c++ accesing static types in cpp is it necessary to have static variable in a static function in c++ What is the use of static variable in c++? Explain static function with example code. c++ how to use static variables what is static keyword in c++ what is static in cpp how static keyword is used in c++ static function members setting static class in c__ static c++ meaning how to declare a static variable in cpp how to declare a static variable incpp c++ static function c++ public static variables static member function in c++ how to initialize static variable in c++ what is a static function in c++ meaning of static in c++ static keyword C++ beginner practice c++ static variable file scope when is static keyword used c++ c++ static members and functions how to create a static variable in c++ global to only an object of a class what is static variable in c++ class static keyword c++ how gatee have a static value c++ public static int c++ c++ access static variable from member function static variable class c++ code for static variable in c++ how to access static member variable in c++ static and non static in c++ static variable c++ class why use static variables c++ static attribute in c++ means what are static function variables in c++ can a class be static in c++ static cplusplus static and non static variable in c++ how do static variables work in C++ how to static method in c++ static methods in cpp static method sin cpp static function and static variable what is a static function and variable c++ is c++ static what is static value in c++ declaring a static variable in a function static variable in c++ class static variable in c ++ static variables in cpp static in cpp variable can we change static value in c++ is static a keyword in c++ c++ static variable initialization c++ static variables in class static variable c++ meaning static variable in class c++ static variable in c++ call Static keyword for a class member variable? how to declare static variable in c++ Choose the correct statement about static member variable in C++. It is used to give special characteristics to an element It is allocated storage only once in a program lifetime Static keyword can be used in static class objects All A, B and C use of static word in c++ real application use of static word in c++ static method in c++ static int in cpp static keyword inside class how to use a static function in c ++ static function in class c++ how to set static variable c++ Acessing static variable in class C++ using static functions to change static variable in c++ why to use static keyword in c++ what do you mean by static variable with example in c++ static variable in function c++ static int in class how to acces static varible inside a member function in c++ static function in c++ c++ class static variable c++ static keyword how to use static variable in c++ static methids in c++ what is static c++ static method in cpp make static var in c++ examples of static in c++ use of static keyword in cpp c++ using static variables declaring a static variable in c++ static functions in class c++ declare variable static c++ why we use static keyword in cpp what is static int in c++ how to use a static variable c++ static functions and variables in c++ declaration of static variable in c++ what happens when we use new with a static variable c++ public static c++ static keyword example c++ what is a static variable in cpp c++ static int how to access static variable after end of scope c++ what is a static in c++ static type varibale in c++ how to use static in c++ c++ benefits of static operator how to use static variables inside class c++ use of static variable and static function in c++ what is a static variable in c++ static int cpp where to use static variable in c++ static in class c++ what is the scope of static variable in c++ function static cpp static int in c++ what are static variables in c++ static kwyword in c++ can an object be static in c++ how to make static variables in c++ static and non static variables in c++ using static c++ statis c++ can i change static variable value in c++ C++ static keywd static variable in cpp why we use static keyword in c++ what is static int in cpp program to demonstrate static functions c++ c++ how to change static variable c++ use static variable in class how to access static data types in cpp WHAT IS STATTIC IN C++ static variables in functions c++ static property example c$ how to change function to static cpp how to access static var inside method of a class c++ static in in c++ what is static in c++ how to use static class variable in c++ static keyword in cpp static inside local scope in c++ static inside class c++ whre are static variables c++ define static fields directly in class without using scope resolution c++ declare static variable in class c++ static variables c++ functions static variables c++ cpp static variable static variables in c++ static variables in classes c++ static keyword in c++ defining static variables in c++ how to get static input in c++ static in c++ c++ what is static static int in c++ static int in class c++ do all member inside static function are static c++ static class variable C++ static variables static variable in class static in cpp set static variable c++ c++ will a static value change static value in c++ static value c++ static varibale in c++ use static int c++ c++ static variable what does static mena in c++ how to make static variable in c++ static variable c++ what is a static variable C++ static c++ c++ static c++ static function variable static variable in c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source