Separating a Class into .h and .cxx Files

来源:互联网 发布:英雄杀最新探宝数据 编辑:程序博客网 时间:2024/05/21 22:36

When I implement a class in C++, I break it into two files:

  1. The header file (such as rocket.h)
      This file contains all the comments that a programmer needs to use myclass. This documents only the public members of the class.It also contains the class declaration, beginning with thekeywordclass and ending with the final closed bracketand semicolon. All of this is wrapped in a macro guard, so the totalmight look like this:
      // File: rocket.h// Written by Michael Main (main@colorado.edu), Dec 18, 2009// Comments on each public member ...#ifndef ROCKET_H#define ROCKET_Hnamespace colorado_edu{    class rocket    {    ...    };#endif

  2. The implementation file (such as rocket.cxx)
      This contains the implementations of all the class's memberfunctions. Also, at the top, I provide a comment that is a classinvariant. This comment describes how the member variables representan object. In effect, the class invariant is an implicit preconditionfor every member function (except the constructors) and it is animplicit postcondition of every member function (except thedestructor).