What is Inheritance in C++ | easy and epic 2021


I am going to be continuing on our adventure 3 object or any programming and told me about inheritance instead C++ sorry operators programming is a huge paradigm inheritance between classes is one of the fundamental aspect of it one of the most powerful features that we can actually leverage inheritance allows us to have a hierarchy of classes which relate to each other or otherwise it allows us to have a base class which contains common functionality and then it allows us to kind of branch off from that class and create subclasses from that from that initial parent class the primary reason why this is source says so useful is because it helps us avoid card to publication Cardiff location refers to what we have to have the exact same card multiple times doing maybe slightly different things at other times just exactly the same thing but instead of us repeating ourselves our number again we can put all about common functionality between classes into a parent class and then simply make some classes from that base class which either change the functionality in subtle ways or introduce entirely new functionality but the idea is inheritance gives us a way to put all of that common card between a number of classes into a base cost. inheritance inheritance

So that we don’t have to keep repeating ourselves kind of like a template so let’s take a look at how we can actually define that now sells good surprise that I had an ANSI class and this was going to go in pretty much every actual entity that I had in my game we have a lot of very specific and cities you know game however to some aspect they will share functionality for example perhaps every entity has a position inside out game this might be expressed through 2 flights for example still have led accent light why next we might want to give every answer be ability to murder theft fire remove method so we’ll take in an axe a and a why IBM the amounts that we want to move by okay great so we’ve got a base and see because we basically said that every single and see we create in our game will have these traits let’s go ahead and create a new type of NC for example clapper right out to play a class for now with no concept of inherent.  inheritance

And so basically if we were doing this from scratch and we want to display to also have a position because it is an ANSI in it makes sense for it to have a position inside our game we would also want to get it to be able to move so we would need to move function carry across we would basically end up writing something that looks fairly similar to this maybe this plan plus has extra dollar that we wanted to store for example a name or something like that we would add this you can say that these are actually different classes however there’s quite a lot of cars has just been copied and pasted and that’s this entire section so what we can do here is use the power of inheritance we could extend this and C. class to create any type called play and then also have a storm you Donna such as a name as well as provide extra functionality such as a function that doesn’t exist in the base class and every garrison Alice get planned to be a sub class of and see the way that we do that is we write a colon off to be type declaration here plus plan and then we write public entity okay so now with this line of code that we’ve written here are a few things about Zella happens the player class now not only has the type of player it also has the type and see maybe that it actually is both of those types and see ++ quite a complicated topic because on one hand. inheritance inheritance

They don’t really exist however on the other hand they kind of do especially if you have certain runtime flags activated when it really got to get in depth about how this whole thing works just yet but definitely something for the future the other thing that the cat is that play out now has everything that NC house so all of these class members that we have such as X. and Y. hit 2 flights they now also including here so right now we’ve actually got full flights totaling the play classical piece to that we have here that are just the carpet impatient ones as well as the original Terry said let’s definitely get rid of all of that duplicate cardio so that we only have everything that’s near such as the name is a construct point and the printer name function which is kind of our additional functionality example okay cool so the plan calls looking really clean now however but he’s actually in and see which means that just looking at this class doesn’t actually tells the whole story we. Have to go out and find the ANSI class and see what it has because as far as the player’s consent anything that is not private inside the ANSI class is actually acceptable by play let’s go ahead and test this out supposedly create A. player class instance him I can now not only cold the prince name function which you can see existing player but I can also call move at access X. and Y. inheritance inheritance

Just like this just as if it was an entity I can provide it with certain parameters for where I want to move I can access X. and Y. as if it was as if it wasn’t NC because is inherited all of that and Steve functionality kind of a concept that we can use this wave is something called polymorphism which will actually dive into any future Article but basically polymorphism is the idea of having multiple types full for single types are member how I mentioned that plan is not only the type player but also as an entity it means that we can actually use a play out wherever we wanted to use an SD and that makes sense because the plan has everything the NC house plus a little bit more and it doesn’t even have to have more that’s something that we’ve added he could be exactly the same but. I is always going to be a superset of Anstey meaning that player is always going to have everything that and she has and possibly more meaning that if I want to create a standalone function with printed in NC object for example perhaps by accessing the X. and Y. inheritance inheritance

Variables and just bring him in to the console I could actually policy in a playoff object into that same function even though accepts an entity as a parameter and the reason that’s possible is because the plant type of play classes guaranteed to have those X. and Y. variables because it has to be it’s a sub class of S. C. meaning it’s got all of that and see stuff in it there are so many things that we can do he’ll sauces all of those polymorphic phages we can also change the behavior of our superclass or app based class for example by overriding a method and giving a new card to run instead but the take away from here today is that inheritance is something that is used all the time it’s just a way for us to extend an existing class and provide new functionality to a base class it’s one of the most important things in overtime to programming and remember when you criticize Klaus it will contain everything that you’ll said the class contains another great way to prove this is that remember we have 2 flights yeah acts in one side and see let’s go ahead and print the size of an NC object to our consuls August 2 size all fantasy I had a 5 hitter on my card you can see that my size is 8 which is what you would expect because we have 2 flights in the NC class here X. and Y. inheritance inheritance

Now let’s go ahead and bring the size of the player now if played did not extend and to see if play was just across all of it sorry it would only have this cost char pointer which should be 4 bytes of memory on a 32 bit application however since we are extending it to you means that it inherits all of the variables that you decide that and C. class so it should actually be 4 plus 4 plus 4 which is 12 let’s go ahead and hit F. 5 and see if that’s right and you can see it is so you can see that we’ve literally inherited everything and see how it’s almost as if we copy and paste everything that and he has into the plate glass because the plane classes now an entity and that additional functionality that may. We’ve attitude in mind of the size of these classes in memory can actually vary if we started our overriding functions and play then we’d actually have to maintain something called off the table which is extremely that we have to all get by not going to get into that right now but that is the gist of how many Africans works if they both lost over the course of the series will be breaking down old individual concepts to do with all charge programming polymorphism inheritance all that stuff individually in in depth to definitely stick around for that because we don’t even scraping the surface. inheritance inheritance

Also See

What is Argument Evaluation Order in C++ | easy and epic 2021

C++ framework

Leave a Comment

Your email address will not be published. Required fields are marked *