 Hey everybody it's Brian and welcome to our 24th C++ tutorial. I should also wish everybody a Merry Christmas as it is ironically December 24th Christmas Eve. Today we're going to be discussing classes and inheritance. So what is inheritance? Well inheritance is when a class inherits the properties and methods of another class. Sounds confusing but it's actually very simple. Let's say class and we'll say fridge and this is like short for refrigerator. Public int temp. So there we have a refrigerator class with a public variable called temp for temperature. Let's actually make another class here. We'll call it truck. Change this to speed. So now we have a truck class that has a public integer of speed. So we have two totally different classes. One refrigerator, one drives. Now we're going to make a third class. We'll call it freezer truck and let's just get out of there. Public and it inherits the truck. Hope if I could spell today. And it also inherits the fridge class. So this is multiple inheritance. Now what this is saying is we have a class called freezer truck. You notice how there's nothing in it but it's going to inherit both the truck and the fridge. So when we go ahead and we say freezer truck, call it m truck, you notice how suddenly magically it has both a speed and a temperature variable. Now you notice how the speed variable comes from truck and the temp variable comes from fridge. That's how multiple inheritance works. Now in a normal classroom setting they're going to go over inheritance and then they're going to go over multiple inheritance. I covered both at the same time because they're essentially the same thing. You just have to understand that you can inherit properties from multiple classes. So you can be not just a truck or a refrigerator, you can be a truck and a refrigerator, like a refrigerated truck. And to use this you use it just like any other class. So he's going 100 miles an hour and temperature is a balmy 20 degrees. And let's just go see out. I mean as you can see there's nothing really new here. It's the exact same thing you've been doing. But now you understand that you can create classes and inherit the properties of other classes. Now you might have noticed the public keyword. What is going on with that? Well let's run this example real quick and then we'll explain. I just want to show you that this does indeed work. See it's going 100 miles an hour and it's 20 degrees. Now the public keyword says you're going to inherit the public properties of truck. Notice how it's inheriting int speed. Let's make another one called int gas. How much gas that truck has. Now when we go back to our truck and we try to do that, you notice how suddenly it only sees that? Well it's because we cannot inherit private properties of class. They're private. They're to the class only. Only the truck class can see gas. Now we are inheriting the public parts of truck. So we are inheriting speed. Now if you have a function int truck for example void set gas int m gas. Let's just make a little function here and we'll say gas equal m gas. Then yes we will inherit set gas and we can manipulate this private variable. Let's try that out here. Notice how gas has a little lock next to it. It means we can't touch it. Set gas and we'll say we're at 100 gas. Let's copy this, paste that, and let's try to access gas. I just want to show you exactly what's going on here. Notice how there's a build error and it says truck gas cannot access private member gas. So this goes back to our getter setter conversation. So we would have to actually say int get gas and return gas. Let's actually say get gas and we have to include our brackets here. Let's run this and sure enough now it works. It's going 100 miles an hour. It's 20 degrees and it has 100 gas. So that is how inheritance and multiple inheritance works with public and private variables. Now it would be incredibly stupid to try to do something like this. Inherit the private variables of the truck because you can't access them anyways. So there's really not a whole lot to do here. Oops, I cannot type today. Public fridge. There's another access modifier we should cover and it is called protected. Actually let's set it up here in the fridge class. Just get rid of some clutter here. We'll say bull has freon. Not sure if I spelled freon right but freon is the chemical inside of a fridge that makes it cold. All right so there's freon and what does protected mean? Protected means it's not public. It's not private. It's somewhere in between. It's private to the class but it is available to all classes that inherit from it. So let's go back to our freezer truck example here and you see how it has a different icon. The lock means it's protected. Has freon equal true and we can run this and well let's see if it runs. Has freon cannot access public members outside of the class fridge. Okay the reason why we cannot access this is because we're trying to access it outside of the class. Now if we were to access it inside of our freezer truck example here let's say set freon. Let's just say this has freon equal true. Now we can say set freon and that function inside of truck simply turns the has freon bull to true and when we run this it should compile just fine. And sure enough there it works. So that in a nutshell is a about a two week course in some computer science classes but we've condensed it down to I think about five ten minutes. It's actually not hard once you get used to it. You just have to play around and you have to understand that a class can inherit other classes and they can inherit the public and the protected parts of the classes and you can only access the protected parts of the classes from within a subclass or the class itself. Remember how when we went into freezer truck we can set the has freon variable but when we try to do it in the main function it can't do it because it doesn't have access. So this is Brian. I hope you found this video educational and entertaining and Merry Christmas. I'm going to take a couple days off, hang out with my family and just relax.