C++ OOP (2020) - What is encapsulation in programming?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone my name is saldina and i make it and programming related videos so if that is something that is of interest to you consider subscribing to my channel and give this video a thumbs up as well and in this particular video i want to talk about encapsulation here i have code that we have written in the previous videos of this course so if you haven't watched those make sure to check them out i'm going to link them in the description of this video and i'm going to go over this code very quickly so that you can understand what we have written here so here i have created this youtube channel class which has four public attributes and those are name owner name subscribers count and then list of published video titles and then here we have a youtube channel constructor and then this get info method as well that just writes out information about our youtube channel and here's the code of our constructor okay and then in our main function we have created an object of this youtube channel class and we have assigned it these two values for our name and our owner name and then we have as well added three videos to this published videos list and here we have written out information about our channel so if i run this code now as you can see this is how it behaves it writes out information about our youtube channel now what i want to talk about in this video is encapsulation and what does this principle of encapsulation says well encapsulation says that these properties here should not be public these should be private and then the way to change the value the data that you store inside these properties should be really using methods that you expose and then you give access to those methods to your user and then using those methods obeying the rules of those methods your user can change the value of these properties okay so let me introduce a problem here so that you can really understand why we need an encapsulation so what happens if i say for example that youtube channel so this that we have created has let's say a million subscribers okay this is a million now we have assigned a million subscribers to this youtube channel and this should not be allowed if i run this program you can see that this youtube channel has a million subscribers now but this is not the natural way to gain subscribers for your youtube channel what you should do instead is you should give your user the ability to invoke subscribe and unsubscribe methods and then using those methods you are going to increase or decrease this subscribers counter so let's do that let's first hide these properties from our user so from whoever is going to use this class here so how do you hide these well you make them private so here instead of using public access modifier i'm going to use private access modifier so i'm going to say private oh private like this and then these should stay public so i'm going to put public here like this okay and then as you can see we have a problem here it says that this subscriber's count is inaccessible and then this public this published video titles as well as inaccessible meaning that we cannot access these private members of our class these private members can be accessed only within this class itself so how we are going to give our user the ability to change this subscriber's counter well as i said we are going to create methods so let's create two methods the first one is going to be void void subscribe like this and then what this method should do it should increase this subscriber's counter so here i'm going to say subscribers counter like this plus plus using this increment operator we are going to increase the value that we store in this variable by one and then we are going to have as well unsubscribe method so i'm going to copy this paste it here and let's say unsubscribe and then here i'm going to say that this subscriber's count is going to be decremented so now if i i will have to comment this code here and then i'm going to delete this code here and now if i want to increase or decrease subscribers for my channel what i will have to do is i will have to invoke one of these two methods so let's say for example youtube channel dot subscribe like this okay and let's invoke this method um three times for example and now if i run this program as you can see my channel so code beauty channel has three subscribers and now let's say for example that i haven't published videos in a while so people decide to unsubscribe so i'm going to invoke this unsubscribe method like this and if i invoke this get info after that i should have two subscribers well oh as you can see i have two subscribers now because three people have subscribed and then one person has unsubscribed from my channel okay so we have this code here as well that was underlined meaning that it has a compile time error and it says remember published video titles is inaccessible meaning that now we cannot access this published video titles list anymore because it is private now so i'm going to expose a method that is going to publish videos for me so here i want to create a method of type void which is going to be called publish video publish video like this and then here i want to add a video to my published video titles like this so i'm going to say published video titles push back and then what i want to do is i want to pass a value here which value well we are going to receive that value as a parameter in our function so here i'm going to add an argument a parameter and that is going to be of type string and let's call it title like this and now we are going to use this title here so we are going to push this title in our push in our published videos titles okay like this now if i want to invoke this method i'm going to invoke it like this so i'm going to say youtube channel dot publish video like this and then i'm going to do the same thing here and then here and here as well okay so now we have created these methods that we invoke and then using these methods we change these properties and these properties should be private meaning these should be encapsulated okay and now as you can see we have a channel called code beauty and then owner is myself and subscribers is two and then these are videos that i have published so far now if you look at this unsubscribe method you will notice that it has a bug what is that bug well let's delete these three lines of code let's delete these subscribe method invocations and if i run my program now what is going to happen as you can see it says that my subscribers count is equal to minus one and that does not really make sense so you cannot have minus one subscriber and we are going to fix that problem if we put here inside this unsubscribe method a check so we want to check if our subscribers count is greater than zero and only in that situation if that subscriber's count is greater than zero zero only then we can decrease this subscriber's count value so if i run my program now as you can see we have invoked this unsubscribe method but we still have zero subscribers and now we have fixed that bug that we had because if no one has subscribed to our channel then no one can unsubscribe from it okay so that was a bug that i wanted to fix and then what you can do here because here we have exposed these three methods that really manipulate the values that are stored inside these properties and then these two properties there is no way to change them outside of this class so what you can do for them for example is you can create a getter and a setter meaning a method that's that is called for example get name and that method is going to return you the value that is stored inside this name variable and then a setter method meaning set name method that is going to receive um parameter which is going to be called name for example and inside that set name method you are just going to change the value that is stored inside this name property and then you can do the same for this owner name as well so you can create a gether and a setter method so let's go over this encapsulation rule one more time so encapsulation rule says that all of these properties should be private meaning they shouldn't be accessible outside of this class and you do that by making them private so you put this private access modifier here and then how do you change the values that you store inside your properties well you expose a public methods that are going to change the data that you are storing inside your properties and here we have created these three public methods that are going to change the data stored inside our subscribers count uh property and then inside our published video list property here so you can for these two properties again you can create getter and setter method and if some of you do that please put that code in the comment section and i'm going to review it so if you enjoyed this video give it a thumbs up and don't forget to subscribe to my channel and hit that bell icon as well and i'm going to see you in my next video bye
Info
Channel: CodeBeauty
Views: 37,864
Rating: undefined out of 5
Keywords: codebeauty, code beauty, c++ programming tutorial, c++ for beginners, visual studio, visual studio 2019, c++, c++ para principiantes, aprender a programar en c++, curso de c++, object oriented programming, oop, classes, objects, introduction to oop, c++ oop, object oriented programming c++, object oriented programming explained, course, tutorial, encapsulation, what is encapsulation, what is encapsulation used for
Id: a8ZB-TPB6EU
Channel Id: undefined
Length: 11min 21sec (681 seconds)
Published: Mon Aug 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.