OK.
Welcome back to a another lesson on inheritance in this lesson, I am going to be kind of extending
what we talked about before and adding one or two new things to our knowledge base about inheritance.
It's going to be kind of a short lecture x.
I'm only going to go over these few things, but definitely pay attention because they will be important.
So what we're going to go over is how the constructors and D structures work in inheritance.
As in like, when is which constructor called?
And we're also going to go over this thing that we talked about, which I said you couldn't ignore in
the last lecture because I just wanted you to know that you could use this for inheritance, but this
word right here can be changed from public to other words as well.
And we're going to talk about this base class access specification, which is what this is right here
and how it relates to the base class and the difference between that and this right here.
Because remember, this used to be private at some point and we changed it to protect it so we could
have access inside of this friend.
Function, right, are overloaded upstream operator and we wanted access to the private members assist
declared in Karjat.
Eight.
It is a protected member.
Protected means that we have access to these in the base class.
These members inside of the derived classes rightly derive classes have access to these now if we put
protected.
So the point is, though, is that we're going to talk about this access, specify a lot and what it
means if we change it.
So first off, we're going to talk about the constructors and D structures, though.
So in each class, in each derive class and base class, I have added a destructor.
And I have put a printout line in the destructor saying that we are in it, so we are in the sedan destructor.
I'm doing that for each destructor and I put a little sea out in the constructor as well.
So constructor and destructor, I have little printout statements that will tell us that we are in there,
so we'll know when it's called.
And I did that for the base class and all of the drive classes.
So what I'm going to do is go ahead and run this program now, but I'm not going to print out info about
the cars using the overloaded operator because that will create unnecessary clutter.
What we want to do is just kind of see the printouts and know what order the constructors are called
in and what order the structures are called in in terms of the base class versus the derived class.
So let's go ahead and run this and we will see what the output is.
OK, so let's go ahead and zoom in on this a little bit.
I'm going to scroll up a bit, so we see that we are first in the car constructor and then we are in
the truck constructor, then car, then sedan, then car, then SUV.
So notice that we instantiate our objects in this order truck and then sit in an SUV.
So what happens with the constructor in inheritance is that the base class constructor will be called
first before the derived class constructor.
So you notice we instantiate a truck, right?
So what C++ needs to do is it needs to sees that you instantiate a truck and then it needs to look at
the superclass or base class of truck, which is car, right?
So it goes to car called the car constructor.
Then it comes back and does the truck constructor.
When we do a sedan, it has to repeat the same process over.
It says, OK, what's the superclass of sedan with the car?
I go to a car called Car Constructor, then calls a day and it does the same thing for SUV.
You see car and then SUV.
The destructor is the mirror image of this right here.
It's basically inverted, so you notice that it destroys the most recently created derived class object.
So SUV and then it goes to the car, right?
And then the sedan and then car and truck and then car.
So the whole ordering is reversed in terms of this and also it is reversed in terms of whether the base
class or drive class is called first.
So the derived class destructor is called before the base class destructor opposite of the constructor,
where the base class constructor is called before the derived class constructor.
So you can just see that it happens in this order, and then it's just a mirror image right here.
So this part right here is the inverse of the constructor up here as far as ordering.
So just wanted to show you that because it's kind of important to know what's going on and what's happening
and what order.
So the next thing that I want to talk to you about is this base class access specification keyword here.
So right now we have public.
So what this word really means is it describes how the.
Functions here and members and everything are going to be inherited into the derived class.
So since we say public.
That means that all of these methods here.
Sorry, all of these member functions here, I keep calling them methods because that's a way to refer
to them in other programming languages.
All of these member functions here are going to be inherited into Sudan and they are going to be made
accessible.
To Maine, for example, from Sudan as public member functions, so.
It says, you know, these are public right here.
It's that means that what's going to happen when we inherit this as public, it's going to make those
functions public through the sedan class as well.
So imagine that we just take all this highlighted stuff here and we just copy paste it here into this
public section.
That's basically what's happening.
It means that all of these highlighted member functions here are going to become public in Sudan and
so we can use them in Maine.
What I can do is change this if I change this to protected.
What happens is that you notice that there was a protected word right here, right, and that gave us
access to these inside of the derived classes, right?
All the derived classes.
If I put a protected.
Right here, what it means is that it's going to when we copy all of this and like invisibly make it
available here.
In our sedan class, it's not going to be public anymore.
These are not going to be public anymore when they're here in sedan.
They're going to be protected in what is protected.
Mean again, it means that they're only available to classes that derive from the classes as protected.
So if we had another class that was deriving from sedan as a sedan was the super class, then that means
that the protected car.
Member functions here would be available to that class that was deriving from it.
But the problem is is it's not available elsewhere in the client code.
Watch what happens when I go to Maine.
Immediately, these two methods coming from car, right, set fuel and set paint are actually coming
from car.
You see set fuel and step paint.
They're red now because we cannot access them in Maine because they are protected.
They are only available to whatever classes may derive from Sudan because they're being exposed to this
client code in a protected manner, in a public manner.
If I go right here, it says that fuel is a protected member of car member of car that's just telling
us that we cannot access that from here in Maine.
But notice how swetnam doors is still fine.
Why is that?
Because we put Satnam Dawes is actually a member of Sudan.
It's not a member of car.
And what did we say about the members of Sudan?
We said that their public right here, right?
That's why this is accessible in Maine.
Yet these set feelings that paint are not accessible in Maine.
Why?
Because they are protected.
We said protected car.
So now when we grab all this, these are all protected, they're not available to me.
That's why when we tried to call those functions here in Maine, set fuel, set paint, they are actually
not available to me.
So we would want to leave this as public, right?
We put it back to public.
You notice that these disappear.
So there is one more thing to do, and that is private.
And it's the same thing is protected, except rather than exposing the inherited member functions here
as protected, it's now exposing them as private so they're not available either in Maine.
So if I go here now, it says Fairfield is a private member of CAR.
So that means that it's private, constrained by private inheritance here, and I click there, and
it brings us to here and it's just like, Hey, you made it private, you made everything that we're
inheriting from car all private, so it is inaccessible to any client code.
So that's just something to note, that's what the access specified means, the access, the base class
access specify air, which is the way that you inherit stuff from the base class.
Whatever word you put here that you're just describing, the way that it's going to be accessible from
Sudan.
So remember, protected one might be nice if you're wanting to make another derived class of Sudan,
where Sudan is like a super class and now you have multiple levels of inheritance.
That way, you would expose all of car to that like derived class of Sudan in a protected manner, so
you could use it in that derived class from Sudan.
Public will make stuff from car
be accessible to client code just like these are.
I mean, these are public, right?
So if you're like, well, I'm also going to want in my client code to call all of cars member functions
here, then you need to make this public.
All right.
So that was kind of it for this lesson.
I just you know what this means and how you can change it.
You know, if you're using multiple levels of inheritance, then you might be putting protected here.
And there are some reasons why you might want to put money.
You might want to put private.
Maybe you still want to just use some of the methods in here.
You know, stuff is still stuff is still accessible to car.
If it's protected, you know, it doesn't, it doesn't matter what you feel like.
If I put private here, even I could still use stuff from car in sedan.
Right?
So for example, I can go over here to sedan, even though this is private, you know, this doesn't
work here anymore.
But if I go to sedan, let's just put something super random.
So.
Let's just say in setting some doors, we also want to just randomly display.
Let's see, like the odometer or something like that.
I don't know.
Let's just say get pain or something like that.
Like, what if we just want to show you the pain as well?
And it's like, so whenever you set the number of doors, we just have this thing that's like, uh,
you know, by the way, the paint color is.
Like that.
So what I can do in here is actually use this function in here, get paint.
So if I go back here, I can say, by the way, the paint color is in, I can just say get paint because
I have access to this function because it is inherited.
All right.
So.
It's not really saying, like all this stuff is public in terms of my sedan class, when I say private,
it means that when I'm using these.
When I copy all these over to Sudan, they become they're basically going to be put down here in this
private.
That's what you can imagine.
Like they're they're basically like put down here in the private list because all of these methods are
private in terms of the Sudan in which it's saying that I'm exposing these in a private way to the clan
code.
So we still have full accessibility to all of this in the Sudan class.
So no matter what word you put right here, private protected public, you're still going to have access
to everything here in car because you're inheriting it right?
If you were to put all of this highlighted junk down here in protected and changes to private or something,
then they would be private to car and the inheriting class would not have access to it.
So you notice, like, let's take a look here.
This is totally fine, right?
And I can go ahead and test this out.
I can print it out.
We'll go back to Maine.
I've commented this stuff out.
All right.
So let's go ahead and run this.
So you notice this says, by the way, the pink color is white, so you see that right there, right?
So if I can go if I go back here to car and I was to change this from public to private.
We start running into a ton of issues, right, if I go back here to Main, you noticed that like none
of these are accessible, like now, these aren't accessible because they're private members of car.
And if I go to Sudan, this is red too, because it's a private number of cars.
So I don't even have access inside of this sedan derived class.
So that's why these should be public.
And I just wanted to show you that if you were to change this to private, that makes that changes the
accessibility inside the derived classes.
But as far as inheritance, it changes the accessibility as far as anything that wants to get access
to the sedan, class client code, whoever imports it, like up here, you know, like we have day and
age.
That means that the way that we describe the inheritance is kind of what affects the accessibility we
would have in Maine to some of the methods that got inherited from car, right?
All of this stuff it now becomes part of sedan is defined at the accessibility level by this word.
All right, cool.
So I'm actually going to make this public because I want that to be public, right?
And I'm going to change this back.
This makes no sense to have it here, so I get rid of that.
Cool.
So now everything you notice will be accessible in here, so if I in peace, they are accessible, and
that's why I believe that as public in here.
So that's pretty much all I had to say about those two kind of sub topics.
It's important to know which order things are being called in as far as inheritance.
And important to know the different types of access specification that you can do.
If at some point you end up having multiple levels of inheritance, you'll need to know about protected
and stuff like that, right?
Because you may not want to make those members here accessible all the way down to the client.
Could you maybe just want to have it accessible to a derived class of sedan or truck or SUV, whatever
you end up trying to implement?
All right, so with that, I will see you in the next lecture.