Abstraction explained with real-life examples and code! - C++ OOP Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone and welcome to my channel in this video i want to talk about very important object-oriented programming concept which is abstraction so in this video i am going to explain what is abstraction and then i am also going to show you how you can implement and use abstraction in c plus programming language but before i continue with this video i would like to take a brief moment to introduce c plus builder it is full featured c plus ide that helps you create apps fast for all major platforms while writing less code it connects natively to almost 20 databases like mariadb oracle sql server postgres and many more the key value is c plus builders framework which are powerful libraries that do more than other c plus tools this includes the award-winning vcl framework for high-performance native windows apps and fire monkey framework for cross-platform uis smart developers write better code faster and you can start for free using the link in the description so in programming obstruction means showing and displaying the important information while hiding the unimportant and complex details now the best way for you to understand how abstraction works is using a real life example so imagine for example how a coffee machine works the only thing that you need to know in order to be able to use a coffee machine is that you put inside coffee and water and you press a button and that is how you make a coffee now that process internally is very complex so the way that internal components of that coffee machine work together in order to prepare that coffee that is very complex but you don't need to understand that that process is abstracted for you and it is hidden behind simple procedure which is put in coffee put in water and press a button and that abstraction makes it very easy for you to use a coffee machine and that same thing happens with the code so when developers write the code they should try to write it in a way which makes it very easy for other developers to use and in order to achieve this in programming we use abstraction so we hide complex details behind simple procedures another idea is that the things that are abstracted don't change very often or at least they don't seem to change for the end user what this means is that you can change the entire implementation which means you can change that complex logic that is in the background as long as you don't change the interface towards the user so as long as you don't change that abstracted level nothing has changed for your end user so imagine the following situation let's say that a new company figures out a better way to make coffee machines so that those coffee machines spend less energy they make coffee faster and they are less noisy so what they need to do is they need to implement very complex logic they need to change the way that these inner components of a coffee machine work together in order to make coffee faster in order to make that machine less noisy and in order to spend less energy they need to change that internal logic but as long as the interface towards the user stays the same so as long as our user needs to put in coffee and water and press the button nothing has changed for the user so he does not need to learn the new way to use that new machine he uses it the same way that he used the previous one even though this new machine is better so it is less noisy it is cheaper and it makes coffee faster now once a certain behavior is abstracted to the point where it is very easy to use for the end user then the obstruction becomes a standard which means that then users expect to press a button and make a coffee or to press a button and take a selfie or to push gas pedal and make their car go faster even though all of these processes all of these procedures are much more complex our end user does not need to know anything about that complexity so once we have this standard then if a new company appears and that company wants to make smartphones for example that company knows that hey there is this standard there is this contract that says that if a user presses this button that means that he wants to take a selfie so now it will be up to that company to make and implement that complex logic of taking a selfie and then to hide it from the end user so that the only thing that the end user knows is press a button take a selfie because if they make it so that the user needs to press three buttons in order to make one selfie then no one will want to use their smartphone so the same way that you can in real life create this abstraction layer which hides all of the complex details in the background and then displays only the important information you can do the same in programming so i as a programmer if i create a class that implements a lot of different functionalities which are very complex and then other developers wants to use those functionalities what i can do is i can say hey i am going to provide you with this abstraction layer and that abstraction layer will hide all of this complexity in the background and then i am going to show you only the important information so only the functionalities that you can use inside that class and you don't really need to know how those functionalities are implemented in the background so you don't need to know anything about that complex logic but here is only a simple interface that you can use in order to access those functionalities so in order to implement this abstraction layer in c plus we use abstract classes and an abstract class in c plus plus is a class that has at least one pure virtual function now if you are not familiar with abstract classes and pure virtual functions i am going to link a video here and then i will put it also in the description so make sure to watch that video first because it will help you to understand what i am talking about better so what i want to do now is i want to create an abstract class which will be called smartphone and that abstract class will represent this contract which means that inside that abstract class i will declare important functionalities that all smartphones need to have so let's jump inside our visual studio and let's do that so let's create a class called smartphone i will say class and then smartphone like this and what i want to do inside this class is i want to declare one public functionality which will be called take a selfie so public and then void take a selfie like this now in order to make this class here an abstract class what we need to do is we need to have at least one pure virtual function inside that class so we will make this function here a pure virtual function again if you are not familiar with virtual functions pure virtual functions and abstract classes i will link a video in the description so make sure to watch that video and then come back to this video so in order to make this function a pure virtual function first of all you add virtual keyword here and then you say that it is equal to zero like this so by doing this this class here has become an abstract class and that means the following first of all we cannot create instances of abstract classes but we can create pointers of abstract classes and then this function here this take a selfie function will not be implemented inside this smartphone class so smartphone class will not provide the implementation for this function here but whoever decides to inherit from this smartphone class will need to provide their own implementation of this function here so now our smartphone has become this abstracted layer now our smartphone class is this interface which says here are all of the important functionalities that smartphones need to have so whoever wants to use a smartphone here are all of the functionalities that you can use without knowing the logic of how those functionalities are implemented and then whoever wants to make a smartphone you need to implement this complex logic for all of these functionalities here so now what i want to do is i want to create a specific type of smartphone so i want to create android smartphone for example so that means that i will create a class called android so let's do that let's say class android like this and what i want to do for this class here is i want to say that it signs this contract called smartphone so how do we do that well it's very simple you just inherit from this class here so let's do that let's say inherits publicly from a class called smartphone let me copy this okay so by doing this by signing this smartphone contract what we have said is that we will implement all of the functionalities that this contract here implies and that is only one functionality called take a selfie because if we don't do that we will not be able to use objects of type android so let's demonstrate that first let's create a smartphone pointer and let's call it s1 so smartphone one and i will say that it is equal to new android okay and we are able to do this so we are able to make a base class pointer point to derived class because this android is a smartphone because it inherits from a smartphone class so android is a smartphone and because of that we can use a base class pointer in order to point to an object of derived class but we have a problem here which says that object of abstract class type called android is not allowed and the reason for that is because pure virtual function called take a selfie has no overrider which means that we are not obeying the rules of this contract here which says that whoever signs this contract will have to provide the implementation for this function here and our android class is not doing that so now we are not able to do this unless we implement this functionality here so let's do that let's say public okay and what i want to do is i want to implement a function called take a selfie like this so this take a selfie function will have very complex logic and very complex implementation in real life but for demonstration purposes i will just write out that android took a selfie so i will say c out and then let's say android like this and then let's say selfie okay so let's say that this is the implementation of take a selfie function so now as you can see the error has disappeared and what i can do now is i can say smartphone one so s1 and then take a selfie like this and keep in mind that we use this symbol here instead of dot when we want to access members of a class when this object here is a pointer and in this situation this here is a pointer so in order to access the objects of its class we use this symbol here instead of a dot okay so if i run this program let's see what will happen okay i believe that this is too small for you to read so let me fix that this is readable now and it says android selfie so let's close the console and let's do another thing let's say for example that another company wants to make their own smartphone so let's create an iphone for example in order to do that i will create a class called iphone like this and this iphone class will also have to sign this contract here so it will have to inherit publicly okay public from this smartphone class like this which means that it will have to provide its own implementation for this pure virtual function so let's copy this code here and then we are going to change the implementation i will say that inside iphone class take a selfie means that iphone is taking a selfie of course this is going to be a very complex logic in real life this is just for demonstration purposes but one thing that i have achieved with this is that for my end user so for someone who wants to use a smartphone he does not need to know anything about how android takes a selfie or how iphone takes a selfie the only thing that is going to change if he wants to use an iphone instead of an android is this part of the code here so now we are just going to say that our smartphone called s1 will be equal to new iphone like this so now if i run this code as you can see everything works as it should but this second implementation is invoked okay so it says iphone selfie so let's close this now another thing that we have achieved with this is the following and this is the true importance of abstraction so now the developer that works on this android class does not need to know anything about this iphone class the only thing that this developer here so android developer needs to know is that he needs to provide the implementation of this function here for android okay and then our iphone developer does not need to know anything about how this android class works but the only thing that he needs to know is how iphone takes a selfie so the only thing that he needs to implement is this logic here and then for our third developer that works on this code here he does not need to know how android takes a selfie so this part is hidden this complexity is hidden and then also he does not need to know how iphone takes a selfie this complexity is also hidden the only thing that he needs to know is this abstraction here so he only knows that if he wants to use a smartphone he can take a selfie and someone else is going to provide that complex logic of how selfie is taken so he can use this functionality here without worrying about the complex details of how that functionality is implemented so let's say for example that we receive a new request that says that every smartphone will need to have the functionality to make a call so what that means for this contract here is that we will need to add another rule to this contract so we will need to add another pure virtual function to this contract here so i am going to copy this pure virtual function and i will just rename it so that it is called make a call for example okay and then what this here means for our android developer is that android developer will need to provide the implementation of making call for android devices so let's do that i am going to copy this function here and then i will change the name so that it is called make a call and the implementation of that function will be for example android calling like this so this here is going to be the job of our android developer so of developer who works on this class here and he will not need to worry about anything else except the implementation of this function here so let's close that and then the job of our iphone developer will be to provide the implementation of this function for iphone devices so let's do that now i am going to copy this function again and i will call it make a call and then the implementation for make a call inside iphone class will be iphone calling like this for example and then again our iphone developer so developer who works on this class here will only have to worry about the implementation of this function so he does not need to know how android is going to implement the same functionality nor how the users are going to use this functionality here so again i am going to hide this complexity and then our third developer who works on this code here knows that the contract has changed so now every smartphone has this functionality here as well and the developer who works on this part of the code here does not need to know how this functionality is implemented inside this class nor this class he only knows that now every smartphone can make a call so what he can do is he can say smartphone1 dot make a call like this and everything should work as expected so if i run my program now okay as you can see it says that iphone is calling okay so what we have achieved by using abstraction is that now we have this contract here called smartphone and this contract represents an abstract layer which says the following hey all the developers who want to use a smartphone here is a list of all the functionalities that smartphones have and you can use those functionalities without knowing the specific details on how those functionalities are implemented and then for all the developers who want to make their own smartphone you will have to provide the implementation for all these functionalities here and that implementation will be hidden in the background so that whoever wants to use a smartphone does not need to know anything about those complex details which are hidden so now android developer only works with his part of the code and then iphone developer only works with his part of the code and then whoever wants to use this smartphone does not need to know anything about android implementation nor iphone implementation he only needs to know about this contract here so i hope that this video helped you to understand the idea of abstraction which is the idea of hiding complex details behind simple procedures so you make a class and then you provide an interface so that whoever wants to use your class does not need to know anything about complex details of how certain functionalities inside that class are implemented but he has a very simple interface which says hey here are all the functionalities that you can use and here is a simple way for you to use those functionalities okay so again if this video was helpful please give it a thumbs up because that helps me a lot to reach more people and if you would like to see more videos like this one in the future subscribe to my channel and click the bell icon and you will get a notification from me every time that i publish a new video and then also if you have any questions or any topics that you would like to see in the future feel free to leave those in the comment section and i will make those videos in the future so thank you very much for watching and i am going to see you in some other video bye
Info
Channel: CodeBeauty
Views: 17,799
Rating: 4.9822221 out of 5
Keywords: c++ oop, oop, programming, abstraction, what is abstraction, abstraction vs encapsulation, abstraction in c++, abstraction oop concept, abstraction programming concept, abstraction simple example, what is abstraction in programming, interfaces in C++, how to implement abstraction, oop concepts, codebeauty, code beauty, for beginners, abstraction for beginners, abstraction and encapsulation, c++, abstract classes in c++, pure virtual functions, real life example of abstraction
Id: Ui7Dca5Kbvw
Channel Id: undefined
Length: 22min 16sec (1336 seconds)
Published: Wed Apr 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.