One header file should contain class declaration of 1 class only. A typical header file:

//include guard begin
#ifndef NAMESPACE_PATH_FILENAME_HPP
#define NAMESPACE_PATH_FILENAME_HPP

//standard c++ headers
#include <...>

//library headers
#include <...>

//in-project headers
#include <...>

//standard namespaces being used
using namespace ...; //the whole namespace
using ...; //a single class

//library namespaces being used
using namespace ...;
using ...;

//in-project namespaces
using namespace ...;
using ...;

//namespace path
namespace Namespace1 {
namespace Namespace2 {...

  //class declaration
  class classname {
    ...
  };

//namespace path
}}

//include guard end
#endif

A typical C++ implementation file containing bodies of declared stuff in header file:

//in-project namespaces
using namespace Namespace1::Namespace2;

//static properties must be in implementation file too
sometype classname::Some_Prop = ...;

//constructor
classname::classname() {
}

//destructor
classname::~classname() {
}

//some method
void classname::some_method() {
}

Notes:

Constructor with no argument to be called without round brackets, or compiler will think it is function declaration.

//error:
sometype Somevar();

//ok:
sometype Somevar;

Notes:

Date/time type in C++ should be stored as int64.

Utilize Boost library when possible, not necessary to write own functions like to_lower, to_upper, to_string, etc.

When passing-in variables can’t be assigned in constructor, for example database connection, web connection, use constructor initialiser:

class classname {
  public:
    sometype& Some_Prop;
  
  public:
    classname(sometype& Some_Prop);
};

classname::classname(sometype& Some_Prop):
Some_Prop(Some_Prop) {
  ...
}

Creating threads with C++11 onwards:

//create thread on static method
thread Some_Thread(classname::some_static_method);
Some_Thread.detach();
//or
Some_Thread.join();

//create thread on instantial method
thread Some_Thread(&classname::some_dynamic_method,&Instance_Var);
Some_Thread.detach();
//or
Some_Thread.join();

Notes:

Try to use stack variables as they are disposed automatically. Only use pointers when there are no other choices, for example: creating entries for a vector in a loop.

Classes like standard ‘string’, ‘map’, ‘vector’…, Boost ptree,… have destructors to destroy their own pointers.

Advertisements