static variable in c++
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
*/
Also in C++:
- Title
- compare values within within a vector c++
- Category
- C++
- Title
- delay millis arduino
- Category
- C++
- Title
- Find the minimum difference between pairs in a simple path of tree C++
- Category
- C++
- Title
- initialise 2d vector in c++
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- new class * [] c++
- Category
- C++
- Title
- range of int
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- int random string generator c++
- Category
- C++
- Title
- set mimetype validation in mongoose
- Category
- C++
- Title
- what is difffrence between s.length() and s.size()
- Category
- C++
- Title
- c++ crash windows
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- Combination Sum
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- how to execute c++ program in cmd
- Category
- C++
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- unordered_map c++
- Category
- C++
- Title
- c++ string to int
- Category
- C++
- Title
- apple and orange hackerrank solution in c++
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- RLE Encoding/Compression c++
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- monotonic deque
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- singleton c++
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- bool function in c++
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- c++ delete dynamically allocated array
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- heap in cpp stl
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- ue4 c++ struct
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- insert elements in array in c++11
- Category
- C++
- Title
- find in vector in c++
- Category
- C++
- Title
- unsorted array to bst
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- elseif c++
- Category
- C++
- Title
- mingw32/bin/ld.exe: C:\Users\mfrom\AppData\Local\Temp\ccSKcRks.o:PizzaPi.cpp:(.text$_ZN5PizzaC2Ev[__ZN5PizzaC2Ev]+0xa): undefined reference to `vtable for Pizza' collect2.exe: error: ld returned 1 exit status
- Category
- C++
- Title
- c++ dereference a pointer
- Category
- C++
- Title
- cpp create lambda with recursion
- Category
- C++
- Title
- function declerations in C++
- Category
- C++
- Title
- howt o initialize 3d vector in c++
- Category
- C++
- Title
- c++ convert const char* to LPCWSTR
- Category
- C++
- Title
- runtime array size c++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- cannot jump from switch statement to this case label c++
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- c++ ros publisher
- Category
- C++
- Title
- sqrt in c++
- Category
- C++
- Title
- primos menores que
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- min heap declaration in c++ stl
- Category
- C++
- Title
- expected unqualified-id before 'if'
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- decimal to hex cpp
- Category
- C++
- Title
- prefix sum array
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- Qt asynchronous HTTP request
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- UPARAM(ref)
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- recursion in cpp with reference
- Category
- C++
- Title
- c++ functions
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- c++ function return pointer to itself
- Category
- C++
- Title
- flake8 max line length
- Category
- C++
- Title
- how to get a letter from the user c++ string
- Category
- C++
- Title
- how to append two vectors in c++
- Category
- C++
- Title
- coping 2d vector in cpp
- Category
- C++
- Title
- for c++
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- how to convert string to int c++
- Category
- C++
- Title
- c++ write string
- Category
- C++
- Title
- access last element in vector in c++
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- variabvles in c++
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- how to check type in c++
- Category
- C++
- Title
- sum of integer in array c++
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- stack c++
- Category
- C++
- Title
- c++ looping through a vector
- Category
- C++
- Title
- c++ tutorial
- Category
- C++
- Title
- digitalwrite C++
- Category
- C++