Classes are to be delcared in header files (.h) and defined in .cpp files. The filenames must match the class name. To avoid multiple inclusions #pragma once is to be used as a guard in the first line of each class header file.
- Since class names ought to be in PascalCase their corresponding header and
.cppfiles must be in PascalCase as well. - All other files must use camelCase.
- The topmost
#includein a.cppfile is its corresponding.hfile, if there's one. - C++ Standard Library
- System Libraries (e.g.: WIN32 API Header)
- 3rd Party Libraries
- Project Headers
Example:
// Foo.cpp
#include "Foo.h"
#include <string>
#include <iostream>
#include <Windows.h>
#include <winsock2.h>
#include "Bar.h"- Non public member variables start with an underscore ('_') followed by lower-case letter (camelCase).
- Public member variables start with an upper-case letter (PascalCase)
#pragma once
#include <...>
#define DEBUG
namespace mynamspace
{
using namespace othernamespace;
using cout;
using endl;
// defining custom types
using FunctionPtr = void (*)();
class Bar
{
public:
static const int PublicStaticConst = 0;
protected:
static const int ProtectedStaticConst = 0;
private:
static const int PrivateStaticConst = 0;
public:
static void PublicStaticFunction();
protected:
static void ProtectedStaticFunction();
private:
static void PrivateStaticFunction();
public:
int PublicInteger;
protected:
int _protectedInteger;
private:
int _privateInteger;
public:
Bar(); // C'tor
~Bar(); // D'tor
Bar(const Bar& other); // copy constructor
Bar(Bar&& other) noexcept; // move constructor
Bar& operator=(const Bar& other); // copy assignment
Bar& operator=(Bar&& other) noexcept; // move assignment
void publicMethod();
protected:
void protectedMethod();
private:
void privateMethod();
};
} // namespace mynamespaceMore than one variable must not be declared per line. Constructs like this are strictly prohibited:
int a, *b, c = 0, *d = nullptr, **e;The pointer * semantically belongs to the type. Thus the declaration above shell be written as:
int a;
int* b
int c = 0
int* d = nullptr
int** e;Notice there's no gap between the pointer * and the type but between the * and the variable name. Variables may be indented.