Constructors in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it was look guys my name is the china rose and back to my sip of blood series today we're going to continue on our whole object-oriented programming classes or that doesn't want to talk about constructors in C++ so what is a constructor a constructor is basically a special type of method which runs every time we instantiate an object so I think the best described with an example suppose that we want to create an empty class let's actually dive into some code daybook we want to create an entity class here which has two members perhaps it has a flirt X and a flirt Y which is basically just describing the position of the entity if I try and create this entity and then perhaps maybe give it a print function so that it can output what it is into the console I'll just print X and have a nice little separator comma between the two then why I'm instantiating the entity here and then I'm going to call the print function if I find on this code you can see that it will work however I get seemingly random values for the position of this entity and that is because when we instantiated this empty and allocated memory for it we didn't actually initialize that memory meaning that we got whatever was left over in that in that memory space what we probably want to do is actually initialize the memory and set it to zero or something like that so that our position is zero by default we don't specify it or set a position in the future I'm going to make an in-depth video about initialization for classes and all of that so I'm going to keep it brief here but another great example is if we decide to manually print x and y they're public so we can go ahead and try and write code which prints out X for example P dot X if we try and compile this code you can see that we get an uninitialized local variable e used error message so in other words it will not even compile because we're trying to use memory that has not been initialized this print function does compile however it doesn't work the way that we expect it to of course because it's printing x and y however they are set to seemingly random values so we've already got a need for some kind of initialization we need a way to every time we construct an entity we want to be able to set x and y to zero unless we've specified some you so what you might want to do to do that is create an init method so I'll create a method called internet it'll just be void and it will just take the job of setting ax and y to zero now what I can do is when my entity gets constructed I can call a dot in it and then if I try and print my event my values you can see that I get zero and then when I call print I get zero comma zero printing meaning that x and y I set to zero wonderful however this is quite a bit of code that we've written extra we've had to define this in this method and every time we want to create an entity in our code it means that we have to actually run that init function that is quite a lot of extra code and definitely not clean so whether it was a way for us to run this initialization code one we construct an empty in comes the constructor so the constructor is a special type of method which is basically this if the message that gets called every time you construct an object to define it we define it like any other method however it does not have a return type and its name must match the name of the class if I want to construct it for entity I just type in the name of the class which is entity I can optionally give it parameters which we'll talk about in a second or at live blog and then I can just give it a body and in this case I can set X to be 0 and Y should be 0 as well we can get rid of this igneous method and if we scroll down here we don't need to call in it anymore at all if I run my code now you can see that we get an identical result to what we had when we ran the init method however now we'll need the init method it's handled for us by the constructor if you don't specify constructor you still have a constructor it's just something called a default constructor it's actually provided for you by default however that constructor doesn't actually do anything it's basically equivalent to this it does nothing so those things that is initializes your variable at all in languages such as Java primitive types like int or floats are automatically initialized and set to 0 that is not the case in simple slots you have to manually initialize all of your primitive types otherwise they will be set to whatever was left over in that memory so very very important that you don't forget about analyzation again I'm going to talk more about initialization in a future video and strategies and ways to initialize memory correctly so definitely check that out when ever I get around to making that let's take a look at a constructor with parameters so I can write as many constructors as I want of course provided they have different parameters are the same as if I was writing methods with the same name however providing different overloads for them so basically different versions of the same method with different parameters I'm going to add X and y as a parameter here and I'm going to assign X and y to x and y so basically I'm assigning my parameter to my member variable here I now have the option of constructing entity with parameters so I simply write whatever my I want my values to be such as 10 and 5 I'll get rid of this extra print and if I run this I should have called get 10 and 5 printing to the console like so all right cool that's constructors pretty much covered constructors of course will not run if you do not instantiate an object so if you just use static methods from a class it would run we haven't talked about heap allocation yet we definitely will in a future video probably really really soon but of course when you use the new keyword and create an object instance it will also call the constructor there are ways to remove a constructor as well if for example you had a log class which you only had static methods in so for example I had my static void log here let's call it something like right instead of monkey silly and I only wanted to allow people to use my class like Thoreau I did not want people creating instances there's two different strategies for sorting that out we can either hide the constructor by making it private like so as you can see that I get an error here because I cannot access the constructor if I don't do something like that you can see it obviously allows the construction of this object because they both lost applied the default constructor for us however we can tell the compiler no I don't want that default constructor and we can do so by simply writing log which is a default constructor equals delete and if you look over here we cannot call love like this because the default constructor doesn't actually exist it's delete it there are also special types of constructor such as the copy constructor and the move constructor they're going to be set videos on each of those because they're rather complicated but to the basic use that is what a constructor is a special method which one whenever you create an instance of a class the primary use for this is to initialize that class make sure that you neutralize all of your memory and do any kind of setups that you need to do whenever you creating your object instance process we've got so much stuff to cover I'll see you guys next time goodbye [Music] [Music]
Info
Channel: The Cherno
Views: 233,356
Rating: undefined out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, classes, constructors, c++ constructors, constructor, object-oriented programming
Id: FXhALMsHwEY
Channel Id: undefined
Length: 6min 58sec (418 seconds)
Published: Fri Aug 04 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.