C++ OOP - What is encapsulation in programming?
CodeBeauty CodeBeauty
274K subscribers
131,342 views
0

 Published On Aug 17, 2020

Encapsulation is one of the most important characteristics of Object-Oriented Programming. Encapsulation helps in data hiding and is used to prevent direct access to data. In order to access that data, public methods are exposed, and the user can change and access private members of the class only by using those public methods.

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
C++ Lambdas e-book - free download here: https://bit.ly/freeCppE-Book
Entire Object-Pascal step-by-step guide - free download here: https://bit.ly/FreeObjectPascalEbook

🚀📈💻🔥 My Practical Programming Course: https://www.codebeautyacademy.com/
Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

👉 Download Visual Assist here: https://bit.ly/VisualAssistDownload
(Improves Visual Studio experience by quickly identifying and fixing code errors)

☕ If you've found my content helpful and would like to support me, you now have the option to buy me a coffee or a cookie! It's a small gesture of gratitude that means a lot to me and helps me keep creating free educational videos for you. You can use the link below to make a contribution: https://bit.ly/CodeBeauty_BuyMeACoffee
However, please don't feel obligated to do so. I appreciate every one of you, and I will continue to share valuable content with you regardless of whether you choose to support me in this way. Thank you for being part of the Code Beauty community! ❤️😇

Common question:
Why should we use encapsulation when it is much easier to access and change properties if we don't?

The answer is because we don't want to allow other developers to change and access everything they want, but we want to expose only properties that they should be able to see and change, and they can use them only if they obey the rules that we define. In that way, our data stays safe from outside misuse, because if you allow someone to have full permissions and do whatever he/she wants, it's just a matter of time when that person will wander off the intended way of building that system, create some complex overkill logic that is hard to understand, make things very dependent on one another, write duplicated code, etc.
So there are many benefits related to encapsulation: easier to maintain the code, hides the data, easier to test, reusable...

Let me give you one simple example of an application that can be used only by people that are 18+ years old. When the user registers, he enters his age, and there you have to check if his age is greater than 18.
If it is, he should be able to use the app, but if it is not, throw him an error.

Imagine also that the mentioned application has the option to edit user data. That means that the user can change his age after registration as well, so you would have to check again if age greater than 18 and behave accordingly, so writing the same code once more.

And then, if all of a sudden someone decides that the app will be for people who are 21+ only, you have two places where you need to make that change. (This is a simple example, changing 18 with 21 is easy, and having two places where you need to do that is easy, imagine having some complex logic that you need to change in 20 places)

What you can do instead is you can do the "age greater than 18" logic in the setter, and save the user's age if he is older than 18, and then write an error is he is not. That way each time that you want to set a value for the Age property, the same logic is executed, and each time that you have to change something, you do that in one place only.

C++ Object-Oriented Programming playlist:
   • C++ OOP - Introduction to classes and...   - Introduction to OOP
   • C++ OOP (2020) - What are constructor...   - Constructors and class methods

C++ for beginners course:    • C++ FOR BEGINNERS (2020) - First prog...  

C++ functions course:
   • C++ FUNCTIONS (2020) - What are funct...  

Follow me on other platforms:
Instagram 📸 -   / truecodebeauty  
Twitter 🐦-   / truecodebeauty  

******Initial code is available on TrueCodeBeauty Git******
https://github.com/TrueCodeBeauty/Enc...

show more

Share/Embed