static member function in c++ code example
Example 1: 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
*/
Example 2: static class in C++
#include <iostream>
using namespace std;
class Box {
public:
static int objectCount;
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// Increase every time object is created
objectCount++;
}
double Volume() {
return length * breadth * height;
}
static int getCount() {
return objectCount;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Initialize static member of class Box
int Box::objectCount = 0;
int main(void) {
// Print total number of objects before creating object.
cout << "Inital Stage Count: " << Box::getCount() << endl;
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
// Print total number of objects after creating object.
cout << "Final Stage Count: " << Box::getCount() << endl;
return 0;
}
Example 3: static inside local scope in c++
#include<iostream>
//Singleton class is a class having only one instance
class SingleTon {
public:
static SingleTon& 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 << i << 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 << i << 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();
}
Example 4: static in class c++
#include <iostream>
class Entity {
public:
static int x,y;
static void Print() {
std::cout << x << ", " << y << std::endl;
}// sta1tic methods can't access class non-static members
};
int Entity:: x;
int Entity:: y;// variable x and y are just in a name space and we declared them here
int main() {
Entity e;
Entity e1;
e.x = 5;
e.y = 6;
e1.x = 10;
e1.y = 10;
e.Print();//output => 10 because variable x and y being static point to same block of memory
e1.Print();//output => 10 because variable x and y being static point to same block of memory
Entity::x; //you can also acess static variables and functions like this without creating an instance
Entity::Print(); //you can also acess static variables and functions like this without creating an instance
std::cin.get();
}
Example 5: static class in C++
#include <iostream>
using namespace std;
class Box {
public:
static int objectCount;
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// Increase every time object is created
objectCount++;
}
double Volume() {
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Initialize static member of class Box
int Box::objectCount = 0;
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
// Print total number of objects.
cout << "Total objects: " << Box::objectCount << endl;
return 0;
}