0

What's the benefit of using private and protected access specifiers in classes?. C++

C++ Private and protected access specifiers benefits

11th May 2020, 8:16 PM
The Unknown
The Unknown - avatar
8 ответов
+ 1
Well yes and no. Imagine you work on a huge project with 30 developers. You write this class where everything is public. In your class there is a variable that is super important, and it needs to be the value "6627221" at ALL times. Since it's public, all of those 30 programmers can set this variable to any value they want to at any time. So in your class, you need to check, at every possible moment, that the value is still "6627221", which will result in many many if statements. If you make the variable private it cannot be changed from the outside, you don't have to write all those if statements, so making the variable private *improved* performance. A slightly morbid comparison would be, if you had no skin and all the organs would hang out, that would be akin to a class where all is public. It's kind of neat, you can eat by making a little hole and spitting food into your stomach, but obviously it's not a good idea. You should use the public `mouth` for feeding, not the private `stomach`.
14th May 2020, 1:22 PM
Schindlabua
Schindlabua - avatar
+ 1
It's all about the concept of encapsulation https://www.sololearn.com/learn/CPlusPlus/1713/
11th May 2020, 8:35 PM
Saad Saleem
Saad Saleem - avatar
+ 1
It's a common question new coders have. The gist of it is that it helps you organize, and you will learn to appreciate it in longer programs. The rule of thumb is to make everything private unless you absolutely need to make it public. A longer answer is here: https://www.sololearn.com/discuss/911006/?ref=app Also use the search bar to look for similar questions, many a good answer has been given on this forum.
11th May 2020, 9:58 PM
Schindlabua
Schindlabua - avatar
0
I know all of this. But what's the benefit of doing it. Why can't I just use public ?
11th May 2020, 8:31 PM
The Unknown
The Unknown - avatar
0
I know about the encapsulation. What I need to know is why would I use private(encapsulation)I know it's about hiding my data but why would I do that?
13th May 2020, 10:19 PM
The Unknown
The Unknown - avatar
0
As I said, it's for organizing and writing maintainable and readable code. For a programmer those are the most important things. What seperates spaghetti code from a well-written program is maintainability, encapsulation and data hiding are part of that, on many different levels. I encourage you to write some larger programs (a few thousand lines, a few tens of files), you will understand. If you want to make everything public you can but trust me there will be a moment when you'll be like "Oh."
14th May 2020, 1:12 AM
Schindlabua
Schindlabua - avatar
0
So as programming performance, there's no main benefit. The only benefit is to make our code clear and easy to understand.
14th May 2020, 7:32 AM
The Unknown
The Unknown - avatar
0
I see. thanks so much for the clear explanation.
14th May 2020, 2:37 PM
The Unknown
The Unknown - avatar