Please explain private and public in library header file

Hello,
I am very confused on these fundamental questions about variables in libraries. Could someone please explain to me when library variables should be:

  1. listed in a .h file

  2. listed as "private" in a .h file

  3. listed as "public" in a .h file

Private means nothing outside the class can access it.
Public is the exact opposite.

Variables should never be declared in a .h file as this is bad practice. They can be declared globally by putting them in a library and the external reference in the header file. If they are declared in the header file they will exist as a local copy everywhere the header file is included.

Variables should never be declared in a .h file as this is bad practice

Unless, of course, they are class variables, in which case, the .h is the very best place to declare them. Probably the only place.

They are defined in the class definition but don't exist until the object is created.

AWOL:

Variables should never be declared in a .h file as this is bad practice

Unless, of course, they are class variables, in which case, the .h is the very best place to declare them. Probably the only place.

Yes, they are all class variables.
Sorry for not giving enough information.

OK, class variables should almost always be private.
It generally keeps things simple and clean.

AWOL:
OK, class variables should almost always be private.
It generally keeps things simple and clean.

Thanks.

However, lets say a variable is used in only one function of the class. Does it even need to be listed in the .h file?

I guess the heart of my question is, when should you list class variables in the .h file?

A class is defined in an .h file.

If the variable is to be a class variable, it is defined within the class.

Are you asking when should a variable be a class variable? If so, that is one of the routine continuous questions of Object Oriented Design.

If you're asking whether the class variable should be public or private (or protected--but no need to go there here), that's another one but the short answer I would say is "public if you want it to be part of the interface" and "private if it's part of the implementation". Another way to say might be

I'm the Object. My variable will be public if I don't mind my users accessing me without me knowing it. If I do mind, I will instead do something like this

class Me{
   private:  
     int myVariablePPM;  // e.g. stored as part-per-million
   public
     int myVariablePpm(){ return myVariable; }          // this way I "know it" when someone tries to access me
     int myVariablePct(){ return myVariable/10000; }  // or even pretend to have variables that I don't
};

But this is just nuts and bolts OO.... not sure if its what you're asking :slight_smile:

Cheers,
John

johncc:
A class is defined in an .h file.

If the variable is to be a class variable, it is defined within the class.

Are you asking when should a variable be a class variable? If so, that is one of the routine continuous questions of Object Oriented Design.

If you're asking whether the class variable should be public or private (or protected--but no need to go there here), that's another one but the short answer I would say is "public if you want it to be part of the interface" and "private if it's part of the implementation". Another way to say might be

I'm the Object. My variable will be public if I don't mind my users accessing me without me knowing it. If I do mind, I will instead do something like this

class Me{

private: 
     int myVariablePPM;  // e.g. stored as part-per-million
   public
     int myVariablePpm(){ return myVariable; }          // this way I "know it" when someone tries to access me
     int myVariablePct(){ return myVariable/10000; }  // or even pretend to have variables that I don't
};




But this is just nuts and bolts OO.... not sure if its what you're asking :)

Cheers,
John

John,
Nice writeup. Its funny that before I read your reply I did some studying on classes and objects. I realize now that I need to understand OO better to build effective Arduino libraries.