Access Specifiers in Inheritance with Example | Public - Protected - Private | OOPs in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] you watch some guys done my offer simple snippets back with another video tutorial on C++ programming especially the object-oriented paradigm I'm in this video tutorial we will talk more about inheritance and this is specially going to be mostly practical and will directly go to the code because one video before this that is in previous video tutorial we discussed the inheritance concept and we saw a lot of theory around it you saw the different types of inheritance and we also saw the modes in which inheritance can occur that is Dax is specifies so most of it was theory and in the end we all saw a very basic program example so I thought let's let's take this to a practical aspect and practical approach and let's try to understand more about how access specifiers working in inheritance and basically in object-oriented programming so that will be very clear when we actually see a program example so we will directly move on to the programming part now if you haven't watch the previous we didn't tell and if you don't know what inheritance is in the basic theory about it you might want to check out this video on the top right corner also make sure you subscribe to this channel because there is a lot of video content related to information technology programming languages and other concepts and other subjects being covered on this channel and if you are new to this channel consider subscribing so with that being said let's get started so quickly open up your day C++ IDE because we directly start off with the practical aspect of inheritance and we will go ahead and type out this code from scratch so I have already written down the basic boilerplate of the C++ code so you can just pause this video and type this out so in this video what I'm going to showcase you is how public protected and private access specifiers work and how they are different I will try to show you programmatically how it works and what happens when we inherit a base class in this different three modes that is public mode private mode and protected mode so if you have seen the previous video we already know that inheritance can happen in these three different modes that is the derived class can inherit base class properties in three different modes so we will see how they differ from each other and what are the restrictions that are applied at each access specifier level and in general you'll also see the different access specifiers order the three access specifiers so let's start off with creating a basic class so what we will see is class my base class so there's a simple class and inside this I'm going to create the three different access specifier so first one is going to be look inside that I'm going to declare a variable index and I'm going to say protected and that I'm going to create one more variable that is into Y and then lastly in private and create in said or in Z now in the public side I'm going to create the constructor that is the default constructor now I've also covered what is constructor and destructor in this video tutorial you can see on the top right corner so I'll see my base class and inside the cell assign X is 0 Y is 0 Z is 0 so the constructor and the functions that as member functions usually come inside the public part because the functions directly access the data members so in object-oriented programming the concept is like the data members are usually kept private or protected depending upon if we are going to perform in irritants or not they not make they are not directly made public and the functions inside the class that is the member functions are kept public so the public functions can directly interact with the data members and not any function which is outside the class so this adds a level of security so that's why all the constructors on are on public and constructor has to be in public only ok so let's let's speed this class as it is pronounced so we have my base class you can see I have created three parts public protected and private inside each I have one variable each so index is in public int Y isn't protected and in Z is in private so now what I'm going to do is I'm going to create an outside function so I will say void my outside function so this function is outside the class and I'm going to pass object of this class that is my base class obj and what I'm going to try to do is I'm going to directly print in x y&z individually so I will say C out X colon obj dot X similarly I will try to print Y and print print Z so what I'm trying to do is my outside function is directly trying to access Delta member of this object now according to object-oriented programming principles or conventions there has to be a function that is there has to be some number that has to access the Rita members so what what essentially should happen that we should have an error over here so what I'll do is I will first try to print X directly because X is public and since it is public essentially this should work directly so let's go to the main function and try to create an object of this class that we just created my base class I will see obj one and once the object is created the default constructor is called and all these values are set to zero in fact let's try to set them as five so that when we print the values you can see them and then I'm going to call our function that we just created so here in the main function I give that function call my outside function and inside it is asking for objects so I will say obj one so I am passing this object so what should happen is this object obj1 which is just created over here has these values x equals to five y equals to 5 and zero equals two files DITA members this is passed in this function and then we are printing for now the x value of the object so let's see if this works let's sort of save this execute compile and run and there you go you can see the X is printed as 5 which means that the DITA member was directly accessed because it was public now what I'm going to do is I'm trying I'm going to comment this out and let's see if Y can be printed now Y is inside protected section so it is a little restricted so let's see if this works I'll just see you this and go to execute compile and run and there you go you can see that it is an error you can see in my base class Y is protected which means that you cannot directly print it over here or you cannot directly see object dot data member you need a function that has to access it so inside the public part I can say void print protected data and inside that I'll say C out y : y and then so I created a function inside the public part which can now access the y part that is the data inside Y so instead of obj dot Y let's try to see if this works so LJ print protected data save this compile and run and we are getting an error so let's see what error is this ok we are trying to use the extraction operator oh sorry insertion operator here which is not being allowed so let's just cut that cut this out and let's handle that in save this function so anyways we are going to get this message so just close this CEO this and let's hit compile and run so there you go you can see now the y-value is printed because we are accessing the y-value using this avoid print protect data method inside our outside function so we cannot directly print and similarly for if I just comment this out and if I go and try to print Z value which is inside private it would give the same error because profit is also a restricted access specifier and you cannot directly access data members that is Z using the dot operator with the object so you need one more function over your print private data which you have to create over here to print value of Z so this was within the class so this is how these access specifiers differ and here you can see protected and private pretty much restricted the printing but there is a slight difference in protected and private and we will see that when we actually create one more class so let me just comment this out now we will create one more class so we have base class over your now in below that what I'm going to do is I'm going to create class my derived class and then I'm performing inheritance using the colon operator so this is the syntax and first I will publicly inherit this class that is my base class just copy this and paste it over here and then regular code for derived class now inside derived class when we are publicly inheriting the properties from my base class all these things are going to come as it is over here which means that public index is going to be public index over here that is this this part is going to come as it is all of it over here except the constructor so now what I'm going to do is I'm going to try and create object of my derived class inside the main function and then again let's try to print or access the data members which have come inside this my dear L class from my base class so my derived class also has int X int Y and injured and in Texas public int wise protected and injured is private so their access specifier doesn't change because we are inheriting it publicly so essentially we have in let me just cut this out we have public and X we have protected into Y and we have private in Z inside this my derived class I do not have to again type it out because this is the beauty of inheritance that we don't need to type code again and this provides reusability and maintainability so to prove that let's try to create an object of my derived class and I'll say obj now since X here is public we should directly be able to access it so I'll do it inside the main function itself instead of creating an outside function let's just not use this directly try to access it in the main function so as I see out X : obj 2 dot X so let's see if this works and let's try to execute compile and run and there you go you can see X is printed as it is because X value in the derived class is also public because we are publicly in editing it now what I am going to try to do is now of course protected Y is not going to come over here so if I say object to dot Y it is going to throw an error because it is protected and you cannot directly access it and private jet is not coming inside this class itself and I made a mistake over here so what I'm trying to say is private jet is never going to come inside my derived class because it is private so anything that you don't want to inner it in your derived class has to be inside the private part so my dear class will only have two variables index anind Y and not set so I made a mistake over there so now what we're going to do is let's try to inherit this in a protected way now when I say protected the public int in the base class becomes protected in in the derived class protected Y stays as protected Y and private doesn't come as it as you know because it is not going to go to the derived class at all so to prove that now we have found inheritance in a protected mode let's try to print the x value let's try to save this and let's try to compile and run and it should throw an error so as you can see in my base class is inaccessible because we just performed in a dense in a protected mode int X and int Y inside the derived class now become protected now lastly if we have if we perform any returns in a private mode the index and into our going to come in Z is not going to come in the right class because it is already private in the base class but in X and in Y will be inheriting in the derived class you know privately so inside derived laws they are becoming private so private X and private Y again if I try to access X it will not be allowed but now since both of these are private if I create a third class class my derived class two or class three and try to inherit this first derived class in any mode be it private public or protected so I will say public since these two are private in the forge derived class they will become or they will not be accessible in the third derived laws because they already become private so this is why when we inherit the first derived class as private the next level of derived class will never inherit those properties so that is why in most cases when we want to perform multi-level inheritance that is there is one base class below that there is one child class or derived class which is in editing directly from the base class and then at level two there is one more child or derived class which is inheriting properties from level one derived class so in that case usually the inheritance is in public mode because otherwise then the last derived class it is the third or the second level of derived class will never inherit the other properties if it is in a private mode if the first dinner place is in private mode so that is the difference that I wanted to show you programmatically so if I create inherit this in a private way and now if I create a variable of my derived class three over here and try to access any object it will again throw an error ICU this in saying compile and run you can see that again in my base class is inaccessible error is throwing up so that's how inheritance works and these are three different modes that I wanted to show you programmatically so in the previous video we saw a big table and we saw the different program and we also understood how it works in a theoretical way but we did not see a program so that's what I wanted to show you so bottom line of the access specifiers is that the public part is accessible in the class and outside the class directly the protected access specifier is only for inheritance purpose so it is slightly in public but not as restricted as private because the derived class gets those properties which are in protected mode as well and the last access specifier that is private is very restricted that is whatever you write inside the private beautif um ssin or bead remember it will never be inherited by the derived class so these are three different levels of access specifiers and now you know why protected is created specially for inheritance similarly inheritance can happen in three different modes that is public private and protected and you've seen the two differences as well in the programming part when inheritance happens in public mode all the properties from the base class come as it is inside the derived class except the private part so leaving the private properties public and protected come inside the derived class as public and protected when we perform inheritance in a protected mode public and protected properties become protected properties in the derived class and the private is not gonna get inherited and when we inner it in a private mode public protected properties become private properties in the first level of derived class and again the private properties of the base class are never inherited so yeah this this is a little bit tricky but once you start coding and like get a hang of it it is very easy to understand since there are not a lot of options there only thing different mode and these these questions are sometimes asked in an interview that explain the difference between public protected private and they usually ask an example as well so this is why I thought of covering a practical aspect and practical example 3 I hope this concept of access specifiers in the different modes in which inheritance happens is now clear in a practical way as well so if you like this video give it a thumbs up share it with your friends and don't forget to subscribe to this channel peace
Info
Channel: Simple Snippets
Views: 41,660
Rating: undefined out of 5
Keywords: public, private, access specifiers in inheritance, c++ access modifiers in inheritance, c++ inheritance by simple snippets, protected, simple snippets youtube channel, c++ inheritance example, modes of inheritance in c++, public inheritance in c++, c++ programming, private inheritance in c++, c++ oops inheritance, c++ inheritance code example, inheritance, access modifiers in c++, access control in c++, c++ access control, protected inheritance in c++, inheritance in c++
Id: 1KVQVXphqJU
Channel Id: undefined
Length: 14min 47sec (887 seconds)
Published: Sun Jan 14 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.