It just scarred static variable and static functions.
So it's very interesting topic in C++ if you are looking at static member variables.
So we will be using a keyword like static to declare the static variable or static friendship between
declared inside class.
So what does the use of static variables or static functions?
So static remember, variables are the member variables that belongs to the class rather than the object
of that class.
So it will be a variable which is dedicated to that class, which means that the value of that object
or that particular variable will be always constant, constant in the sense, if I'm trying to create
an object of a class and I'm trying to create that variable.
It will be common across all the classes, not all the classes for all the objects created across that
class.
So only one instance of that variable will be created no matter how many objects of the same class at
later.
So if I have 10 objects, I'll pick last name Class E then and I'm declaring one static variable.
Only one static variable will be created.
OK, let the static variable will be distance.
OK.
That will be the name of that variable released and static int distance.
That is that of how you create that static variable on a static function.
So what would happen if I'm creating multiple objects of that pass, even if I'm creating 10 objects
of the class like object or an object to object to two object 10 that all the variables inside objects
named distance will be the same.
So how are you going to understand that?
It can be the by seeing the.
So let's see a demo to understand this concept.
So let's move to the static program here again.
See, I have created a class named box and you have a static object.
This is our static object.
So we had, OK, I'm sticking it out here, constructor.
We will be covering shortly.
So constructor will be setting the values of this meeting was ill be and it's it will be length and
height.
So you will be able to calculate calculate the volume of a box.
It's used by Ellen to be in the edge length interpreted into how it will calculate the warning.
So we have a function for calculating the volume of the box.
So this glass box is about all, about connected to a box and we have a box which is having a length,
bright and height.
And you will be calculating you'll be able to calculate the volume of the box.
So here we have the variables created for that length, but eight which are private and you have the
constructor to get to give the values of the box.
So it will be debris equal to two point zero number B equal to two point zero eight equal to 2.0.
So length would be too, but it will be two and three b two.
Then I am assigning this value of length equal to L and this value bracket equal to be and this value
will be each.
So the values of land, Brent and height will become two two.
And you know what we are doing inside this constructor.
I have this object count as a counter variable created but just static.
You have been taught that if you include a static keyword in front of any variable or declaration inside
a glass, that will become a static variable.
So what would happen?
I'm calling the get go on function.
This is our step function, but this is our static.
So we have a static function and a static variable out here.
Now I'm trying to increment the object count or that variable count.
So each time that object is created, this column, this increment.
So let's see how many object will be creating.
So we created a static number object count equal to zero plus the count will be zero, then going within
into main function initial stage count.
You will be showing the false start.
Get done.
So how are you going to access the static variable?
You have to use the syntax you have to use.
You have to use this update to score a resolution on Theta.
OK.
Like four cards.
OK.
How you can create score resolution once you do this, and again, you add this.
So this will be the SCOP resolution operator.
Why we are using this to access the that big meeting was so static and shoots so banks are to go down.
We are accessing that function.
What will happen?
We have the object can be equal to zero.
So blocks that count return object count.
What is the value of object count?
It will be zero.
No box one.
We will be passing the values three point three one point twenty one point five.
That is box one and Box two.
OK, what happens when we have Box one created again?
It will be going to the constructor.
All of that glass glass, we've been preventing the object count.
OK, then you will be incriminating.
The currently the value of object carries obviously count is zero.
It is incremental to one.
So when this object was created, it will become Object County Court to it too.
And when this glass, this object is created, object count will be becoming three.
So why are you not expecting this behavior?
That is what you need to understand in this program, because that variable object down to static.
So even if different objects are created, the value is not initialized to zero each time because it
will maintain only one copy.
So all these objects are accessing that same medium.
But if you create an orange static variable, what happens is all the objects will have different variables.
It will not be blinding to a single variable.
So that system or these static variables, I didn't include this example.
So that's why I wanted to point out how the static object or static variable will be created in this
X-number of a cube instead of box.
Here you can see a few, and it's having sight equal to five side equal bay density.
And so each time you create that variable or to object for a glass named cube and it's having its non
static variable part say three different variables would be created.
And if you have created one a variable called object down, which is static.
This will be common to all three objects.
So Cube one, you may be having a pointing to the same object.
This will also binding to the same object.
This will also be binding to the same object.
So that's one point which you will need to remember when you study about the static variable.