Hello and welcome to the 55th tutorial in the c++ programming series, and in this part we're gonna be looking at static functions. A static function is an independent of any object of the class, as it can be called without even instantiating. A class at all. This is useful if the function is independent of the class and therefore is recommended to use static functions as it decreases the application for frankly, not creating objects everywhere just to use particular functions, we should only use tactics be the variable the function if they need otherwise, just use regular member also with static functions. The benefit or another use case is when the function can be encapsulated within a class or a group functions can. But their functionality is quite generic so they don't need their own objects.
So for example, maybe years passing in variables, and it's just outputting some piece of data based on what you've passed. But it doesn't actually care about itself or it doesn't need an object in itself doesn't need his own property, let's say. So let's go ahead and open up our project. And I was gonna want it open and be able to create a very simple class. So class at gyptian, Bray store and Republic create a now leverage constructor, we don't need that. So to create a static function, you use the keyword static first, then we'll just put your function I'm going to put a void print, actually not on the print function, then do int and math function int a and b Functions cannot be declared in an anonymous struct.
It should work. They will be heard. Maybe he needs a constructor. Oh, there we go. My bad forgot the class name. Yep.
Okay, so about that as the notification on my phone. And now we can simply implement this function tracking implemented on the keyword static you just put well the return type which is int, do vehicle colon, colon, then you put well your function name some function. And then for this, we just put well into a should be in a there any here we're simply going to return a time by B. And what we're going to do now is F to a C. Vehicle math function provided sixth and seventh, the answer should be 42. Now if we run this, open up the debug area first and now let's run it and we get the answer 42. We didn't have to create an object of this class, we were able to just access this method directly.
But if we just remove the keywords static for a moment, I really say build failed court and non static member function without an object argument. Basically, the function isn't static. You need to have an object instantiated and initialize otherwise if it is that if you can just access that method just by querying the class name, colon, comma, and then the actual function itself. So I think this has some great advantages, like said, if you have some functionality that can be encapsulated within the vehicle class, give me an example creating a game recently, and we have a little collision, or a few collision check feature functions, I should say. And they didn't really need their own property, but they were doing stuff and they were doing some tasks. But all they actually needed were four variables, I believe, that were passed in.
So we encapsulated them these methods into a class called I believe collision manager. And then all we did was to collision manager colon colon, then whatever it was going and yet yet yeah, ignore big castle fanatic. Yeah, that's what we did. We can lated it into a class and then we could just access it by saying the class name colon, colon and the function name. And that was so beneficial. We didn't have to create keep creating objects anywhere that we wanted to use it.
So that's it for this part of the series. If you have any questions feel free to message us at support at Sumner systems calm okay. The email will be in the description. You can comment on this video just directly messages via YouTube, or the recording for source code will also be in the description. The next part of this series we'll be looking at template classes. And as usual, thank you for watching and I hope you have a nice day.