Members
In a class, members will be ordered as following:
Nested classes
Enums
Constants
Static variables
Static functions
Instance variables
Default constructor
Constructor with arguments
Member functions
All public members go to the top of their respective categories, protected goes under public, package goes under protected, and private goes last.
EXAMPLE:
class Foo {
public:
  
  enum class Foonums {
    FOO1,
    FOO2,
    ...
  }
  
  const int FOOBAR = 3;
  const bool IS_FOO = true;
  VERBOSE_FOO = false;
  void runFoo();
  std::string fooName = "";
  Foo() noexcept;
  ~Foo() = default;
private:
  private int fooAmount = 2;
  ...
}class Foo {
  
  enum Foonums {
    FOO1,
    FOO2,
    ...
  }
  
  public static final int FOOBAR = 3;
  private static final bool IS_FOO = true;
  public static VERBOSE_FOO = false;
  public static void runFoo() {
    ...
  }
  public String fooName = "";
  private int fooAmount = 2;
  public Foo() {
  }
  ...
}Getters and setters
Never use public variables. Always use getters and setters.
protected and package variables are okay.
EXCEPTION: There are very specific cases in which it's okay to use public, however those should be evaluated on a case-by-case basis. Usually, you can use public variables in POD (Plain Old Data) classes, where the class only contains variables and no methods at all.
Last updated
Was this helpful?