private and public

Hi Guys! I'm completely new to Arduino and don't really understand the 'private' and 'public' things in class definition. I mean, what should I put into private? OK, I understand that for ex. a class variable with a '_' can go there, but what does the term mean 'you put things in private that you want to stay in scope of the class, only the class can use it'.... What the heck does this mean? I understand that things that are in the public section are to be used by other users, but why is it necessary to put anything into the private section? What make sense in this? Sorry for the stupid question, it's just a mess in my head...

That`s a very basic OOP (Object Oriented Programming) subject. Maybe this link can get you started:

http://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html

Do you understand that every variable has scope, ie its value is available only in the portions of the code in which it is in scope ?

If you create an instance of a class, you can access the public variables and public methods through that instance. You can not access the private ones.

The _ is a convention that is often used to indicate that a variable is private.

In short, the main reason we use private is we don`t want another class to change the variable accidentally. Another class just can access and change public variable.

Thanks guys for the help, i'm now giving it a second chance! :wink:

Papatonk:
In short, the main reason we use private is we don`t want another class to change the variable accidentally.
Another class just can access and change public variable.

Replace "another class" by "anybody".

The idea of restricting scope to private is that it enables us to define a class and be able to prove that it will always have a valid internal state.

I did a sketch for someone a few days ago involving two relays (extend and retract) which must never both be on at the same time. I wrote a class with three public methods (extend, retract, and off). Because the class is simple, I am pretty confident that even if my state machine code screws up it will never be possible for both of those relays to be on.

Of course, I didn't need to make it private. I could have just written my other code correctly. The real benefit is when you are writing libraries, or working on a team. When other people will be using your code. Miscommunications happen, and some people are ahem not as l33t as others.

But what if some other piece of code does need to access the private variables? Well, that indicates that you have not properly designed the class. That is, there's something missing in its specification. Having a defined public interface and everything else private, so that you can't easily "back door" functionality that a class wasn't designed to have, and which might be changed in a future revision. It forces you to think about design rather than simply writing a hack.

It's more about big lumpy codebases written by teams and having a revision history. It's about managing complexity. It's not so relevant for Arduino programming, maybe, but anyone who does programming for a living will habitually make things private that are not part of the "outside" part of a class.