Hello, and welcome to the 61st tutorial on the c++ program series. And in this part we will connect class inheritance. c++ provides the functionality for a class to inherit a normal class methods and variables. This is useful as you could have a base class that contains generic methods and variables, and specific classes that could inherit the or have their own properties as well. So for example, you could have a vehicle class and that could be the base class, let's say and then from there, you could have a car class that inherits from the class of truck classic boy Class A airplane class. And each one of these will probably have their own unique variable their methods as well obviously, you would generally you would want them to have their own unique wherever the method because if they didn't, there's sort of no point in harriton from another class if they are willing take two classes I've done the same anyway.
Okay, the base class. So for example, on a airplane, you might have a jet Variable object property. And because obviously an aeroplane, but on every other vehicle or any every vehicle, so the vehicle class class and for speed property. Let's go ahead and open up our project and show you how to implement a really simple c++ class inheritance example. Class inheritance is one of the core fundamentals of loop aka object oriented programming. So something that you definitely want to get your head around the topics are getting a bit more complex in this series.
So don't look understand straight away to the marriage, just watch the video a few more times. Google it if you want to get to the lower resources available, apart from our log messages and you should be all good Olden credit class codes vehicle will be public. In speed and age. They were going to create a class called car and then we're going to inherit from the vehicle class and to do that you do cold on after you Last night public via public good to go through the class for coupler vehicle and then in heavily public Truck Show and we can do is simply implement the measures so we do call amenities, objects and classes cramming, create objects from the classroom already implemented. BMW truck. big truck simply don't really know any truck names maybe GMC The truck actually be a lot of it coming to truck I just generally don't drive trucks don't have a car being the speed of the sun that 200 being the data is broken equals true.
That's upsetting the BMW. big truck, truck equals equal four. big truck dot age equal 55. Now it has plenty there's the P and the B dots beat. Tt rolling. Everyone this we get a value of 200.
Print here. If you have a look, car or BMW is an object of the class car, and car doesn't specifically have any variable code. Speed is inherited all the variables and methods and all methods at the moment, all the variables and methods from vehicles of your speed and same with big truck. So we do for change, infamous, is broken as well, which is the variable declared within the core class. If we were to create a object of view, classic important, actually have is broken, we're going to change this to a truck changes to Trump charm, which is like big, which is like is broken. It's a specific variable for the car class or of the truck class in this example, changes to age run every day 55 so that's the number in that we want to show you is we will create another class more specifically to call it BMW.
You can even inherit from the car class itself, like this and that run perfectly a Okay, so you can have a vehicle class that then a car class inherits all the vehicle properties, and the car class could have its own specific postal general car properties and BMW could inherit from the car which in From the vehicle, so it gets all the car properties or the properties and then maybe BMW have their own general properties as well, which obviously each manufacturer at this point in their own little nice little cool things. So witness removed this, but that's the last thing to know you can inherit from an already inherited class. And another thing we're going to show you, sorry about that graduated constructor. And if we do our colon colon car, brackets here, and if we take credit private variable into it, and we try and do by equals nine, whenever you don't have either private member or vehicle, basically, obviously, cuz it's public.
It doesn't inherit any of the private variables and methods but if we were to change this to protected and when they succeed because protected is essentially the private it makes all the variables and methods private but it allows you or allows classes that inherit from the class okay the vehicle class to get those properties as well which is great. So the way to have private wherever the methods so they're only accessible within the class itself, but they can be inherited from. Well, that's it for this part of the tutorial. If you have any questions, feel free to message us at support at sonar systems. The curriculum the email will be in the description you can comment on this video we'll just start the method as well why YouTube or requiring for source code will also be in the description. The next part of this series we'll be looking at multiple class inheritance and initial.
Thanks for watching and I hope you have a nice day.