I usually try to arrange the declaration of the class so that it's easy for others to use the said class.
The usual is thus: public/protected/private
, in this order, because it simplifies life for the readers.
- People who use the class can stop reading once reaching the
protected
tag, anything after is none of their concern.
- People who derive from the class can stop reading once reaching the
private
tag, anything after is implementation detail.
This, coupled with not writing the code of the methods at their point of declarations, makes for an easy to read interface.
There are however a couple of tricks:
- when using metatemplate programming, you may need to declare types first, methods afterward, so you end up with 2 series of
public/protected/private
- when using the Key idiom (instead of
friend
), you have a public
section that is in fact dedicated to only a small portion of the users and is best isolated either at the bottom of the normal public
section or after the protected
section.
Finally, as to comment about the layout issue among the attributes. Encapsulation means that attributes should be private
. So, either you have a struct
and everything is public
or you have a class and everything is private
, mixing the two means breaking encapsulation, and that's a bug in the making.
public
specifier in the middle of it). – Shiller