In this video, we'll learn how the class member access
modifiers work with inheritance.
We'll also learn about the protected class member access modifier.
We've already seen the public and private class member access
modifiers, and we know how they work.
While c++ provides a third access modifier named protected.
We define protected data members and methods exactly
as we do public and private.
We provide the protected keyword followed by a colon
within the class declaration.
All data and method declarations that follow the protected block
will have protected access.
So protected class members are accessible to all
methods of the class itself.
Protected class members are also accessible by any derived classes that
have been derived from the base class.
However, protected class members act like private members.
So they're not accessible directly from objects of either the base
class or of the derived class.
So you can see the protected class members act very much like
private class members unless there's inheritance involved.
Let's see the syntax once more so we can discuss exactly what's
available to the derived class.
Here you can see that we have a base class and that base class has public,
protected and private class members.
We'll use three integers: a b and c each within a different
access modifier to understand how inheritance works with access
modifiers in the next few slides.
Since c++ provides three types of inheritance, we have to
look at each type individually since the access depends on the
type of inheritance involved.
In the case of public inheritance, public class members are
inherited, and they're also public in the drive class.
Protected class members are inherited, and they're also
protected in the drive class.
Finally, private class members are also inherited, however, the derived
class does not have access to them.
Any attempt to access base class private class members from the derived
class will result in a compiler error.
This is the most common form of inheritance used in c++.
You can see that the rules make sense.
Public is public.
Protected is protected, which means that no matter how deep
the class hierarchy goes, the protected class members will be
accessible by all derived classes.
Finally, something marked as private in the base class implies
that the developer of the base class doesn't want to grant
access to those class members.
Now let's look at protected inheritance.
Remember, this is not is a inheritance.
The public-based class members are protected in the derived class.
Protected base class members are protected in the derived class.
And private-based class members are not accessible from the drive class.
Finally, let's look at private inheritance.
Again, private inheritance is not is a inheritance.
So in the case of private inheritance, the base class public
and protected class members are private in the derived class, and
the private class members are not accessible from the drive class.
This may seem confusing and a lot to remember, but it's
really a very logical approach to inheriting class members.
As mentioned previously, inheritance in c++ is a very complex topic.
We'll concentrate on single public inheritance in this course.
Now let's head over to the ide and walk through an example,
using access modifiers and public inheritance and live code.
Okay, so I'm in the IDE.
I'm in the section 15 workspace in the ProtectedMembersClassAccess project.
And what I've got here is a real simple class similar to the one
that we created on the slides where I've got a base class
that I'm declaring right here.
And it's got a public integer a, which I'm initializing to
0 and a method called display.
I've also got a protected integer b and a private integer c,
both of them initialized to 0.
Now the first thing we'll look at is this line right here, line 11.
Obviously, member methods have access to everything in the class, so there's
no problem here accessing a b or c regardless of their access modifier.
So this will be just fine.
Something else I wanted to note was that if we have a friend of
this base class, right, a friend function or a friend class.
Then we'll have access to public protected and private
information in this class.
Okay, so let's look at this first at the object level before
we even get into inheritance.
Here we have a base class.
Why don't we create a base object, and it's right here on line 31, I'm
creating an object called base and its class is base right up here.
Okay, we've got those three integers: a b and c.
Now let's access them all.
So let's say base.a is 100, base.b is 200, and base.c is 300.
Now what do we expect?
Well, we've been through this before with public and private.
The only difference here is protected.
We expect base.a to be okay, right.
It's public in base, so the objects have access to it.
And we expect base.c to give us a compiler error,
right because it's private.
We don't have access to private data from our objects.
This is nothing new.
This is what we've learned all along.
Now there is no inheritance in here, right, because we're only dealing
with that base class right now.
There's no derived class yet here.
So in this case, I also expect the compiler error because as far as the
compiler is concerned, the protected and private access modifiers really
are the same when it comes to objects.
So this object base has no business messing with b or
c because they're protected and the other one is private.
So if I compile this -- if i try to build and run this, I should get two
compiler errors and you can see them right there on the red arrows right
here: one says b is protected and the other one should say c is private.
Okay, so that makes sense, right.
That's working with objects.
So let me comment these two lines, and we'll move on now.
Okay, so now let's talk about inheritance.
Right here we're create -- we've got our base class just as before.
Now I'm creating a derived class.
I'm deriving this class publicly from base.
So I'm inheriting everything in base and I'm using those rules that
we saw with public inheritance.
So a is public in my parent, so it's public in me.
B is protected in my parent, so it's protected in me.
C is private in my parent, so I inherit it, but it is not accessible.
Okay, so it's important to understand that c is indeed inherited, and we'll
look at that when we walk through the debugger when we step through it.
You just don't have access to it from within derived.
It makes sense that it's part of you since it is part of your parent.
Okay, so now what do we have access to here.
Well, we should have access to a and b, right, within our derived.
We should not have access to c.
So why don't we write a real simple method here.
We'll just call it void access base members, let's say,
and this won't do anything.
We'll just access those things and see when we get compiler errors.
So we're going to say a equals 100, let's say, right here.
Now again, we expect this to be okay because a is public in
my parent, it's public and me.
We also expect b equals 200 to be okay because it's protected in
my parent, it's protected in me.
This is a member function.
It's got access to it.
This is the problem here.
If I say c is 300, c is not accessible.
So we expect a compiler error here.
Okay, again, remember c is there.
If you need to access c or modify c, you can't do it directly from derived.
You'll have to go through some getter function or setter function
provided in the base class, but we'll talk about that later.
So let's compile this now, and we expect to get a compiler error
right there on line 28, and that's exactly what happens, right.
C is not accessible.
So you can see how these rules apply.
Now I'll comment that out.
And we'll do one last thing is we'll come down here now.
And remember, so what we did was we created base object and tried
to access those three data members.
Then we created a derived class and saw what the access was here.
Now we're going to do it from a derived object, so let's do that.
Let's say derived, let's just call it d, and we'll do the same thing.
We'll say d.a is 100, d.b is 200 and d.c is 300.
Okay, so take a look at a.
I'm accessing the a data member in the derived class.
Well, let's move up here just a little bit.
A is going to be public, so i should expect that to work.
But look in b and c, protected and not accessible.
I don't have access to protected data, and I certainly don't have access
to something that's not accessible.
So we expect this line to be okay, and we expect both of these to
give us compiler errors right here.
All right, so let's give this a build and run.
And there you see everything else is clean.
I'll scroll up just a little bit.
And there you can see the compiler errors.
And it says just like you would expect, this is protected in this
context, and this guy is -- oops sorry, I forgot a semicolon, let
me do that one more time, make sure we get the right compiler errors.
There we go.
So we still got the same two compiler errors, but there
are different errors now.
So in this case, b is protected, so we don't have access to it.
And down here, c is private, so we certainly, don't have access to it.
Okay, all right, So that's really what I wanted to show you in this video.
You can see that this makes sense.
All these protected data members will be inherited, inherit, inherited
all the way down the hierarchy so you've got access to them throughout.
Okay, so the last thing I want to do is I want to put a break point
right here on line 42, right, where we're creating that derived object d.
And I want to debug this and step through it, mainly just so
we can see what d looks like.
Okay, so I'm right there at line 42.
I'm going to step, and I've just created that object.
And let's refresh this over here.
And first, let's look at base.
Our base object, which you can see right here on line
36, has an a b and a c in it, that's exactly what we expect.
B and c are 0, and a is 100 because I set that to 100 on line
37, as you can see right here.
Okay, but now what about the derived object.
What would you expect that to look like internally.
Well, it is a base, right, because we're using public inheritance.
You expect a b and c to be in there, and that's exactly what you have.
There's your base part, and there's a b and c.
So that derived object d has a base object within it.
It seems obvious right, but a lot of students get this confused
because of that one rule that says private elements are not accessible.
Somehow they sometimes think that not accessible means you didn't
inherit it, and that's not the case.
You can see, it's really here, right.
You can see that that c is there.
It has to be there since you are one of your parents.
So we're not deleting stuff.
We're not disinheriting stuff.
So we're getting everything from our parent.
So just because we don't have access to, it doesn't mean it's not there.
I wanted to be really clear on that because I see students
misunderstanding that a lot.
All right, so in the next video, we'll talk about constructors and
destructors and the order that stuff happens in with inheritance.