- 2.1. Our emphasis
- 2.2. The basic goal—a major difference between C++ and Java
- 2.3. Constructors and destructor
- 2.4. Operator overloading in C++
- 2.5. Operator overloading in Java
- 2.6. Flow-control constructs
- 2.7. Manipulating character strings in C++
- 2.8. Canonical class structure
- 2.9. Overcoming macrophobia
- 2.10. Program readability
- 2.11. Error detection and exceptions
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:
- private data members
- accessor functions
- inverse conversion operator
- arithmetic operator functions
- relational operator functions
- 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.