Static Data Members Example 1 | C++ Tutorial | Mr. Kishore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to narration this is sure and today we are going to continue static data members okay in C++ we are having static members and we are going to use two types of static members one is what static data members another one is static member functions now in this session I am going to cover what is a static data member and when it is required okay next like the general variables like the general data members already we know that in C language also we are having static variables okay in C language already we have used static variables similar to that in C++ classes also we can declare static data members okay when it is a normal variable what it is called static variable but when it is declared inside a class what it is called static data member now how the static data member is declared and where it is stored here the point is a class data member can also declare as a static okay here the pointage one class data member can be declared as status under it is initialized with the 0 automatically ok generally we know that whenever a variable is not initialized the default value becomes garbage when it is local variable but when a static variable is declared automatically it is initialized with a 0 and another important point is another important point is suppose we are having a class and we are having a data member called int a in that class suppose from that class we have defined three objects now what happens all the objects are having their local variables a okay for example just watch this suppose class sample or test what it may be and here one data member I into a okay fine now the class is completed for example now from this class I am going to define three objects test t1 t2 t3 now what happens here three objects are different k1 object as usual t2 object t3 object okay now we are having three objects and here the major point is all these objects are having local variable called a that means here one is this here one is this here one is there now with this what happens suppose here I have entered ten it doesn't reflects on this one so well here I have entered country it doesn't affect on this one or this one that means that having their own memory locations because of they have created three times in three memory locations but when it is static when it is static it will become class member it will become class number not the object member but objects are what instance of the classes that's why they are able to access this class member directly by using dot operator that's why here one important point is whenever it is a normal data member okay each object contains one instance of that data member when it is static data member what happens it will become class number not the object member that means now three copies are not created here three copies are not created only one copy is created and it is resided inside the class that means a static data member is not nothing but the class member not the object member but it is executed by the objects of that class and here another interesting thing is static data members are also access it with the class name directly actually we are having a rule in C++ what every data number should be accessed every data number shall have access it with number functions of that class using a object name using object but when it is static data member we can access the static data member directly to the class name by using the syntax tester colon colon a equal to for example hundred now watch this test is what here class them now this class is directly accessing a and now a value becomes 100 under usual suppose there is a get function inside this class now we can use T 1 dot gate and it is also extinguish one the same meaning that means static data members are accessible by both the classes and as well as objects and another major concept is what static data members initialized with the 0 automatically and here one more point we have to discuss generally in data members we are having only one part declaration but but static data number is having two parts one is declared it another one is defining it here the rule is static data member should be declared inside the class static data member should be declared inside the class using static keyword and it should be defined outside the class using this syntax first data type int later class name test next : : remember a equal to some value but valleys not ment great because of when value is not passed automatically dear initializer with a zero the SEC int test : : a equal to value now for example equal to 10 now it is initially is revisited suppose if you are not going to initialize with the 10 automatically this number becomes a zero now this part is called declared and this one is called define okay and next another important thing is stat data members static data members are created in data segment which is public area okay static data members are created in data segment which is public area that's why any object can action this one okay means all the objects can share this same data that is why static data members are useful whenever you are going to share one data member through all the objects okay whenever you want to share one data number through all the objects of that class then declare the data member using a static keyword now I will show you how to access a static data member over here how the access static data member directly with the class name first of all I am going to show you class test here in public area static int next one function also the wide gate inside gate function I am going to write like this I equal to 10 okay next C out a equal to a candle here watch it static int a is declared as a static data number and to access this data member I have written a function called get inside gate function a value becomes 10 C of a equal to okay another point is okay another point is here it is what static data number declaration okay static data member declaration finish it later here we have to pass some value for this you know okay and now a value equal to value also possible later watch it is and here already we have disgusted that every static data member is having two parts one is declared apart another one is what define at the part one is declared another one is define that's why declaration completely now define it means we have to declare define the value that's why first of all written data right ind extra class name class name is what test : : undead a number name is what a okay now what happens here test : : a is going to okay define it with initial value 0 with initial value 0 okay fine later now main ok and here directly I am going to use test : : a equal to hundred watch it generally data number should be called with number function but a say what static data member that's why we can access the a directly through the class name okay next here I want to do one more thing okay what it is means here al is what 10 I am going to remove this on here I have is any 10 int test : : equal to 10 and now here I want to put this value okay and means I want to print this well so here watch it what I am doing here directly test : : okay here ll becomes 100 now here there is another function called get I want to access the CA value through the gate function thus say faster I am going to declare a object like this test te okay otherwise you can print this value by using off what see out suppose printed is really using three out see out okay equal to test : : a ndele that's it now what happened this a value becomes okay 100 and it is going to print here and here we have access to the a value directly through the class name next here I want to access the a value through the gate function that's why first object testa T and T dot get and get is going to do what it is going to print the eval already a value becomes then how it is going to print and okay now get CH program close and here we are going to access the a value through the class name directly and here we are going to access the a valley through the number function with object name that is why static members static data members can be accessed with the class name otherwise directly object name also it is how the access is static data member through the class and a object okay thank you [Music]
Info
Channel: Naresh i Technologies
Views: 69,949
Rating: undefined out of 5
Keywords: Static Member Functions, Member Functions, Functions, C++ Tutorial, C++, cpp Training, Kishore, C++ Examples
Id: 0ToUv0Mu3Qs
Channel Id: undefined
Length: 12min 16sec (736 seconds)
Published: Tue Apr 18 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.