Const Data Member In C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends this is relational wedding simply not video series on C++ and in this video we'll be looking into this constant data members so before this video we saw studied in a member and normal data members what they are and how they behave inside your class so this is a constant data member so if you would have used somewhere like constant integer X is equal to 10 and if you're coming from C background you might be knowing that okay once you have initialized X with 10 you are done you cannot modify this X now okay so that much is pretty clear because you have created this X as constant integer so you cannot change this once you have declared I mean initialized it okay so that's what it is studying they are constant so cannot be changed once in it sliced and yeah there is a difference between initialization and assignment so this is an initialization and if you are doing something like this now then this is assignment okay so this is assignment this is initialization keep that in mind for some reference so this is the typical behavior of your constant variables so let's see how this is done in your class so for that we need to think about a class so let's suppose your professor has given you a task to create a class of circle so in circle property there is something called PI and this pi values if you know that it's good otherwise this is a constant term 3.14 and something something something but this is generally considered value for pi so if you want to get the area of your circle then it's PI R square we know that right so if you have R which is radius of your circle then you can get the area of your circle okay by using this formula so let's model this thing inside your class so let's create a class named circle and constant load pi is equal to three point one four and we'll be learning to type of initialization first is initialize itself inside the class like what I'm doing right now I'm initializing this constant inside class itself and another one is you can initialize this constant from outside the class that we'll see later so your pie is done now let's go for your radius radius let's keep it in teacher or even take this one as float no problem radius okay and now you need some constructors I hope you know what is the job of constructor and integer a and R is equal to a so this is what your constructor will do it will take the value of Circle Circle C 1 and let's make it 5 point 2 so your circles radius is 5 point 2 I mean this there is one circle C 1 and it's radius is a PI point 132 and this will go here so it should be a float okay so now you're analyzing e which is 5 point 2 inside R so you are ultimately having this R and P I inside your C one object and you have done initialization for our here and your P is a nice place automatically so no matter how many objects you create this P I will always be 3.14 for all the objects and let's get the area of your circle so area is going to be get area and what is the formula for this one R into R into pi so pi r square so this is the formula for your area and if you want to print that let's print that seam one dot that area let's compile this 100 what happened okay yeah actually I forgot to keep things and try with I mean public section generally what happens you keep your constructor and all the functions in public section so that you can call from outside your class so this is your class and you're calling them from outside okay so this you're calling not inside circle class from outside class so this should be in public section and we generally keep designed private section okay so we'll discuss all these things little later for now you just understand that we are calling this one and let's compile this so eight four point nine zero five six so this is your area if your radius is this one your area will be this one and let's create another circle see to maybe you just give this as area ten and you can get output part of that c2 dot okay idea let's compile this so this is three one four obviously ten will get multiplied two times with 3.14 then it will be three one four okay so what I'm trying to tell you is this P I you have initialized inside class and this is not gonna change for all the objects no matter how many objects you create but there is a situation I mean and now I'm talking about this one and this one is you can initialize your constant data member which is this one from outside this class and outside this class means when you are creating the object that time you can pass these values okay so for that I need a little different example now I will be taking the example of a phone class let's suppose you have a phone class and we will not go for much details here just keep the phone name and that's it so phone name is a string tied then let's create Const string P name okay and phone and this is your constructor and get phone me this is a simple function will return phone name and just return for name now and one important thing is when you are using initializer list to initialize from outside there is a different method you will use to initialize your constant data member that is called initializer list that topic is little big topic I won't cover that but I will show you how you will do that and you will understand it so as I told this is your constant data member but you will not be initializing your constant member here you will take it from here so let's write that so let's create phone one p1 and pass the name of your phone like moto G for this is your phone okay so the property is once your phone is created you cannot change the name of that right but you can create different different phones so one time you will create p1 another time you can create p2 there you can write maybe iPhone 10 okay so this is another phone and its name is iPhone 10 now once it has created you cannot change that okay so this is the logic when you are creating the object that time you are passing what is constant for you so as this will end up calling constructor and we have to create a constructor here so as that is a string value we have to take it as string STR and here comes the initializer list now notice this point here there is this column and the name and this curly bracket and inside that you just write STR yes this is the syntax for initializer list and this is how you initialize your constant data member from outside the class okay so if you want to check this let's just trim these values see out P 1 P P 2 dot gets full name and just compile this Oh the same problem I guess I have not created inside public let's create this public and rerun again what is this node candidate phone candidate expect zero ultimen what is this oh this is string okay see now it is working it should be string I just did some type of year so you have achieved setting your constant data member from outside the class okay and once you have set this you cannot change this and if you want to see that let's create another function which will change name actually it won't be able to change that but just I am giving you the hint here so we will get that name is STR void and if you want to change the name of your phone like p1 dot change name here you can write anything my phone okay so now if you will compile this you will get the error so we learned that this is not gonna work okay so let's just remove this one so we learn two ways of initializing your constant data members inside your class first was inside your class itself let me go to that example now see this is your example so first Way was 3.14 was in his lies inside your class because PI value will not change for different different circles it is a constant thing and it is constant for all the circles so making good sense to initialize this one inside the class itself but as we saw in the phone example when you are creating different different phones they will have their own name and after construction their name will not change but that name should be coming from outside the class so that we saw in that example correct and you learn what was the use of initializer list and that initializer list looks like this this one so this is your initializer list and you can use let me just do this and not only this you can just use round bracket also here that will also work so there is actually a difference between this round bracket and curly bracket but I won't discuss that here because this video is for students the thing is you have to remember these two things that's it any slicing inside class when it makes sense in his lies outside the glass when it makes sense for this one and in a slicing outside class requires initializer list okay and yeah one more thing this initializer list work only in case of constructors it won't work in normal functions here okay so you can use this kind of things inside constructor only and you might be thinking maybe some another the remember is there then what so let's create another one man sighs so you have memory slot and you will get memory slot size when you are creating this phone so maybe moto G is I don't know 32gb and this one maybe 16gb they always have less right I phone I don't know why it's like that yeah so as you change this one you have to change your constructor now so you will get it like a and now you can initialize your mem size here only with a so this is the syntax for initializing your number of data members inside constructor using initializer list just use comma here and keep on initializing so the left-hand side should be the data member what we're initializing and right-hand side of that should be what is coming from this here okay so this Model G is inside this STR and that STR we are in slicing insert this peeny correct so let us compile this we'll see no difference here moto G iPhone 10 okay so this code actually compiled and I'm writing here using this curly braces and round break it because you can use either round or curly or mixed like this and don't forget this bracket here and it's not like you cannot write anything inside this you can write let me just give you an example here let's call it construct it okay so once your object will get constructed this will print that see constructed constructed moto G for our moto I mean iPhone X so that's it it was very simple creating different different objects passing the constant value from your object itself like this and don't forget you have to use this initializer list in order to initialize your constant data member and for normal data members this is not required anyway you can do this this is good practice this is little optimized way of writing your constructor but you don't have to worry about that optimization we will discuss these things when you will get make sure about programming in C++ okay so for now let's just keep this much I hope you don't enjoy this video if you liked this don't forget to hit the like button and if you are not subscribed yet go for it it would help you a lot so I will see in the next video bye
Info
Channel: CppNuts
Views: 12,454
Rating: 4.8214288 out of 5
Keywords: Const Data Member In C++, c++ tutorial for beginners, c++ tutorial, c++, const member, programming, coding, study, practice
Id: YHr-ywZ30c0
Channel Id: undefined
Length: 15min 2sec (902 seconds)
Published: Mon Apr 23 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.