Hello, welcome to the 54th tutorial in the c++ program series, and this program will look at static variables. statics share common error in memory. So for example, if a variable is declared as static within a class, then any object of that class will all share the same value for that variable. Also, here's another example. If for example, you have a function and you create a variable inside there, so you declare in so it's a local variable, which we discussed earlier in this series, but if you declare that static, every time that function is called, it will contain the same you can you will have the same value that was a doll previously assigned to it. And it was in the same area of memory so that way you can have persistent also, sorry, minus one black that you can add persistent values during the lifecycle of an application.
Obviously, with Application had ended and is no longer viable and removed. Let's show you how to implement static variables. It's something that is very useful indeed, especially when you're dealing with stuff like Singleton's. But you've got to be very careful when using it. Simply because you can have some conflicts. And if you start having conflicts in memory, it can cause some issues with your application.
So what we're going to do is to assume the class example, create a class called vehicle. Now we're going to create a constructor we call, no credit constructor, right? All we're going to create a static variable called int. All right. What we're going to do is simply in here. Actually, before we do this, when you create a static, you have to initialize it in the way you initialize it by pulling the data type here like inside the class.
Then you do vehicle int, V, you collect butterfingers today, yeah, int vehicle colon colon the name of the variable, which is I was gonna assign a value of 89. And that was going to create two objects are going to be called P and the P one and the W two. First of all, this will print out the value added SFC be able to be one dot. I have no idea what that is. Go build a new one. I printed a new line one.
Yeah, by the way, value of 89. But if we were to to give the beautiful to Dr. Ali equalled 900 and parenthesize your value of 900 because the a is technically only one all you variable within any object of vehicle and marks that he shared one area memory, so any changes that we do to it, it will affect all the variables because technically the only one variable which they're all sharing so this is something maybe a little complex and we are going to later in the series we are going to come across more and more complex features of c++ and programming in general. That's it for this part of the series. The next part we'll be looking at static functions. But if you have any questions regarding this or anything else because it is getting a bit complex now, feel free to message us or support us on our systems coda UK, the email will be in the description you can comment on this video or just direct messages via YouTube of law, or the requirement for source code will also be in the description.
And finally, as usual, thanks for watching and I hope you have a great day.