Transcript for:
Advanced C++ Concepts

hello and welcome to part two of this series on C++ this is a continuation of part one which we previously did and I would like to thank you so much for all the likes and all the support you guys have done now in part 2 we're going to talk about more advanced C++ stuff like pointers to object-oriented programming to talk about structures and classes and you know method overloading overriding virtual methods and all these kind of good stuff now this lecture goes really into the details of object-oriented programming and stuff like pointers and addresses so we're going to like really look at the basic details of what every programmer should know about pointers and object-oriented programming so without further ado let's get started ok let's talk a bit about pointers let's go first to the whiteboard and explain what pointers are so let's say you declared an int a is two examples I'm going to divide this for it for the coding and here for in memory code and memory so in a naive sense what happens is that the compiler reserves certain amount of memory for a so this is a box or a memory address stored for a and the value 2 is assigned to a at this line of code let's say you change the value of a to 3 at a certain point of your code then what happens is that a is now 3 right now a natural question here is how much memory is allocated for a well since a is declared as an int then we have 4 bytes which is 32 bits right because each byte is equal to 8 bits and this memory size is static it does not change because it's an int and an INT has a fixed amount of size of memory allocated to it floats have different sizes double has another size long ends as well if you're interested to know how much memory is allocated to each of those data types I found this link right here really useful even though it says C programming but the size of data types is the same so if you go down here you can see that one byte or eight bits it's is allocated for HR and since there's eight bits then the range of values are from 0 to 2 power 8 minus 1 which is 2 power a 256 minus 1 it's 255 as you can see here ants have 4 bytes older computers it used to have 2 bytes you can see also that floats are 4 bytes doubles are 8 bytes and so on now why am I saying this how does this relate to pointers well a pointer if you want to define a pointer and star B B is a pointer and the way you should think about a pointer is that it really points towards an address so if I wanted to point towards the address of a if I wanted to point towards this box right here all I have to do is to say it's equal to the address of a so this is the end and what happens behind the scenes is that bead will come and point at the egg as so so B is also another box which contains the address of a so let's say the address of a is something like 0 X a B 0 1 so the address is written and hexadecimal notation the numbers that you use in your daily life which are 1 2 3 and 11 1010 and so on are written in decimal notation there's other types of notation like writing numbers in a binary basis so one would be 0 1 or simply 1 2 would be 1 0 3 would be 1 1 4 would be 1 0 0 and so on and how we represent numbers and their binary basis is let's say 1 0 0 is 1 times 2 power 2 because it is at position 2 plus 0 times 2 power 1 plus 0 times 2 power 0 which is 2 power 2 which is 4 right so we're converting from binary to decimal notation so pick any number in decimal notation let's say the number 512 so how would you write that in decimal notation so simply it's 5 times ten decimals to the base ten binaries to the base two so 10 power 2 plus 1 times 10 power 1 plus 2 times 10 power 0 which if you compute on a calculator it's simply 512 right on the other hand hexadecimal is to the base 16 so a number 512 and exit decimal is 5 times 16 to the power 2 plus 1 times 16 to the power 1 plus 2 times 16 to the power 0 and since it's 16 means you have 16 digits if you will which are 0 up till 9 then you have a b c d e f so you have the alphabets as well so from 0 to minors 10 11 12 13 14 15 16 which means that you could write a number in hexadecimal as a 1/2 this is a valid number in hexadecimal notation which simply means that a is 10 okay it's treated as the number 10 so 10 times 16 power 2 plus 1 times 16 power 1 plus 2 times 16 power 0 also B is treated as 11 C is treated as 12 13 14 and 15 time you that is because the address right here is expressed in hexadecimal notation it's good to know that because we'll see during the code that when I print the value of B it's not 3 it's a hexadecimal representation of the address of a when do we use pointers pointers are everywhere binary trees doubly-linked lists queues and stacks as well they all use points and even though if we didn't say it when we talk about arrays and let's say an array size let's say 5 is equal to blah blah blah array is implicitly a pointer an array is a container of fixed size we assign the following values array is actually a pointer towards the first element of the list so arrays are actually pointers if you use array as so if you want to print out array as so without saying which index this means that you're printing the address of and this is what it is so a tray is also a pointer that contains some hexadecimal representation of the address of the first element of the array now let's see what we can do with pointers let's go to the code right here create a new file so let's say I have a number print that number out run to make sure everything is ok now let's say you would like to declare a pointer towards your number pointer number so the way you would do that is you would include an asterisk saying that this is a pointer towards the address of your number so let's try printing pointer number pointer number one so as you can see it's a hexadecimal representation of the memory where number is stored so what does that mean it means that I could simply print the value as well as pointer number so asterisk before pointer number simply means that print the value stored in the memory address this one so run we're going to get one right because we're pointing at number and number contains 1 so the value of pointer number is 1 what does that mean it means you could change the value of number without having to say a number is equal to something like number is 2 you don't have to do that you could do this value of pointer number is 2 now let's check what number is right now is it still 1 or is it to run it's 2 because we changed it using the pointer could you have done so using another normal integer let's say and another number is equal to number at this point it's two so print it out run it's 2 if I say another number is 3 would number change no it will not because another number is something totally independent from number it's located at another memory address they're not linked to each other using some sort of pointer or something no so let me remove this another number because I think I made my point using this block of code okay so basically what we did right now is declare the number declare the pointer that points at this number change the value of pointer number and printed number and saw that number indeed changes let's declare arrey and let's print out a Reyes also what do we get we get also a hexadecimal representation of the memory address of array which means that array is pointing at the first entry of the array to keep track of all the array we have to point at the first element of the array right so what happens if I do this int array pointer is equal to array and star because it's a pointer right see out let's print array pointer as you can see they both hold the same address because they're both pointing at the same location right so they're both similar pointers using a ray pointer I could loop over the list the array I have right here so what I can do is this so let me do for empty I equals 0 I less than 5 my plus was and at each iteration let me not only print array pointer I'm going to also print the value so the value of array pointer with a comma for clarity and the value of array pointer so so what i'm doing here is printing array pointer which is the address of the current array pointer and the value of the array pointer and also I'm incrementing at a pointer I'll show you what incrementing of winter means so run so here I'm pointing towards array cos at this point at a pointer and array are pointing towards the same int or integer location so this is the memory address of the first location or the first entry of array which is 10 then upon incrementing I'm not adding one incrementing a pointer is totally different from incrementing an integer it means go to the next memory address which is in our case and ends so incrementing from one INT address to another one is jumping four bytes and so as we said here and end has four bytes right so incrementing or jumping from one end memory address to another one is adding four bytes so let me show you what I mean let's go here again so this is the memory address or the block of memory so right here let's say have a complicated has the notation that ends with 10 then here we will have 14 because this is allocated 4 bytes then the next one is 18 and so on so as you can see here we have 1994 98 the ninety eight plus four is nine see so count eight nine a b c an ad for to c c d e f then you have you know the one here nine would become a would have a zero here and you can see the corresponding values right that's the basics of pointers let me comment everything out what else could you use pointers for let's say i have a function right boy do something and pass it a value a or an integer a and change the value of a within the block of code to a hundred now let's declare an end to a note that everything is commented here so I'm going to start from scratch and a is one print a before executing the function run so a is one do something is the name of my function passes a then print this message after executing oh what just happened it's still one so what happened is that when you write a function and you change values or affect the value of certain arguments they will not be changed upon execution of the function well then the function a is 100 so let me throw it off within the function a is a so well then the function a is 100 but after executing the function a goes back to its normal value to its latest value which is 1 however if you pass it by an efference if you pass the address of a as so things are totally different and here's what I mean run a is a hundred so if you want your values to change after the execution of the function you have two options this is the first option passing an integer by reference or by its address or you can do something else void do something else and star a you could pass a pointer and change the value of the pointer so within the function the value of a is because a is now a pointer right so if you want to change the value of a declare it as a pointer then here print the value and here you'd have to and here I cannot say in star it is 1 right on because it's a so ante the value of a is 1 as so and here do something else run and there you have it so as you can see pointers also get affected within a function so which means that their value is changed within the function now that being said since arrays so end array I mean copy/paste the array I had here that a 5 and let's pass an array to void do something for arrays so the way you pass an array to a function is by end a as so so let's say I would like to change this the first value of my array would that do the trick let's pass the array so do something for arrays pass the array then let's print out all the elements of the array for int I 0 I less than 5 I plus plus C out array I so is equal to array I and and go and there you can see that the first value has been changed so since an array as we said is a pointer means that if you change values of arrays within a function the values will change so that's basically it for pointers those are the basics that you should always keep in mind how to change a value of another variable using pointers what does a pointer hold in its content which is a hexadecimal representation of the memory address arrays are also pointers they're recorded as pointers we could loop over the array and so using pointers and the value of the array pointer is the hexadecimal representation of the first entry right here the first entry of the array and last but not least functions if you pass an end to a function and change the value of that variable within the function it will not be affected upon execution of the function there's two ways of to so ie if you want to change the value after the function has been executed or you're done executing the function you could do so by passing it using the address or by a pointer perfect let's talk about structs so you'd want to use drugs when you've got a bunch of variables or even methods that are related to one another so let's say I would like to create a struct that models some sort of car I forgot a semicolon here so this is the blueprint of a struct struct the keyword followed by the name of your structure car and within the scope of a struct you've got some sort of properties related to the stroke let's say you'd like to define in number of wheels and you would like to have some sort of name or brand of your car Mercedes or BMW and so on you'd also like to know the color of your car right what else you also would like to keep track of I don't know max speed you'd like to save max speed and so on you also could do some stuff like void print properties so see out let's start by printing the brand is Brad let's say you'd want to print the number of wheels you also want to know the color of the car is and max speed is max speed right okay so how do I go and create this struct all I have to do is like I say into X the type of the struct is given a name core right so car I don't know car one so I have just declared a structure of type car and given the name car one let's say I'd like to fill in the variables car one in number of wheels is for car one so I refer to the properties of the car using the dot so dot go inside the scope of the struct and access the different properties so car brand is I don't know Mercedes car one dot color is black and car one dot max speed is maybe 300 okay so now at this point of code run at this point of code I have stored in sidecar one different values of the properties now if I want to call print properties all I have to do is carve on da prynce properties and run and there you have it so we go here brand is Mercedes number of wheels is four color is black and max speed is 300 you can also print within the main using say car one dot don't know max speed within the main scope ISO 300 right you could also include pointers and print a pointer the value of a is equal to the value of a right so there's an error because we did not fill and the value of a so a is equal to say to address B and then to B is 1 right so let's say I change the value of B again to 10 here value of a is 10 because the pointer is pointing at B let's remove this this means that I could create pointers as follows let's say I would like to have a chain of course we're connected to one another so what I can do is this car start car as so so what that means is the structure car has those properties and we have this method if you will that prints the properties of the car and there's one more property which is also of type car but it's a pointer pointing towards another car so with that being said let's create another car let me type here a method instead of having to type in repetitive code a second method saying create car and it should return a car so let's create car and fill in what we have to fill in so I'll copy paste everything here and move this create the car fill in the properties of the car and fill in maybe car dog car this one right I don't know put it null for the moment which means point nowhere just to make sure that everything is properly initialized and I do not want to each time create a Mercedes car which is black as four wheels and 300 max speed no I'd want some flexibility within my function so I'll pass those parameters as you know arguments number of wheels brand color and max speed and right here number of is equal to number of wheels just so avoid confusion I'm going to name them something else number of wheels and brand uppercase color uppercase and max speed camelcase and I have to finally return the car right when I call this function new car is created and the following arguments are passed as properties to our car and finally the car that points towards another car is set to note for the moment car car is equal to create car and pass the arguments for brand Mercedes color black and Max B let's say 300 run all executed properly now let's print the properties there you have it okay so let's create two other cars and let me name this one car one car one so car car two is create car say I have a car of six wheels it's a BMW which is yellow and max speed is 400 let's print all the properties one more thing see out this car is pointing at run so 0x0 and other words is pointing at null right okay now let's figure out a way to link all those cars in one chain meaning that I want to access car two from car one and car three from car two and so on so how do we do that remove this first then I want car one dot the property car to point at or two and I want car two dot car to point out our three now we should have all the cars connected via car one so car one is connected to car to and car two is connected to country let's demonstrate this see out car one dot its property car which is the other car connected to its doc let's say of wheels right here I have an adder maybe we should use this yes so run and there you have it six so what happened here is car one has a property car which is set to car to now since we're using pointers we should access using the arrow notation and not dot notation so everything using pointers or addresses you should be using the arrow notation let's string two other properties let's say I want to print the brand and the color what else max speed right so run and there you have it this is the car connected to car one which is BMW six yellow let's access the third car connected to car one car one car it's the car connected to the car connected to car one so you can do this run there you have it there's three renault Mahanta 202 print the properties of the card itself augmented first here will remove all this so it's directly the max speed of this car so really this is a way to connect structures to one another so here you have it this is the first four lines of printing right here then comes car two then car three so insert some comments right here a natural question that you might ask is how do I print so using a loop that's a nice question and the way you can do so is as follows let's declare a temporary car which will loop over all the cars until it will be null while 10 not equal to null because that's what the address of the last car would be so car 3 right here is the last node of the list if you will so car 3 was created right here using this function and as you can see the car property of itself is not whereas the other car properties are not not they on the other hand point towards other cars right so how do we do that while temp is not non temp is equal to address of car 1 then 10 print properties I don't want to do this I'd like to do that then temp is not car one right now it should be assigned the value of the next car let's see if that works Ron also temp should be initialized outside my body so we can do this of course temp is known because we didn't initialize it outside like when we had this block of code right now and we're on it temp is null because we didn't give it a value right so let's initialize it here and do this one voila there you can see it let me insert some dashes and right here let me insert a certain counter and the counter is one first time we enter the loop we're going to print iteration number counter right let's insert some dashes right here and right here and at the end you would want to increment your counter so run there you have it now discus doesn't look good so the first iteration we're printing car one second iteration car two and third iteration car through without having to access car one two three directly we access the properties using only the temp so this is like an array of cars notice that car three is pointing at node which is nothing zero X zero Y address of nothing okay okay let's talk about class okay so classes are really similar to structures except that they could inherit from one another and they have other similarities and differences we'll be talking about in later lectures so to define a class the blueprint is as follows so let's say I want to define a class of a vehicle as so so every class has properties and methods similar to structs you could mark them as private and public so private public as so those are keywords and private are basically properties and methods that you cannot access within your main so let's say I want some private methods or properties such as number of wheels I would like to know how many windows I have number of windows is my car convertible is convertible say I need a double of max P all those properties since they're marked as private this means that we cannot access them within the main on the other hand if your properties are public you could do solos so let's say and size since size is marked as public we can access it from the main so let's create a vehicle class so vehicle lowercase with vehicle let me clear all this okay so run and we have created a vehicle now let's check could we access the private ones no we cannot we can see that there's no suggestions and if we want to access them number of wheels is equal to two for example you would see that there's an edit run it there you have it it's private on the other hand you could access size because it's public run and there you have it now this notion of private public duality is related to data encapsulation so it's a way to hide your data from other users now how do we set and get those properties within the main using methods let's create some public methods to get and set so I call them getters and setters getters is to get their values and setters is to set their values so getters I would like to get the number of wheels as so and return number of wheels right so those are my getters they return the data types of their corresponding property so since number of wheels is an int this method should return an int which is the number of wheels now setters on the other hand will set those private properties if you want to set number of wheels you have to pass how many wheels you want then you would set number of wheels to the argument you just passed let's do the same thing for the other three set number of windows so number of windows which is a private property should be equal to windows the argument we passed and so on good now let's print some stuff so see out number of wheels so get numbers right because we want to get it let me copy paste this four more times number of windows convertible max speed and last but not least the size so let's set our vehicle vehicle set number of wheels to for that number of windows to to set convertibility to true and set back speed to 200 last but not least the public one which is size set it to I don't know three and then you would want to print one and there you have it okay for two one one is true zero is false 203 you could also include some constructors and a destructor so constructor is a method that is called upon creation or instantiation of your class vehicle here at lower case so how do we write the au constructor it's as simple as writing the name of your class followed by parentheses and then the block of code that you want to be executed upon creation of the class so how do we do that it's just type in something like object of the class has been created in the simpler stuff so run there you see it this executes right here to see that it does is type gonna see out saying this is right after creation run there you have this is how you do a constructor there's another way of doing it it depends what you prefer you can do this without typing in the functionality inside all you have to do is type in a prototype the signature of your constructor which is this and right after the class so right after line 38 let me zoom out just so that you can see so this is your class vehicle all this right here all you have to do is type in the name of the class then the name of the message is also vehicle right so what you need to do is that in the message object has been created and don't forget the semicolon run there you have it let's say you wanted print properties to have a prototype then you can do this so remove all this then vehicle followed by two colons followed by the name of the left hand paste so you've got to include the return type Voight - Voight as in void right here then you have to say that this method is coming out of the class vehicle perfect let's run and there you have it so object of the class has been created thanks to line 42 then all this is setting the different properties of vehicle and then we print the properties using this method right here so that's about it let's talk about destructors what are these structures so in contrast to constructors a destructor is a method that is executed when the object has been freed from memory or destructed and this happens for example when the main function has done executing or when an object of the class has been declared within a scope of a function let's say the function is here with a function and I have a certain vehicle that's called truck and I do some stuff with truck and then when the function has done executing when it reaches line 46 for example then the truck is destructed meaning that it is freed from memory so the following method which is destructor will be called automatically without you having to call it so how do you define a certain destructor all you have to do is type in tilde followed by the name of the class vehicle that's it okay this is a prototype you can go ahead and type in the functionality right here and type in a message saying this object has been destructed to be more consistent let's be more precise on something let's give this vehicle an ID and create a getter and setter get ID pass it and or pass it nothing we turn ID right and void set ID pass it an ID and what you want to do is set the ID and properties you'd want to print the ID as well and finally pass the ID to the object set ID to this random number right here it could be a plate number or whatever uniquely points out the vehicle okay now let's run it so my idea is this all those are the properties and as you can see this object has been disrupted but let's be more accurate it's about that let's say I want to print the ID of the object that has been disrupted otherwise if I have let's say a hundred objects within my man and some of them were destructed you to some reason then this useless message is printed this object well which object you have to tell me so to do that all we have to do is change the message to the object of ID has been destructed let's say you have other classes that also include IDs so let's be more accurate here of ID which belongs to the vehicle class has been disrupted okay let's give it a shot run there you have it this is the idea of this vehicle right you can also go ahead and include a prototype of the destructor as so given that you will write the method just here let me paste this here and let me copy paste the signature of the method right here okay that's wrong and there you have it I could remove the semicolons right here by the way or I could include them whatever so there is straight what method overloading is let's create a new script called method overloading just to tell you what it is okay so method overloading is when you have methods of the same name but different signatures so what is a signature let's say void my function it's called my function this is the signature avoid my function so void my function is going to print out I am and function number 1 let's say I have another function of the same name but you should give a different argument so let's say you pass it an integer see house you print I am in function number 2 where a is equal to a and the list goes on so you can pass and a and a bullion as well I'll copy/paste all this I am in function number 3 where is so and B is so let's see what happens if I call my function so the first function should be executed I am in function number 1 if I pass it an end let's say one then this function should be executed I am in function number two where a is one that's positive boy yen sure I haven't function number three where ace wanna be is true so this is what method overloading is and in case you were wondering what if I create the same signature as so and say I am in function number I don't know for then you'd get an editor saying previous definition is here so why are you defining the same function twice you should not so this is what method overloading is let's go back to classes and to demonstrate this it's a nice way of creating several constructors let's say I have a vehicle I want to pass it the ID upon creation so all I have to do is send ID to the ID I just fast so this is an instance of method overloading creating the same method twice but with different arguments which means that right here I could save this line and pass the ID upon creation which is cool right this means that the following code corresponding to the first constructor will not be called because we are calling second constructor which is this one so to show you what I mean let's run and there you have it we weren't greeted with this message object of the class has been created not because this is the constructor that has been called we did not print any message if you want it to print a message to show you the difference let's print this I am in constructor number two right and there you have it let's include a prototype so you can do this via KHOU the equal again and tidy set ID to ID and remove all this including your ID right here so all you have to do in prototypes where you're passing arguments just include the datatype of the argument and right here let's say constructor number one and over here constructor number two so run there you have it constructor number two we have set the ID automatically upon creation let's do other stuff vehicle and if you want to pass two integers let's add this block of code right here and the ID let's say you would like to pass the number of wheels then you'd say here set number of wheels to number of wheels that you just passed let's continue the list each time we increase vehicle end and end number of windows this is constructor number four so set ID set number of wheels and set number of windows to number of windows so let me show you what I mean let's remove this passed them as arguments so let's pass for n to run and there you have it so this is the ID number of wheels and two you can continue the list right here and keep on adding or if you want just automatically include all your properties as arguments when you want to create the class so let's do that let's do that I'm going to create five and bool is convertible and set its yelling at me right here because I did not include the prototype yet youko and and and an Amul so and and and we'll set convertibility to as convertible let's copy-paste this again and add a double which is machspeed and set it right here max speed go here to include the prototype or the signature whatever you want to call it and the last one is including the public property which is the size and have it include a last int which is the size so going back here i'll copy paste last constructor and say that it's number seven and have it include the size and say that size is size what do you think so let's pass all the properties right here remove all this run and everything is correct except one thing which is the size and why is it so because I just set sizes size does that make sense no it does not so what do we do we should discriminate between the two sizes so I could say and sizes so and that this size is equal to that side so run there you have it or I could keep size lowercase but to differentiate between this size of this size so obviously you mean by this size the property of the class and this size right here you mean the argument so you should do this this dot size or arrow size run and there you have it okay so this arrow size means the size within the class whereas this size means the argument now of course this seems easier but it's nice to know how to reference different variables of the same name I'm going to do some feature of vs code which is it's an extension you could install which is called code folding I could fold my code s so it's for organization's sake and this is my main right here okay and this is my class so as you can see I have seven constructors you can also do the same thing with disruptors but I think you get the point let me see what okay let's talk about static variables so what exactly are static variables well it's a variable that is shared among all objects at the same class that's why it's a static so static I'd say I want to keep track of the number of vehicles created within my program or within my main how do I do so so is the simplest way to create a static variable let's say number of vehicles and I do not want to initialize it inside because if I do so then each time I create a vehicle number of vehicles will be set to zero so to avoid this confusion let's define it or initialize it outside so ends vehicle to Collins then let's point towards number of vehicles is equal to zero that's an initial value note that I'm writing this outside the class and also outside my main let me show you how I do that let me just fold my code so these are all the constructor is defined outside the class this is print property defined outside my class this is my class okay and here's my end vehicle right so since I defined it as private I need a getter for it so the getter should also be static so static return type and get number of vehicles then returned number of vehicles right we should note that each time we create a vehicle as such we should automatically increment number of vehicles so let's try to a method for that so there is two ways of doing so you could go inside each constructor and at the end of each constructor increment the value vehicle two columns then number of vehicles plus plus now note that here I'm incrementing the value with but you should keep in mind that all this code written in the constructor is seen within the class right because it's a constructor of the class so I'd want to increment it here and here here as well so within each of my constructors so this is not a practical way I'm just doing so to show you my point and not just that I want to decrement it each time I disrupt an object of the class so I'm decrementing it at the destructor let me add it to print properties right here number of vehicles is so let's run this there you have it number of vehicles is one because I created one let me create another one right here and give it another ID 300 max speed size is let's say for false to comfortability let's change number of wheels to six number of windows to four then print the properties again right so iran i should give it another so vehicle two because vehicle is already defined so run there you have it number of vehicles is two it would be really useful to also print the number of vehicles upon destructing right so number of vehicles now is I could also go ahead and remove this vehicle so run and I could remove vehicle right here but it's just emphasizing that number of vehicles is static that's why we put vehicle with two columns as you can see here this is the first printing up till now the number of vehicles is one the second printing is right here you can see number of vehicles is too then since we're returning zero at the end of the main function it's now time to destruct our vehicles so it's decremented 1 then 0 let me show you where this has a useful case as well function create sorry no function void create temporary vehicle let's pass it an nth ID and number of wheels windows is convertible max speed last but not least and size and right here I do this vehicle vehicle is my constructed ID number of so here I'm creating the vehicle within the scope of this class create a temporary vehicle right so let's see what happens here it's really nice to note that the vehicle is going to be destructed before the main is terminated so let me create it here and let's see what happens run so constructor number seven is called and as you can see here something really nice to observe the object of ID one three seven one nine two which belongs to the vehicle class has been destructed and now the number of vehicles is set to zero that's to a print of this vehicle right here vehicle dog print properties right so run so it's one number of vehicles is one and then it's zero because when this function is done terminating the vehicle is disrupted and therefore the destructor is called and finally the number of vehicles is zero again okay let's talk about class inheritance before we do that let me organize the cool bit right here let's get rid of that okay and let's fold the code right here with some space right here perfect so let's say you have another class that you want to create which shares the same property as the first class vehicle so in that case vehicle would serve as a father or superclass to the inheriting class for example if you want to create a core class a car is a vehicle in that case we say that car inherits from vehicle so that being said all the properties and methods of vehicle are shared by the class car so car could access all the properties and methods of vehicles as if we copy pasted everything written for vehicle to the car and you can go ahead and add the cars special properties / methods right so how do we do that let's say class car will inherit using a colin could say and head it from the public class which is vehicle right doing that you could now create a car that shares the same properties and methods of vehicles so let's not forget the semicolon after we create the car right here so let's add some properties and methods that are special to the car let's say the car has a certain model so string model right we'd like to keep it private since model is private we need getters and setters so I gather in a Center for the Model strength get model and said ok let's say you would like to have a constructor and you'd like the same constructor of vehicle so I'm not going to add all of those no I just the last one so we need all the properties here to be set in addition to the model okay so how do you go and do that in case you would like to override a method car car you take all the properties right here that you need the vehicle we want to set all those in addition to the model so we want the vehicle constructor to execute and we pass it all this ID number of wheels number of windows is convertible max speed and size right and we want one more thing is to set the model by passing this model in the constructor right here there you have it so I'm just going to do a small modification for print properties because it's been annoying me from the storage I don't know why I didn't do it so as some dashes write and say info let's create a course or car car let me copy paste all those properties give it a random ID a song and let's say this is true just to show you the difference say x for windows for wheels we need one more property which is the model let's say it's a Ferrari and so and let me print the properties so remember since car is inheriting from vehicle I could run this method as well as I'm using a vehicle so run okay here we have it this is the first print properties and second print property and the third one constructor number seven so this is the first printing we talked about this and we also talked about this now this one is for the car it's the same method being wrapped we have one more property which is model and it's not being printed because property is not found in vehicle so we cannot go ahead right here to the print properties and say see out blah blah blah then I don't know model it doesn't work right because this is a vehicle it's not a car so how do we do that you can do that simply by including the same function name print properties we should include the return type so it's a void also and right down here you could do car print properties don't forget the void and you could call print properties of the super class which is vehicle and here you could print car property which is model so model get model we should be able to see beneath this line the model did we see it no we did not I think it's this so run yep so how do you call the superclass print property by using vehicle and two columns right so you can't just say print properties because this would lead to confusion print properties you have now two methods you have one for the class and one for the class car and one for the class vehicle so if you go ahead and say print property as so as we did previously it would lead to an editor so you have to state explicitly that this print properties is the one coming from the vehicle on top of that I'd like to also print my model so there yeah that's basically it this is how you inherit from another class just go ahead and add its special properties and methods if you will and that's a so before I talk about virtual methods I'm just going to point something really important let me get rid of all this code in the main and let's say we're stumbling across this situation where I create a vehicle and I pass it all what it needs and I have a function which basically accepts a vehicle as a parameter so void of function that accepts a vehicle okay and just for argument's sake let's say this function prints the properties right we're not calling this function yet run and as we can see constructor seven is called then it's disrupted because we reached return zero and the number of vehicles is now zero so let's now call the function and pass it the vehicle object as such we have a problem here number of vehicles is now minus one so let me just remove print properties to show you what I mean this object is destructed twice why is that well simply as we said before the destructor is called when the object is destructed so once we call this function and pass it a vehicle by value and value is a keyword here and when we pass it by value so upon execution of the function a copy of this object is destructed because if we pass something by value a copy of this something is created so everything within this class vehicle is copied to V and when this function is done then V is destructed hence the number of vehicles is decremented and when we reach here return zero and the main function disruptor is called again and the number of vehicles is decremented again so how do we solve that one way to solve it you can pass a pointer a vehicle to be a pointer and then pass your vehicle by address as as such running that would solve the issue and what just happened here is that this function accepts a pointer towards a vehicle therefore no copy is created and therefore when this function is done the destructor will not be called it will be called only once at return zero of the main function that's a really important point to keep in mind when you want to pass the vehicle by value then your destructing a copy of it and therefore you should bear the consequences of the dish if you do not want the destructor on the other hand to be executed then should not pass it by value so this is it this is all you need to know before we start with virtual methods and another point to keep in mind is the difference between method overloading and overriding so method overloading is when I have a method that is implemented multiple times but with different arguments this is overloading whereas overriding is when you have the same method implemented in two or multiple different places but one in the superclass and the other one in its subclass or the class that inherits from it so an example of this that occurred is the print properties so the print properties occurs in the vehicle class right and it occurs again and the car class which inherits from vehicle so here we have void properties and the car class which is implemented right here as you can see it calls the superclass vehicle to execute the print properties and on top of that we print a new line which includes the model of the car right so that's also a really important point to keep in mind the difference between method overloading and overriding okay so let's talk about virtual methods first of all let me get rid of this method right here and it's calling mine so let's say I'm in a situation where I would like to override a method let's go to the base class which is vehicle let's say I would like to create a public method called get my class which simply prints the class I on and I am a vehicle and we've got the same method in car this time I am a car so let's create a car now let me copy paste those and should do some minor changes let's run to make sure everything is good okay everything seems good and now let's call get my class and vehicle get my class right now let's run it and it works properly okay now you're going to stumble across this problem may be within your coding experience let's say you would like to have a function that tells you who am i okay it accepts a vehicle so let's get rid of this and let's call this function first time on a vehicle and in case you're wondering you could also pass it a car right because a car is a vehicle the vehicle is not a car due to inheritance so running that would give you I am a vehicle twice because a car is a vehicle and we have this problem of minus one minus two which we talked about which can be solved as follows that's a pointer and pass the vehicle and the car as reference instead of a dot you should use an arrow because it's a pointer we talked about this on wiiu structures so run this and the issue of negative number of vehicles is solved because the disruptor is not called more than it should be called but we have this problem so I am a vehicle twice a car is not a vehicle but the method get my class is being called from the superclass instead of the subclass now this is a problem and the only way to solve this is by using the virtual method so going up to the base class which is the vehicle all you have to do is mention the keyword virtual next to the get my class going back down and running this will solve the issue so that's about it that's how virtual methods work I'll see you in the next one thank you very much for watching this lecture it means a lot that you've made it so far and I hope that you know this lecture was useful and it could help anyone out there struggling with C++ and in particular anything on pointers and object-oriented programming remember I could respond to emails or comments that have to do of the lecture if you have anything in mind I could you know create a video on that topic you could just leave a comment or send me an email what is it that you're struggling with and I'm open to all suggestions on C++ and other topics related to programming or even mathematics Electrical Engineering and all these kind of good stuff again thank you for watching and I'll see you in later lectures thank you so much and have a happy new year because we still have what at the day of recording this lecture we still have like I don't know two more days for New Year's so happy new years okay so I'll see you then