Constructors & Destructors in C++ Programming | Object Oriented Programming Concepts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] Yoga is thin my offer simple snippets back with another video tutorial for a playlist object-oriented programming in C++ so in the previous video tutorial we started off with the object-oriented programming paradigm of C++ programming and we covered a little bit of theoretical aspect of what object-oriented programming is and we also saw basic program so if you have missed that video I'll put a card and you can see the card in the top right corner and go ahead and check that out because if you don't know what object-oriented programming is then that video is just for you so in today's video tutorial we are going to be discussing constructors and destructors you know object-oriented programming so we first go ahead with the theoretical aspect as in what constructors and destructors are and then later on see a program so both theory as well as practical part would be covering a little bit a very clear idea and before we start off if you haven't subscribed to this channel yet make sure you subscribe to the channel because there are more upcoming videos on object Android programming as well as many other topics like mathematics aptitude other programming subjects and so on okay so now that do you subscribe to this channel let's start with today's tutorial okay so let's start off with constructors in C++ and let's go ahead with the theoretical aspect of what exactly a constructor is and I have written some points out so let me just you know the first question that is what is a constructor so the answer is also written there so a constructor is a member function of a class so we already know what member functions are essentially the functions inside a class unknown as member functions and also you know what exactly a function is because we've covered that topic as well in this playlist so a constructor is a member function of a class which initializes objects of a class okay so this this constructor is a special function okay and what it does is it initializes the object of a class so when an object is created in the main function in the void main function at that time the member the data members of that class are initialized and that job is done by this constructor function so in C++ constructor is automatically called when object that is instance of a class is created so it is a special member function of the class so what exactly makes it special so that's point number two how our constructor is different from a normal member function so constructor is different from normal function in the following ways so constructor same name as that of a class so whatever the name of the class the same name has to be given to the constructor and we know all functions have a name right so even constructor is a function so it will have a name but in this case it has to be compulsorily the same name of the class and if the class is having a all-caps letter then the constructor name will also have that all-caps letter in the name the next point is constructors do not have return type so normal functions usually have certain return type even if it is not going to return anything it is void or some integer value some some data def has to be returned but in constructors case there is no return type the Third Point is a constructor is automatically called when an object is created so even though when we do not create a constructor explicitly the program when it is compiled or when it runs and when the object is created at that time the program itself creates a constructor which is usually known as default constructor and we'll go through the types of constructors as well so the last point C is the same that if we do not specify a constructor C++ compiler generates a default constructor for us and what it has is it expects no parameters and has an empty body so that is our default constructor is okay so now this is just a theoretical aspect I know you are not still clear because we haven't seen the program and you'll get a very clear idea and we actually go ahead and go to the practical part and actually code and write that constructor itself so let's move on so there are three different types of constructors in C++ the first one is default constructor and default constructor is a constructor which does not take any arguments so we know in functions we can pass arguments or passing parameters in the opening and closing parentheses but in default constructor there is no parameter then second one we have parameterised constructor in this case we have to pass arguments in the constructor so typically these arguments help initialize an object when it is created so say for example we have two data members in a class and then we want to initialize those two data members so a parameterised constructor can do that job by taking those two values as arguments and you will understand more when we go ahead and create those constructors then the third one is copy constructor now this is a special constructor and a copy conchita is a member function which initializes an object using another object of the same class so there is a special use case of this copy constructor and there is a concept as well which is known as deep copy and shallow copy which comes along with this and we will discuss that in the next or further video tutorials because it's a totally different topic which will take some more time understand and have kept that as a separate tutorial itself for simplicity purposes understand that a copy constructor is a member function which initialize an object using another object I will show you is case but there are more use cases to that and it is not usually used because copy constructor again is by default called and it is only essential when we have dynamic memory allocation into picture and you know what dynamic memory allocation is and if you don't then you can check that topic in this playlist itself and I will drop a card as well which you can see in the top right corner okay so moving ahead let's go to what destructors are in C++ so destructors as the name suggests is a member function again which districts or deletes an object so it is exactly opposite to what a constructor is a constructor is used to initialize and construct an object and destructor is the opposite which districts or deletes an object so one is a destructor call so a destructor is called when the function ends or the program ends or a block containing local variables end or when it delete operator is called so now again destructor is also a member function but it is again a special member function and the way it is special is because you can see the two points that have given there destructors have same name as the class but it is preceded by till sign so that the district term constructor can be distinctly known so disturb does have an extra till sign and you will see that in the program as well and last new destructor do not take any arguments and do not return anything not even worried so in this case they do not take any arguments also so there is no different types of destructors there is only one type of destructor in this case so can there be more than one destructor in a class so as I mentioned there can be no different type of destructor so no there can be only one disturbed on a class with a class name preceded by till and no parameters in no return type so when do we exactly need to write a user-defined destructor so usually we do not need to write destructor explicitly because the compiler creates a default destructor for us and that default destructor is pretty decent enough to clear the memory but if we have dynamically allocated memory or any pointer in class then that time we have to create a destructor to be allocate that memory so that's what the last point is stating so when a class contains a pointer to memory allocated in a class we should write a destructor to release that memory before the class instance is destroyed this must be done to avoid memory leaks which essentially means that that memory is allocated but it does not properly be allocated so when we actually need a destructor and in usual cases who do not need a destructor so this was all about the theoretical aspect of constructors and destructors in C++ and now that you have a brief idea and what exactly they are let's actually go ahead and see them in action and let's go to the practical part where we wherein we will create some constructors and destructors in a program ok so what you can do is quickly go ahead and open your day with C++ ID and in the previous video tutorial we saw program wherein we created a class named cars and we also created certain objects of that class and you can see over here in the main function so what you can do is you can go ahead and open that program and if you haven't typed it already you can quickly pause this video and type it out or what I'll do is I'll share a file in the description you can simply download it but I would recommend that you watch the previous video and go ahead and code this program so that you get a good practice and that is the best way to actually learn programming so in the previous video tutorial what we did is we created a class named cars we had certain data members you can see three strings one float and one double we created public number functions these are the two member functions that is word set data and display data and this was the main function wherein we created one object we set some data and we displayed the data so since we already have this program we'll just continue with the constructor and disturb the part in this case so let's go ahead and create a constructor and the constructor and destructor has to be in the public part of the class since it is a function just that it is a special function but it has to be over here so what I'll do is I will create a default constructor first so as I mentioned it does not have any return type so you don't have to type in void or something like that so just the name and the name has to be same as that of the class so I will just copy this physic cars and since it is a function attached to have opening and closing round brackets it has to have a body but what I will do in this case is I will just don't see out and I will print default constructor called so what this does is when the object is created in the main function this default constructor will be called and we know that default constructor is always called even if we don't create this but now we have created this so the compiler will call this version of that default constructor and it has to be called explicitly every time when the object is created so but this time we will get message that default constructor is called so let's go ahead and comment this out first and let's see if this works I will see you this you go to execute compile and run and you can see the message default constructor called over here which means that our program worked fine and this constructor was called now if I create two objects the default constructor should be called two times so let me just see you this again go to execute compile and run and there you go you can see the test printed two times default constructor call which means that two objects have been created so this was a default constructor but what exactly the default constructor is used for is to initialize these values so what happens is when the object is created say for example car one is created in main function what what is happening is some value is allocated to the members or the data members so some default value is by default given to these data members by the default constructor which is auto generated by the compiler now this default value is not ideally right or is not usually needed by us say for example let me just show you an example and what we do is we comment this set data part and will directly go ahead and print the data that is there in this object so what we did is in the main function we created car one object and before even setting the data so we have not set any data we will directly display data so we will see what by default the constructor has allocated so I'll see you this and I'll say compile and run so as you can see we have car properties company name - and there is no data which means the first string that is company name this one is kept blank again the company model is blanked and type is blank which means all the string type data members have blank values allocated by the default constructor but now here you can see car mileage you can see a vir number and in the price also you can see a weird number now this is by default allocated by the default constructor which is this one since we have not specified any values it is giving any random value to the float and double type of data members and for the string type it is giving blank values so this can be changed in the default constructor and that is the actual use of default constructor the what I'll do is I'll just copy this part and paste it over here and what I'll do is I will give some hard-coded values so we know the company name is Toyota in this case we'll give model name Altus and the fueled by gas petrol the mileage will give as 15.5 and price 15 lakhs so if I see you this and if I see compiled and run in this time you can see the correct values are given but again there is a issue over your if I print display data for car to again the same constructor is going to be called and the same values are going to be assigned and these get these values are hard-coded so every time the same value is going to be displayed so we don't want that so in that case what we can do is we can create a parameterised constructor and pass those values over here and I'll show you how that goes so what I do is I will just keep the company name constant or hard-coded which means that every object that I create will have company name s Toyota but the model name the price the fuel type will change so now what I'm going to do is I'm going to create a parameterized constructor so eight cars since it is a constructor it has to have the same name opening and closing curly braces and this here we will pass the values so I'll say string m name then as a string F type and just copy and paste this part over here okay so what I did is I included four parameters leaving the company name I have included all the parameter so what I'll do is I'll just copy and paste this over and exclude the company name so our default constructor is ready over here you can see and just comment it out then we have a parameterised constructor which is taking four arguments that is leaving the company name all the other arguments and they are assigning to the data members so now you can see I have created car one and in this case the default constructor is called but for Karl to what I'm going to do is I'm going to open and close round brackets over here and here I'll pass those values and those values are the model name so in this case I will say Fortuner which is the model name then the fuel type is going to be diesel mileage is going to be 10 kilometer per liter and the price is going to be 35 lakhs so what exactly happened is while creating the the first one did not have any opening and closing round brackets which means that the default constructor is called and this is how you call the parametrized constructor you create the object and along with that you open and close the round brackets and pass the equal number of arguments according to the prototype which you have created in the parametrized constructor so you have two strings so that's why I passed two strings then this is going to be taken as float value and this is going to be taken as double value okay one mistake that I'm doing is this does not have to be in double quotes since it is a float value and this is a double value only the string values are supposed to be in double quotes so now let's go ahead and try to run this and let's see what the value is for car one and what the value is for Carl - I'll just add one more ndele over here so that we can have a clear distinguishing between the two so let me just save this go execute compile and run okay I have made a typing error you can see that I have added string as caps so I just have to move small caps over here see you this would compile and run so there you go with the output and you can see for the car 1 we have company name as Toyota the company model since I have not set anything the default constructor is called but in default constructor I'm not setting anything so that is why it is blank blank then the car mileage is zero and price is set to some random number and the car mileage is zero because every time the constructor is called it will set any random number so it can be any number over here now for the second car you can see that we did not pass the company name so leaving that everything is correct over here it is coming from the parametrized constructor so what we can do is we can even pass the company name over here so I'll say Toyota and just add one more argument over here in the parameterised constructor so just go ahead and pasted over here give a comma copy this and paste it over here so what I'll do is s set the data for Carwin using the set data function but for Carl too I'm setting the data using the parameter s constructor so there are two ways to set data and usually the constructor is used to set data so let's again execute this so there you go for the first case we have total despair toll fifteen point five and fifteen thousand this was said by this function set data and for the car too we have car company name Toyota Fortuner diesel mileage is ten kilometer per liter and car price is 35 lakhs which is set by this parametrized constructor so now these two are very clear to you so let's go ahead and create one copy constructor so again same name as the class name so it's cars opening and closing curly braces for the function body now inside the copy constructor what we have to do is we have to pass an object of the same class but the way we pass this is cars and obj now this and means the address is passed so what is happening is let me just copy and paste it over here first so this object is of the same data type as cars and the object that we are going to create using this object is also going to be the same so essentially what we are doing is we are creating an object of cars using another object of the same data type that is cars so this company name is going to be for the object that is being created and this obj dot company name is going to be the object that is going to be used or the object whose values are going to be used values by by values I mean the data members are going to be used to create the new object again we have obj dot model name then we have obj dot fuel type then we have obj dot mileage and lastly we have obj dot price so this is the syntax when we are going to create a copy constructor you have to pass the object of the same data type so that is very cars but in this case you have to pass with the end which is the address operator so what I will do is I will just print individual messages copy constructor so this will paste default constructor called this will paste parameterised constructor call and this will paste copy constructor world so let's just save this now in the main function we have the first object cars one so here since there is no opening and closing round brackets you know that the default constructor is going to be called so the first message is going to be printed which is over your default constructor called and there is nothing else inside the body so only this message is going to be called for the second object you know the parametrized constructor is called getting gold because you can see that we have passed five different values so here's the parametrized constructor so the message is also going to be printed then what we are doing is we are setting the data of carbon using the set data function then we have printing carbon data and we are ending call to date our what I will do is I will create one more object of same data-type cars car three but in this case what I'm going to do is let me just create it over here at the end but what I am going to do is I'm going to do create cars three using the object car one now this means that we are going to copy data from car one to car three so this is the place where copy constructor is called now the car one is going to be passed as an object in this place and car three data members are going to be on the LHS side so the company name model name over your this this this this and this is for car three and this object dot company name objected model name is for carbon so that is how the assignment happens from right to left so okay let's go ahead and compile this and let's see how this goes so there you go the first default constructor was called you can see on the line one for car one then you can see parametrized constructor was called which means at this place then we set the data so here we haven't printed any message but the data for car one is set at this line then we displayed the data for car one you can see the car properties over here then we have displaying the data for car to which is over here and here the copy constructor is called you can see the message copy constructor called however we did not print the data for car three so what we will do is let's go ahead and print that it does call three dot display data so if car three data members are equal to car one data members we can see that the copy constructor was successfully called so let's see if this go to execute compile and run so this is car one and this is car 3 let's compare the data you can see carbon company name Toyota here which a Toyota Altis Altis patrol fifteen point five and fifteen lakhs which is same as the car three so this means that to the copy constructor functionality was successfully achieved and you can also see that the message is printed copy constructor call ok so these were the three different variations of constructors now let's go ahead and create one destructor since we only have one type of destructor and in this case we are not having any dynamic memory or any pointers so we actually do not need to implement any destructor but just for the sake of understanding Louise will create a destructor so the way we create a destructor is we have to start off with the till sign and then again the same class name so cars opening and closing round brackets since it is a function and an empty body but inside the empty body what I will do is I will just print destructor called so this destructor is going to be called when the object is going to go out of scope or the program is going to exit exit so let's try to run this and let's see if this destructor called method is called and we can see that message okay so as you can see the program ended over your that is the reason why we are getting destructive or destructive or destructive us we got it three times and you might be wondering why we did we get the message three times so let me just first add escape sequence and let's try to compile and run it again okay so the program got over at this point over your car's leader display data so the third car displayed the data and the program was completed so after that the three objects went out of school and the destructor was called for all the three objects so that is the reason why I printed destructor called for three times so this is all the destructor works and we just printed out a simple message but if you have a pointer or dynamic memory allocation the the allocation should happen in the destructor so this is a destructor so there you go we have the destructor over here then we have the three power constructors so this is the copy constructor the parametrized constructor and default constructor so this was the practical aspect of constructors and destructors and you have seen a practical implementation and you also know what constructors and destructors do as well as we've gone through the theoretical aspect so that's it for this video guys and I hope you understood the concept of constructors and destructors in C++ object-oriented programming and what is the functionality of constructors why do we need them what is the functionality of destructor why do we need them the types of constructors and the practical implementation program so if you have any comments or queries you can always put them in the comment section and if you liked this video give it a thumbs up share it with your friends and make sure you subscribe to this channel so that you get notified when I upload the further tutorials on object-oriented programming and other topics as well thanks for watching I will see you guys in the next video tutorial peace
Info
Channel: Simple Snippets
Views: 200,025
Rating: undefined out of 5
Keywords: Constructors & Destructors in C++ Programming by simple snippets, constructor and destructor in c++ example program, constructors and destructors in c++ with programming examples, object oriented programming c++ construtor and destructor, constructors and destructors in c++, constructor and destructor in c++, c pogramming constructor and distructor pogram, what is constructor and destructor in c++, constructor and destructor in c, constructors and destructors in c++ tutorial
Id: hAA8FBq2bA4
Channel Id: undefined
Length: 23min 12sec (1392 seconds)
Published: Sat Dec 02 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.