 Hello and welcome back. In this session, we are going to talk about access control in derived classes. Here is a quick recap of the last lecture we have had on inheritance. We have seen the compositional approach and the inheritance based approach of representing a hierarchy of classes. And specifically in the inheritance based approach, we have seen how to represent a class hierarchy with base classes or super classes and then derived classes. And of course, these derived classes could in turn be base classes for further derived classes. In the examples that we have seen so far, all the data members were public and the inheritance or derivation was also public. If you recall, we use some notation like this class D colon public class B, which basically means that D is a class that is derived from class B and this derivation or the mode of inheritance is going to be public. So, this is what we have seen in the last lecture. In this lecture, we are going to see inheritance with data members that have access control defined by public, private or protected keywords. This protected is a new keyword that we are encountering now and we will soon see what this means. We will also see what inheritance or derivation in the public, private and protected mode means and together these two things will actually determine how you control access of data members and member functions in derived classes. So, let us dive straight away into the class inheritance mechanism. So, let us say this is a class named B and in this class, there are three data members M 1, M 2, M 3 all of type integer and M 1 is declared to be private, M 2 is declared to be public and M 3 is declared to be protected, but this protected is a new access control keyword that we are seeing for the first time and by the end of this lecture, you will understand exactly what this means. For the time being, let us just say that this is a new access control keyword. Now, I want to write a class D which is going to be derived from class B and I want to specify here how this derivation should be and in particular, this derivation itself can be in one of three modes. It could be a private derivation, a public derivation or a protected derivation. So, we have seen these three keywords which can appear within a class definition to represent access control of individual members of the class and the same three keywords can also be used to denote how the derivation of a derived class must happen from the base class. So, these keywords private, public and protected in this context, when you put it in front of data member declarations or even member function declarations within a class definition, these keywords can be thought of as filters that control access to the individual members of the class. Now, the keywords that are appearing here, which are the same keywords, but when they appear here and they determine how a class is derived from another base class, these keywords can be thought of as filters that control the inheritance mechanism. Now, these three keywords private, protected and public actually represent an ordering of accessibility. Anything that is private is meant to be very less accessible, something which is public is meant to be highly accessible and protected lies somewhere in between. So, going back to our example, this was the class B within which I have three data members with three different access control specifications and here I have the class D derived from B and the mode of inheritance or the mode of derivation could be one of private, public or protected and let us say I want to understand how this data member M 2 can be accessed within the derived class D. So, I sort of pick up the access control specification for the data member M 2, which is the screen box denoting public and suppose the class D has been derived from the class B in the protected mode. So, then in that case I will also pick up that information that how is class D derived from class B in the protected mode and in this derivation I am trying to access data member M 2 of class B within class D. So, I have to pick up the access control specification for the data member M 2 and so basically I get these two filters. This filter is associated with the declaration of the data member inside the base class and this filter is associated with how the derived class is derived from the base class, how it inherits members from the base class and it is really the combination of these two filters that is going to determine the accessibility and access control of members like M 2 inside the derived class D and the general rule of thumb here is that the least accessibility filter prevails. So, for example, here if D was derived from B in the protected mode and if I was trying to access the data member M 2 of the base class B inside the derived class D, then protected is less accessible than public and therefore that is going to prevail. So, the data member M 2 can be accessed within the derived class D in the protected mode. Its access control specification will be protected. Now, let us take a slightly closer look at this through some example. So, here is our class B which we have seen earlier and let us say D is derived from class B in the public mode and within this I have another data member which is of this derived class D called 10. Now, if I have a main program like this, here I am declaring I am instantiating an object D of class capital D and then I am trying to read in the members D dot M 1, D dot M 2, D dot M 3. Now, note that these members M 1, M 2, M 3 are actually members of the base class B and we are trying to access these members in an object of class D because the class D itself is derived from the class B. Now, it turns out that because M 1 is a private member of the class B, the derived class D cannot access that member directly. So, I cannot write D dot M 1 over here. I cannot access a private data member of the base class from the derived class. So, this statement is going to give you a compilation error and the compilation error will state that we are trying to access a private member of a base class from a derived class. However, can I access D dot M 2? Yes, I can because M 2 is a public data member of the class B. So, since D is derived from B, I can inherit the public data member of class B. So, I can access it. What about D dot M 3? D dot M 3 also cannot be accessed because M 3 is a protected data member of the class B. What this means is that this data member can only be accessed within derived classes that are derived from class B, but this data member cannot be accessed in the main program which is outside the definition of the class D. So, we will see more examples of this later in this lecture, but note the interesting thing here that although class D was derived from the class B in the public mode, the access control specification of the data members of class B of the base class B actually determine what I can and cannot access in the main program as a data member of class D. Things that are inherited as public can of course be accessed, private members cannot be accessed and similarly, protected members cannot be accessed when I am trying to access these data members from the main program. Now, similarly here is a object B, here is an object B of the base class B and here I am trying to output the values of B dot M 1, B dot M 2, B dot M 3. Now, because B is of the class B, so this object small b has all the three. Now, since this object small b is of the class capital B, so this object small b has all these three data members M 1, M 2, M 3, but because M 1 is specified to be a private data member, I cannot access M 1 from the main program over here. So, this will again give rise to a compilation error. M 2 is a public data member of the class B, so I can access the data member M 2 in the main program. M 3 being a protected data member can only be accessed within the definition of derived classes, it cannot be accessed from the main program. So, once again I cannot access a protected data member from the main program. So, all these statements which are marked with the cross will actually give rise to compilation errors, if you take these two class definitions and this main program and try to compile it. Now, what we are going to do now in the next few slides is to take a very close and detailed look of how the access control specifications of individual data members in a base class interact with the mode of derivation or the mode of inheritance, when I am trying to declare a derived class and how this interaction determines what data members of the base class can be accessed, where and what data members cannot be accessed. So, here is the class B which I will just shrink and put it there at the corner and let us say here is a derived class D 1 which is derived from the base class B and the mode of derivation or the mode of inheritance is public and this class D 1 has two additional data members within it, this is declared to be private, this is declared to be public. In fact, it has a third data member also which is declared to be protected. Now, because D 1 is derived from the class B, so it tries to inherit the data members of the class B. However, M 1 was a private data member of class B, so M 1 is not going to be accessible from within the derived class D 1, M 2 was a public data member of class B and this mode of inheritance is public. So, the least accessible of these two specifications is public itself. So, M 2 can be accessed inside the class D 1 as a public data member, M 3 is a protected data member of class B which means it can only be accessed within the definition of a derived class like class D 1 and since the class D 1's mode of derivation is public, so the least accessible of these two keywords which is protected and public. So, the least accessible between these two is protected, so M 3 will be inherited by the class D 1 in the protected mode. So, these are the two data members of class B which can be accessed from within class D 1 and this is their access control specification within the class D 1. Here I have also defined a public function within the derived class D 1 and this function directly tries to read values for x 1, x 2, x 3 and another public function f 2 which tries to read values for the members M 1, M 2, M 3 which are really members of the base class B. So, we want to see which of these will be allowed by the compiler and which of these will not be allowed. Now, since here I am trying to read data members that are data members of the class D 1, they are defined within the class D 1, they are declared within the class D 1. So, therefore, this function is fine, this member function can certainly access all the data members that are defined within this class. Now, what about the function f 2? Well, if you notice the function f 2 is actually trying to access the data member M 1 which is actually a data member of the base class B and moreover, it is a private data member. So, the derived class D 1 cannot access private data members of the base class and so it will not be able to access M 1 and so this will lead to a compilation error. However, what about M 2? As we have already seen M 2, the data member M 2 can be accessed by the derived class D 1 and its access control specification will be public. So, this member function f 2 of the class D 1, if it is trying to read in a value for M 2, since M 2 can be accessed from within the class D 1, this is fine. And similarly for M 3, M 3 is once again a data member of the base class B, but it is a protected data member which means it can be accessed within derived classes, but not in the main program, but here I am trying to access it within the derived class in the definition of this member function of the derived class. So, this is I can access it and here is a little main function which declares an object D 1 of type capital D 1 and then it tries to invoke the two public member functions of the object D 1 and it also tries to access some data members of the object D 1. Now, we will just see which one of these will be allowed by the compiler and which one will not be. So, f 1 being a public member function of the class D 1 and small D 1 being an object of class capital D 1, it is perfectly fine to invoke the member function f 1 in the context of the object D 1. And as we have seen here the member function f 1 can indeed access all the members x 1, x 2, x 3 of the class D 1. What about D 1 dot f 2? Well, we have already seen here that if I just give this f 2, then I cannot even compile there will be a compilation error because of this statement. So, let us for the time being say that this statement is deleted and the function f 2 just has these two statements for which the compiler is not going to complain about anything. Now, when I call D 1 dot f 2, if I have only these two statements then it is fine it will go through the compiler will not complain. But if this is the f 2 that I kept then although f 2 is a public function of D 1 and I can call D 1 dot f 2 over here, but this f 2 internally is trying to access the data member m 1 of the base class B and that is a private data member. So, it cannot be accessed within f 2 and so if I keep this statement seen greater than greater than m 1 here. And then if I try to call D 1 dot f 2 the compiler will also complain here that it cannot access the private data member m 1 of the base class B when this function is called. What about these two accesses which are directly accesses of data members? So, once again D 1 dot m 1 is going to lead to a compilation error why because the data member m 1 of the base class B is private and so it is not accessible from within the derived class D 1 and of course it is not accessible from the main program as well. What about D 1 dot m 2 is assigned 1 that is fine because m 2 is a public data member of the base class B. So, in the derived class D 1 where the mode of derivation is public it will continue to be a public data member of the derived class D 1 and since it is a public data member of the derived class D 1 I can access it in the main program. So, you see how the access control is specified this specification in the base class interacts with this specification of how the derivation on inheritance happens to really give the access control specification of a data member like m 2 which is declared within the base class B, but is going to be accessed from the derived class D 1 and then this access control specification determines whether I can access the data member within the main function or not. So, to summarize what we studied in the slide we said that well if I am doing public derivation which means if I am deriving a class from another class in the public mode then if the base class member was private then it is not going to be directly accessible within the derived class. If the base class member was protected it is going to be accessible within the derived class as a protected member and if the base class data member was public it is going to be accessible within the derived class as a public data member. Of course such a data member which is protected within the derived class cannot be accessed from the main program because protected data members can only be accessed from derived classes, but a data member that is public within the derived class can certainly be accessed from the main program. Now, here is another example where we basically have the same class definition B, the base class definition, but we have a different derived class definition where this mode of derivation is protected. So, we want to see what is going to happen with the inheritance of these data members of the base class in the derived class. Now, note that this derived class also has its own data members which are also of different access control specifications private, public and protected. Now, if you look at this data member M2 this was public in the base class B, but this inheritance is protected. So, I have to take the least of these two accessibility specifications and since protected is less accessible than public. So, M2 is going to be accessible within the derived class T2, but in the protected mode. What about M3? M3 was protected in the base class B this mode of derivation is also protected. So, the least of these two access control specifications in this case both are protected. So, the least of them is protected determines the access control specification of M3 in the derived class. And once again I have two public member functions here. This member function is trying to directly read values for the data members of the derived class. So, this should be fine because a member function can certainly access all the data members of the same class. Now, what about H2? This is a public member function of the derived class D2, but here once again I am trying to access the data member M1 of the base class B, but M1 was private. So, I will not be able to access it within the derived class T2. So, this will lead to a compilation error. What about M2 and M3? M2 and M3 are indeed accessible within the derived class T2 as protected data members. So, these two statements will not have a problem. So, the compiler will report an error here, but not report any error for these two statements. Once again if this is the main program where I have declared an object small D2 of class capital D2 and then I have tried to invoke these two public member functions of the class capital D2. And I have also tried to access a data member of the class T2 directly over here. So, let us see what will happen when I try to compile it. Well, this is a public member function of the class T2. So, I can certainly invoke it from the main program and this function is actually trying to just access the data members defined within the class T2. So, everything is fine here. What about D2 dot H2? H2 is a public member function of the class T2. So, I can certainly invoke it from the main function. However, within the member function H2, I am trying to access M1 which is a private data member of the base class. So, this is not allowed. So, the compiler will complain that here you are trying to access a private data member of the base class that is not allowed. What about this assignment? Remember M2 was accessible in the class D2 as a protected data member. Now, protected data member is only accessible from derived classes. So, if I derive another class from D2, I can access M2. But here in the main program, I cannot access M2. So, this will also give rise to a compilation error. So, to summarize what we studied in this slide, when we are having protected derivation, then if the base class member is private, then of course it cannot be accessed within the derived class. If the base class member is protected, then it can be accessed within the derived class as a protected member. If the base class member is public, then because I am doing protected derivation, therefore the least accessible of these two specifications will prevail and therefore this data member will be accessible within the derived class, but as a protected member. And finally, let us see what happens when I do a private derivation. So, I have the same class B and here I have a derived class D3, which is derived from the base class B, but the derivation is a private derivation. And once again, I have three additional data members of different access control specifications over here. Once again, M1 being private in the base class cannot be accessed within the derived class D3. M2 is public in the base class. So, it should be accessible in the derived class D3, but since the mode of derivation is private, so I have to take the least accessible of these two specifications. And the least accessible of these two specifications is private. So, M2 will be accessible within the derived class D3 as a private data member. And similarly, M3 will be accessible within the derived class D3 as a private data member. This is protected and the mode of derivation is private. So, you take the least accessible of these two specifications and that is private. Once again I have two public member functions and let us see whether the compiler should report an error on them or not. So, for this public member function I am just trying to access the data members already declared within this class. So, this is perfectly fine, no compilation problems. Here I am trying to access once again a data member which was private in the base class. So, the compiler will complain here. Here I am trying to access a data member which was public in the base class, but because of the private inheritance over here, because of the private mode of derivation over here, it is a private data member of the derived class D3, but member functions of a class can certainly access the private members of the class. So, it is fine I can access this and what about M3? M3 also has become a private data member of the derived class D3. So, I can certainly access it from a member function of D3. Now, if I have a little main program which appears like this over here and once again let us try to understand which of these will give rise to compilation errors and which of these will not. So, D3, small D3 is an object of class capital D3 and because G1 is a public member function of class capital D3, I can certainly invoke D3.G1 here and note that this function can certainly access all the data members derived in this class. So, everything is fine here. G2 is a public member function of the class D3. So, I can certainly invoke D3.G2 in the main function. However, G3 is trying to access a private data member of a base class. So, that is not allowed. So, the compiler will again report an error here. What about D3.M2? So, note that M2 has actually now become a private data member of the class D3 and therefore, M2 will not be accessible from the main program and once again that will lead to a compilation error. So, once again summarizing what we have studied in this slide is that when we have private derivation like we have over here, then if the base class member is private of course, that member is not directly accessible in the derived class. But regardless of the other two access control specifications of data members of the base class that is whether a data member is public or protected within the derived class, those data members will be accessible only as private data members. So, this table summarizes all the three previous slides that we have seen so far and this might be a good reference table for you to refer to when you are in confusion about how the combination of the derivation and the access control specification of a base class member combines to tell you whether a particular member of the base class is at all accessible within a derived class and if at all what is the access control specification in the derived class. Finally, we also want to talk about inheritance of member functions. We have seen so far inheritance of data members. Now, the inheritance of member functions basically follows the same rules as the inheritance of data members. So, we should be able to go through this fairly quickly. So, for example, here I have a member function which I have declared as protected within the base class and here I have a derived class in which I am trying to invoke member functions of the base class. Now, a member function which is public in the base class can of course, be accessed from anywhere. So, I can certainly access it from the derived class, but a member function which is protected in the base class and the mode of derivation is public. Once again, you take the least accessible of these two specifications that is protected. So, G2 will be accessible from the derived class D1 here, but in the protected mode. So, you see the access control specifications of member functions G1 and G2 of the base class. Their access control specifications in the derived class will follow exactly the same rules as we used to determine access control specifications of data members of the base class as seen in the derived class. Here is a protected member function of the derived class D1 and it is trying to access the data members x1, x2, x3 already defined within the derived class D1. So, this is not a problem. Now, here is another derived class D2 and in this case it is actually derived from the class D1 which in turn was derived from the class B. So, as far as D2 is concerned D1 is its base class and the mode of derivation of D2 from D1 is protected. It once again has some additional data members and this is a public member function of the derived class D2 and within this public member function I am trying to access member functions of the class D1 and some of these member functions are actually member functions of the class B. So, for example, can I access G1 here? So, note that G1 was inherited by class D1 from class B as a public member function. So, I can certainly access it. Can I access G2? G2 was inherited as a protected member function. What it means is that any derived class, any class that is derived from D1 like class D2 can access it. So, this is fine. Can I access F1? F1 was a public member function of D1. So, I can certainly access it. Can I access F2? F2 was a protected member function of D1 which means that any derived class like class D2 can access it. So, all of these are fine and here is the definition of a protected member function of D2 in which I am trying to access G1, G2, F1, F2 but we have just seen that all of these can be accessed from within the class D2. So, this is also fine and now here is the main program in which I have declared some objects small b of class capital B, small d1 of class capital D1 and small d2 of class capital D2 and I am trying to access different member functions of these classes. So, let us see which one of these will be allowed and which will not be allowed. So, can I access B.G2 which means in the base class B, can I access the member function G2 but the member function G2 is declared as protected in the base class B. So, it can only be accessed from derived classes not from the main function. So, this will give rise to a compilation error. Can I access D1.G1 in the class capital D1, G1 is a public member function. So, I can certainly access it, can I access D1.F1, F1 is a public member function of the class D1. So, I can certainly access it, can I access D1.F2, F2 is a protected member function of the class D1. So, it can only be accessed from derived classes and not from the main function. So, the compiler will report an error here. Can I access D2.G1, G1 is a protected member function of the class D2. Why is it a protected member function? Because G1 is actually inherited from D1 and in D1 the accessibility specification of G1 was public but this mode of derivation was protected. So, protected and public together makes it protected here. So, since G1 is accessible in D2 as a protected member function, it can only be accessed in classes derived from D2, it cannot be accessed from the main function. What about H1? H1 is a public member function of D2. So, it can certainly be accessed from the main function. What about H2? H2 is a protected member function of D2. So, it can only be accessed from classes that are derived from D2 and not from the main function. So, you see that this combination of derivation modes and access control specifications of different data members and different member functions really determines which member functions you can call from where. What can you call from the main function? What can you call from within a derived class? All of this is determined by the combination of the mode of derivation and the access control specification of the data members in the base class. So, to summarize, we looked at different access control mechanisms in derived classes. We saw how we can inherit public protected and private data members from a base class into a derived class and we also saw how we can have different kinds of inheritance, public protected and private inheritance and it is really the combination of the access control specification for a data member or a member function and the mode of inheritance or the mode of derivation that determines how data members or member functions can be accessed within derived classes. Thank you for your attention.