A Coding Convention for C++ Code
DiggBlinkRedditDeliciousTechnorati
article by Support@PA
Source: http://www.cs.rice.edu/~dwallach/CPlusPlusStyle.html
Purpose
------------------------------
To make it easier for us to read each other's code, here are a few guidelines to follow when writing C++ code.
Definitions
* Member functions are functions that are associated with classes.
* Data members are variables that are part of classes.
Identifier Names
------------------------------
* All variable and constant identifiers begin with a lowercase letter.
* Global variables begin with the (for example, theWindows).
* Constants begin with lowercase c (for example, cMaxWindows).
* Class data members begin with its (for example, itsCanvas).
* Static data members begin with their (for example, theirTotal).
* Both member and non-member functions begin with an uppercase letter (for example, TextStyle).
* Names of classes begin with an uppercase letter (for example, Canvas).
Class Declarations
------------------------------
Classes are declared in the following order: public member functions, protected member functions, private member functions, protected data members, and private data members. No data members may be declared public. For example:
class Application {
public:
Application();
~Application();
void Run();
protected:
void EventLoop();
private:
EventRecord itsLastMouseEvent;
}
Control Structures
------------------------------
The control structures are as follows:
if (expression) {
statements;
} else {
statements;
}
for (expression; expression; expression) {
statements;
}
do {
statements;
} while (expression);
while (expression) {
statements;
}
switch (expression) {
case constant:
statements;
break;
default:
statements;
break;
}
/*
* long comments which need more than one line use
* this block comment style, if you're writing C
*/
//
// if you're writing C++, your block comments should look more
// like this -- it's ugly to mix commenting styles
//
statement; /* short C comments */
statement; // short C++ comments
File Names
------------------------------
C++ source files use the suffix .cc. C++ header files use the suffix .h.