If you don't understand OOPS concepts, WATCH THIS!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the next few minutes you will be learning more than six C++ principles by solving just one question this question is by far the most beautiful question I have seen and it's very short given a Class A we want to know at any time how many objects of a live in the heap section of the memory and the concepts that it will teach us is stated data members in classes how to define them what are constructors and destructors what is dynamic memory and what is the difference between memory allocation in stock Xhosas heap what are the difference between new delete and malach free operators then I don't want to highlight the seventh point over here because I will be diving into the code after this and I want to explain each and everything in one line and I also don't want you to get lost or start thinking out the solution and then at the end I will if you stay for a while I will give in I will be giving a bonus question which I will be covering in my next video before I start I just want to give a brief introduction about myself I am wretched Jan a software engineer at Microsoft and I am constantly uploading videos on coding interview questions and lectures and coding interview tips for helping bigness learn quickly and get an exposure to what exactly happens in a coding interview and how to approach those problems if you want to learn specific topics I have a playlist for that as well like dynamic programming from zero to hero and if you want to learn specifically about trees and graphs I have dynamic programming on trees multiple lectures for that and questions based on graphs as well I'll be uploading all the playlists but it will take me a bit time and I want you to stay structured and use these playlists to to achieve as much as you can from these videos so starting out with the present problem so this is the class a and in the public section I have created a static variable and in the constructor I am incrementing that now since static access a static variable static data member of this class a for as many objects that we create the copy of X will be only one and it will be shared among those and since you are incrementing that again and again for in the constructor so hop for how many objects we create of a the constructor will give us how many instances of this class a we have in memory but this will give us the number of objects in all in both memory in stack as well as heap but the question is talking only about heap so let's solve an easier version first and then we can make things complex so I have created two objects in first line and three in here so this should print five but as you can see this is throwing an error about undefined reference to X and this can be solved by declaring X or defining X over here now C++ knows that X is a static variable in a and it's defined now here with the value 0 so we can now use this code to print 5 all right awesome now let's see if this code also so these are local variables right local variables and they are stored in the stack section of the membrane but let's create a dynamic object now which is created at runtime so we need a pointer to hold that and we call the malloc function malach accepts the size of a as input and then we can cast that pointer to a star and we have a dynamic object so now we have 6 objects in memory for a let's see what does the code print oh so we can see that the code prints 5 but we created 6 objects so it means it's not counting the dynamic object till now but why is that this is because of this line over here mel or just allocates memory but it never calls the create constructor it doesn't call constructor to solve that issue let's try allocating the memory using the new operator which is present in C++ so this is the way you this is a C style way of allocating memory but in C++ to handle this issue we can do something like this allocates memory and calls the constructor as well now if you run the code we can see that the count is now 6 which is which is good now we are counting all the instances of a now let's now since this is constructed in the runtime or it's a dynamic object we can also delete it so let's free this memory by calling the free function now we have after this line five instances of a so the code should print 500 again it's not what we want and it 6 how what is the problem here again this is C style way and it only destroys memory or D allocates memory it does not call the destructor doesn't call the destructor ok fine we can solve this by using the C cluster style way of deleting things which is delete pointer and we can comment this out and run our code again oops we are again getting 6 but this is not because the destructor is not getting called it's because we are not decrementing the court the counter will value over here so when the object is destructed would decrement the value of x and now if we run the code we are getting 5 things look good now and in fact just to make sure that free was not working I will comment out delete and run this code now and this is still 6 so as I told this C style way of malloc and free they do not call constructors and destructors and I highly recommend you to use new and delete all right so this is fine now we are doing great we are counting all variable all instances whether they are in stock or they are in heap and I didn't mention it here so dynamic variables are created in heap and the question now focus is to count only those which are in heap how do we do that now so basically we have to so from the total count we have to decrement the number of variables which are in stack or that's one approach or the other approaches we just need to increment counters when we are creating on heap how do we do that because the constructor has no way of knowing whether it's getting called from new or it's a local variable getting created to solve that issue the next answer is operator overloading what we are going to do is very easy we are just simply going to overload the new operator and what it how new operator works is it returns a pointer to do the operator overloading we write the syntax so operator followed by the keyword that we were operator that we want to override in this case new and new accepts parameters like size underscore P let's say size okay so now whenever you call or new on an object of class a or whenever you call new on a to create an object this function will get called and what we can do is we can increment X only over here so now we have overloaded and now we know basically now we can remove these that we have here and now the encounter will get increment only when a variable is getting created in heap let's call let's try running this once I'll not delete it over here so I've created only one object in now in the heap so the answer should be one because all the five variables up above are created in stack and the answer is one that's good let's try it out for one more pointer let's say a pointer to and run the code again and it's working well now we just have to make sure that if we delete something it should get one so we have to do the overloading for delete as well and I will leave that as an exercise for you I hope you are clear with how these things are functioning and the only thing you have to do is override delete and then decrement the counter over there instead of incrementing and decrementing in the constructors because the Constructors have no way of dealing whether this object is in the stack or heap so that's why it's better to use operator overloading I hope you liked this video and you learnt a lot if you want more such videos shoot out in the comments about what problem you want me to make a video next and hit the subscribe button if you want to see more such videos and get notifications for them I'll see you in the next time but as I'm entered there will be some bonus question and I will also talk about that and I will be covering the solution for that in the next video so let's say the question now changes and now we have a Class A and for some reason we do not want any object of that class to be created into heap how are you going to solve that problem so basically if someone do does something like this it should fail the program should fail it should throw a warning or something like that and it should stop the user from doing that how can we use the concept that we just learned to do this let me know in the comment section if you were able to solve this problem and as well as empower IDE the delete function and and I will see you next time bye bye you
Info
Channel: Rachit Jain
Views: 94,514
Rating: undefined out of 5
Keywords: dynamic allocation, static variables, static class, inheritance, operator overloading, new malloc, free delete, new delete, coding interview, oops concepts, oops interview, oops questions, coding questions, coding interview questions, competitive programming, coding, google interview, microsoft interview, google software engineer, dynamic programming, graph theory, algorithms, c++ static, c++ new vs malloc, c++ inheritance, c++ oops
Id: qsMahidcehY
Channel Id: undefined
Length: 10min 53sec (653 seconds)
Published: Tue Sep 04 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.