Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

2.8. Canonical class structure

A typical, complete class definition, even one for a simple elementary data type, consists of several pages of dense code. The maintenance programmer needs to find items of interest quickly and reliably. Therefore, most organizations’ standards for C++ or Java classes specify a preferred sequence.

Fashions change. In the early days of C++, class definitions typically began with the private data members. Today, however, many programmers place the data members last, beginning the class with the public interface (the constructors, accessors, overloaded operators, and so on). This recent practice caters less to the maintenance programmer than to the client programmer (user) who has to consult source code in the absence of usage documentation.

Actually, it makes very little practical difference. Each organization or project team should choose a preferred sequence and stick to it for the sake of consistency. Regardless of your own preference on this minor issue, you should be willing to follow the standards of the group you’re working with at the time. Where there’s a compelling reason to depart from the sequence your group expects, explain it in brief commentary.

The class examples in this book and on the code examples Website adhere to the following sequence:

  1. private data members
  2. constructors
  3. destructor
  4. accessor functions
  5. inverse conversion operator
  6. arithmetic operator functions
  7. relational operator functions
  8. I-O stream operator and external conversion methods

We put static data, static functions, and private methods near any other members to which they’re closely related. Again, if your organization prefers a different sequence, that’s fine. The sequence of class members is not an issue worth discussion.

  • + Share This
  • 🔖 Save To Your Account