C++ OOP (2020) - What are constructors and class methods? How to use them?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to my channel my name is saldina and i make it and programming related videos on this channel so if that is something that is of interest to you consider subscribing and give this video a thumbs up as well and in this particular video i want to talk about constructors and class methods so here i have some code that we have written in a previous tutorial of this course so if you haven't watched that make sure to check it out i am going to link it here and in the description as well and let's explain very quickly this code that we have here so i have created a class called youtube channel and that class has four public properties so for attributes those are name owner name subscribers count and then a list of published video titles and then here i have created an object of that class i have assigned the value today to these properties here okay and then here i have written out information about this object that we created and as i said in this video i want to talk about constructors and class methods and how am i going to explain these well let me first introduce a problem to this approach here so that you can really understand the need for these two so what happens if i want to create another object of this youtube channel class how am i going to do that well in this current situation what i would do is i would copy this so i'm going to copy it and then i'm going to paste it here like this and let's give it a name let's say that this channel is going to be youtube channel 2 and let's call it let's say youtube channel 2 dot name let's say that it is going to be called amy sings for example and owner is going to be a girl called amy and then she is going to have let's say 2 000 subscribers okay and then the videos that she has published are going to be songs obviously because she has a channel named amy sings so let's say that she has published for example um let's see johnny b cover like this so that's one song that's one video and then let's say that another video that she has published is going to be for example lorelei so that's a song from scorpions and she has made a cover of that song like this now these two are two of my favorite songs and you can put whatever you want here in this list and i'm also wondering and you can write that in the comments down below what are your favorite songs so please write that in the comments just don't put links because i think that youtube has this policy of treating um links as advertising or spam or something so just write out the name of your song and then uh the name of a singer i guess and i'm really looking forward to reading your comments so now that i have created this second youtube channel what i want to do is i want to write out information about this channel but i noticed that i'm missing here uh this youtube channel too so here i'm going to say youtube channel 2 dot owner name and then youtube channel two dot subscribers count and then youtube channel two dot published video titles as well okay now if i wanted to write out information about this second channel what i would do is i would copy this so i'm going to copy it and then paste it here and here i will say that the name is youtube channel 2 dot name and that owner name is youtube channel 2 dot owner name and then here as well and then youtube channel two dot published video titles okay now if i run this program we should get information about these two channels so the first channel is this one that we already had it's code beauty owner is me and then this is subscribers counter and then these are three videos that i published so far i mean i published more than three videos uh but this is just an example and then here is our second channel it's called amy sings and then owner is amy she has 2 000 subscribers and then she has published two covers now this here has a problem if we wanted to create a third a fourth fifth object we would have to copy this code and then each time that we want to write out information about those channels we would have to copy this code and this really collides with a principle called don't repeat yourself meaning do not repeat your code if you don't really have to repeat it and in this particular situation we do not have to repeat it so how we are going to solve this problem well the first thing that i want to talk about are going to be constructors and what a constructor is it is a special method that is going to be invoked each time that you create an object of a specific class meaning that method is going to be called on the construction of that object so there are two rules when it comes to constructors and the first rule is that constructor has the same name as your class and then the second rule is that constructor does not have a return type so let's create a constructor for this youtube channel class here i'm going to say youtube channel like this and then i'm going to put these parentheses and i'm going to put these curly brackets as well now what i want to do in this constructor is i want to receive two parameters i want to receive two arguments and those are going to be string name and then string um let's let's say owner name okay now these are two parameters that i want to pass to my constructor and what i'm going to do with these in my constructor is i'm going to assign these to these properties here so i'm going to say that name is going to be equal to whatever my user has passed to this constructor so name is equal to this name here and then owner name in my class is going to be equal to this owner name that i have received in my constructor now since since this constructor here is invoked each time that i create object each time that it is constructed that means that at the beginning when a channel is created it has zero subscribers so i'm going to put here zero like this we do not even have to pass this value in our constructor because when you create your youtube channel you do not have subscribers okay now what am i going to do here is i do not need to write this code like this anymore so i'm going to delete i'm going to actually comment this code here and as you can see this is underlined it says that no default constructor exists for class youtube channel now what we have to do is we have to use this constructor here that i have just created and how you do that well you put parentheses here and then inside these parentheses we are going to pass these two parameters so the first parameter is going to be the name so that is the name of our channel and that is this name here so code beauty like this and then a second parameter as you can see here is owner name and that was this value here so i'm going to pass that here like this and now after i have created this youtube channel we do not need this code um anymore so i'm going to delete it okay and what we can do is we can do the same with this second channel so with this youtube channel too so i'm going to really comment this code for a moment and then here i want to invoke a constructor for this youtube channel class and pass it as first value the name because we are receiving name as our first value in our constructor like this and then the second value that we need to pass is going to be owner name so i'm going to copy that as well and now we have successfully created this second youtube channel okay so now i can delete this code here as well okay and now if i run my program as you can see it has written out information about my two channels the first one is code beauty owner is me and then i have zero subscribers because i have just created my channel and we have assigned that here and then our second channel is amy sings owner is amy and she has zero subscribers as well and as you can see both of these videos published videos lists are empty because we haven't uh really added any videos okay so how do you do that i'm going to delete this from here and then move it here like this so now we have both of our objects being created at the beginning here so what i want to do now is i want to add a couple of videos to this channel here so how do you do that well you say youtube channel like this and then i'm going to put dot and here in this published video titles list i'm going to invoke a method push back which is going to add at the end of this list another element and that element is going to be of type string because this published video titles is a list of strings so here i'm going to add a string and i'm going to call that video c plus plus for beginners like this so that is one video and then let's add a couple more videos let's say uh html and css for beginners and then let's say um oop for beginners well no let's say c plus plus oop like this okay so we have added three videos for this first channel and then the second channel does not have videos yet so if i run my program again you can see that this first channel has three videos and then this check the second channel does not have any videos yet because we have deleted those that we previously had okay now that was the example on how you can create a constructor and how you can use a constructor so this here is the constructor of this youtube channel class and we have passed two parameters two arguments to this constructor and then here in our constructor we have really assigned initial values to these properties here that we have in our youtube channel and then this here is the way that you use your constructor so you put these parentheses and then you pass the values that your constructor has to receive and here we have created two objects and we have done that without repeating code now what is going to happen with this here so this here and then this here is repeating code as well and in order to solve this particular problem we are going to use class methods so class method is going to describe a behavior of a class and since here we are writing out these attributes of our class since we are writing out information about our youtube channels what i want to do is i want to create a method that is going to be called get info for example so that method is going to be of type void let's call it get info like this and what i want to do in this particular method is i want to copy this code here so i'm going to cut it and then paste it here and in this particular method in this get info i want to say please write out name and then the name property we don't need this youtube channel object anymore because we are in the class itself okay and then here as well i'm going to delete this here and then here and now this code should work so whenever you invoke this get info method on your object it should write out all of these properties of that object so now i can remove this code here as well so this um information about our second channel and how am i going to get how am i going to invoke this method well i say the name of my object so for example youtube channel dot and then as you can see i have this get info method available here because it is public so i say get info and i invoke it like i would invoke any other function and let's do the same for our second youtube channel let's say youtube channel to get info like this and now if i run my program as you can see it has written out information about my two channels so code beauty saldena's owner and then subscribers count my videos and then info about my second channel as well so i hope that you understood the concept behind a constructor of a class and then behind a class method here we have demonstrated how you can create an object using a constructor and then here as well so you don't really have to repeat your code each time that you create an object and then here we have invoked a method of these objects which we have implemented here so you do not have to repeat that code as well each time that you need to get information about your object you just invoke this get info method and now our main function looks much cleaner and all the implementations of the construction of our objects and then this get info method are in this youtube channel class so i hope that you enjoyed this video if you did give it a thumbs up and also subscribe to my channel don't forget to hit that bell icon and i'm going to see you in my next video bye
Info
Channel: CodeBeauty
Views: 41,847
Rating: undefined out of 5
Keywords: codebeauty, code beauty, c++ programming tutorial, c++ for beginners, visual studio, visual studio 2019, c++, c++ para principiantes, aprender a programar en c++, curso de c++, object oriented programming, oop, classes, objects, introduction to oop, c++ oop, object oriented programming c++, object oriented programming explained, course, constructors, class method, constructor, class methods, what is constructor, what is class method, constructor with parameters, tutorial
Id: 1LGJSRFrxqQ
Channel Id: undefined
Length: 15min 5sec (905 seconds)
Published: Sun Aug 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.