Static Data Members in C++ & Static Member Functions in C++|C++ programming tutorials for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] yo what's going on guys done my offer simple snippets back with another video tutorial and in this video tutorial we'll be covering the topic of starting data members and Static member functions so I wanted to cover this topic v back in this entire doodle playlist but then all the other topics were lined up and I missed out on this topic so in this video we will extensively covered the concept of static data members and Static member functions also if you are new on this channel make sure you subscribe to this channel because there are a lot of information technology oriented video tutorials on this channel and a lot of coming soon as well so you'll get notified when I upload the next video tutorial so turn on the notifications as well so with that being said let's get started so we will first go through a little bit of theory on static data members and then we will jump into the programming part so a static data member is shared by all objects of the class so right now just let's go through this theory and then we will understand what exactly each and every statement means when we actually see the program as well so that time it will be more clear so let me just first leave out all these points all static data is initialized to zero when the first object is created if no other initial initialization is present we can't put it in any class definition but it can be initialized outside the class using the scope resolution operator which is two times : to identify which class it belongs to and when we declare a member of a class as static it means no matter how many objects of the class are created there is only one copy of static member okay so I just went through the entire four different points theoretical point of starting data members so let's let's imagine a class that is having an integer variable and if we make it static and if we have initialized to find if any of the objects that we create of this class will have the same copy so there will be only one copy of that data member being shared amongst object one object to object three and n number of objects that we create after that - in general scenarios when it is not static every object will have their own integer variable and we can assign different values but in this case if it is a static data member all the objects will have only one copy of the data member and it will have only one value so where well does this situation or where does this come into picture and where does this static data member become advantageous to us so say for example you have a class which is creating an new creating n number of objects and you want to keep a track of the number of objects being created so in that case a starting data member see for example count can help you keep a track of the number of objects being created so let's try to see a program and you get a very clear understanding of what I am trying to say so let's quickly jump into the Devi C++ ID okay so as you can see on the screen I have a basic layout of C++ program written out so make sure to type along with me so that you get the best practice and let's start off with creating a class well it's a class myclass you can name your class anything you want just this is just for a presentation purpose so in the public part I'm going to create an integer variable so I'll say int X and also I'm going to create a static variable and say static int count so that's about it these are the two variables that I want to create now as I mentioned in the theory to initialize the static data member you have to initialize it outside the class so your I'll initialize it so in order to initialize it you have to use the class name my class if you use the colon operator or scope resolution operator that is two times colon and then you can access the two variables you can see now currently you can access both the variables because it is in public public section but right now we just want to initialize this count variable we will say count equals to zero so this is how you initialize static data members you cannot do it inside the class so inside the public section I can create a constructor so I will say my class so this is a default constructor and what I am trying to do over here is every time an object is created this default constructor is going to be called so that's that's the purpose of constructor and that's how constructors work so this is a LeFort constructor so what I'm going to do is every time an object is created I'm going to increment the value of count oh and by the way we can always access the static data member in the member functions because ultimately it is a class level data member and it belongs to the class only yes that it is not an object level it is at a class level and you do not need an object to call or access the value of the static data member and you will see that when we actually go ahead and code in the main main function okay so let's try to save this I just saved it as untitled - dot CPP you can save it anywhere with any name now what I'm going to do is in the main function I'm going to directly bring the counter value so I'll say initial count and then again I'll use the Mike class classname scope resolution operator and print the count value and let's see you this and let's try to compile and run this and in the initialization I just need a mistake and I forgot that we have to also have a data type over here so the data type would be the data type of the static data member variable so yeah this is the complete initialization don't forget to actually add the data member or data type of the static data member so that's how you initialize it let's save this and I'm sure this won't run now go to compile and run okay so there you go you can see the initial count equals to zero so this is what it was initialized to now notice that we did not have to create any object and we could directly access this count starting data member even then we did not have any object in presence we haven't read to created any object so this is the functionality of static data member that it is at a class level and it is not at an object level wherein we do not need any object to access the static data member now what I'm going to do is I'm going to create one object obj1 and then I'm again going to print this I will say count after one variable count after one object and just print an Endon so that it goes on the next line so what happens at this line is the default constructor is called [Applause] so since default constructor is called the value of count is incremented so initially it was zero but now it is incremented by one so it should print value one so let's see if the add is to go to execute compile and run and there you go you can see count after one object is one okay so what I'm going to do is I'm going to create one more variable I will say comma owe me J - I will say count up to two objects now it should show two so let's see because the concept M is going to run two times and the count starting data member since only one copy shared between the two when it will say plus plus two times it would be two so let's say compile and run and they you can see count off to two objects equal to 2 so this I can keep track of how many objects I have created so it can act as a counter variable so now let's let's see the concept of what is a static data member function and not a data member I am talking about member function okay so by declaring a member function as static you make it independent of any particular object of the class again as you as you already know when we use the static keyword for a data member it becomes a class level data member similarly with member functions when we add a static keyword that function can be directly accessed using the class name the scope resolution operator and the function name so you do not have to explicitly create an object and a static member function as I told can be called even if no objects of the class exist and the static functions are accessed using the class name and the scope resolution operator so a static member function can only access static data member other static member functions and any other function from outside the class so this is one very important key point that I have written in yellow that is a static member function can only access static data members and other static member functions and any other function from outside the class static member functions have a class group and they do not have access to this pointer of the class so there is always at this point perhaps a corresponding to every class but since static member functions is at a class level we cannot use that pointer and you could use a static member function to determine whether some objects of the class have been created or not so that's just one use case which is which comes along with static member function so let's try to use a static member function in our existing program that we have seen up until now so we just saw the theory of static number function let's try to create one so I'll say static in get count so you can see to make a function static you just have to add the keyword static just as we added for the data member inside this I'm going to say return count so as I mentioned in the theory static data members or static member functions can only access static data members so it is a returning count which is also static so you can see there is one more non static data member index let's try to return that first let's save this and if I execute it should throw an error so there you go you can see member function in static number box in static into my class get count in values of member my class colon connects in static member function which means that it is not allowing us to return this value because it is a non-static number function so let's try to return count and it should work let's try to compile and run you can see it worked which means that it was not giving us any error now instead of directly accessing this count as a data member I can also use the get count function so if I say get count you can see I can directly access that as well because again this is also in the public part now I can keep this in the protected part or private part as well so let me try that so I kept the data members both the meta members the static and the non static data members in the private part let's try to save this and let's see if it works and I can have to change the data member to member function because then now that I've made it private I cannot directly access it outside let's try to compile and run and there you go you can see now I'm using the static member function to access the static data member which is now in the private domain of the class so if I would have directly tried to access count over your I think it should throw an error yeah it is throwing an error because count is now made to private so in order to access the count which is a static data member inside the private part of the class I have to use a public function get count which is again static which is giving us indirectly the static data member count so you have to use get count okay so this was the usage of static data members and Static member functions and you can use them basically to keep a count of number of objects that you create for a particular class so that at any moment you can just use the get count function to know how many objects are being created and since only one copy is being shared so no multiple data members are going to be created only one data member is going to be there for the entire class objects so that would be only for that particular class so all the objects of this my class that we've created will share the same copy of the static data member and it can have multiple types and you can see this is how we initialize it we have to initialize it outside the class and then we can increment or decrement it in our member functions according to our need here I'm using it as a counter so I'm just incrementing it by incrementing its value at every object creation so it since the default concept is called at every object creation I'm using it as a counter so yeah that's it for this video guys I hope understood what static data members are and what our static number functions and how to use them and how they are different from our regular data members and member functions now if you liked this video give it a thumbs up share it with your friends and make sure you subscribe to this channel peace
Info
Channel: Simple Snippets
Views: 56,438
Rating: undefined out of 5
Keywords: static member function program in c++, static member variable and function in c++, static member function in c++, static data member in c++ tutorial, static data member function in c++, static data members in c++, c++ static members, c++ static member functions, static data in c++, static data member in c++, static member variables c++, static data member and member function, c++ static data member, c++ static variables, simple snippets, c++ programming, static member functions
Id: 1QTZeeDL0bc
Channel Id: undefined
Length: 11min 14sec (674 seconds)
Published: Thu Jan 25 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.