C++ OOP - What is encapsulation in programming?

preview_player
Показать описание
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 ⬇️

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.

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++ functions course:

Follow me on other platforms:

*******Initial code is available on TrueCodeBeauty Git*******
Рекомендации по теме
Комментарии
Автор

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
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.

CodeBeauty
Автор

i've tried to learn oop concepts from half a dozen sources .
this is by far the most practical example i've seen .
thank you, and keep up the good work!

suyashmisra
Автор

I love how you're all business. Your videos are straight to the point with no BS. Thank you for being here on YouTube.

mistersir
Автор

I started watching the OOP playlist as a part of my preparation process with the constructor video and I must say your explanations are very precise and to the point. Instead of dumping out every piece of information, you walk us through the concepts with a possible realistic scenario. Hope to see more content from your side. ❤

theKuntalPaul.
Автор

Thanks for keeping it simple, please keep these videos coming.

bolow
Автор

so inside the class under the public access modifier :
void setName(string name) {
Name = name;
}
string getName() {
return Name;
}

Progged
Автор

Amazing, clear and to the point explanation with a very relatable example. Keep up the good work.👍

rameshwarprasad
Автор

The way you explaining I started falling in love with coding..😇❤️

vivekanand
Автор

Preparing for an exam tomorrow. 🥴
Crash course!! Really helpful, the playlist. Thanks for awesome Content!

russelyg
Автор

She is the most beautiful coding teacher

felicytatomaszewska
Автор

from kenya i have never subscribed to any youtube chanell and this is the first one dear, Thanks alot, i have been looking for some one with good explanation like yours

bryanwere
Автор

hello Saldina this is John from freecode camp and i love the effort thanls for sharing

dukujohnkenyi
Автор

VERY PRACTICAL, VERY INFORMATIVE, VERY DEMURE, VERY MINDFUL.

judebubune
Автор

Started with oop concepts, I got stucked & then these explanation on c++ oop help me a lot
- thanks a lot 😊

azadr
Автор

Thank you so much for your videos and your time.

I have been studying OOP for this semester and I did not understand a word from my Professors.

Your OOP Course on Free Code Camp Yt Channel has helped me a lot plus I am a subscriber to your channel and watched the Html and css playlist and C++ for beginners.

You are a great tutor.

PS Note:
Try to keep your smile in every video because it is very nice.

tawansiforlifetfl
Автор

by far the best definition ... thank you

SHIVAMSHUKLA-bezj
Автор

you are really a nice teacher, till now I am not able to grasp the concepts of oops but because of your lucid explanation I am able to make it. thank you so much 🙂
getter setter methods--
public:
void getter(){
int ChannelName = " peaky blinders";
}
void setter(int setname){
setname = ChannelName;
}

technicalteam
Автор

this playlis is really helpfull to me, i wanted to thank you so much because of the effort that you made, keep shining up lady

imSky
Автор

Hope you get to 10k+ subs very, very soon.

monoman
Автор

I really wish I got to know about your videos two years ago

ephraimokoriko