用C++开发工程时项目的管理: .h和.cpp文件的存放

在用C++开发大型工程时,如何组织文件的存放很重要。总的来说,.h文件用于存放对类的定义,包括类中的数据成员和函数成员。.cpp文件用于实现了类中的成员函数。为了便于理解,有以下例子:

我们用C++实现了一个二叉树的类,其中对类的定义放在BinaryTree.h文件中:

#pragma once
#include”iostream”
using namespace std;
struct Node
{
 char data;
 Node *l;
 Node *r;
};
class BinaryTree
{
 int level;
 int node_num;
 int array_len;
 Node *temp[100];
public:
 Node *root;
 BinaryTree(void);
 ~BinaryTree(void);
 void count_node();
 void pre_order_bitree(Node *N);
 void in_order_bitree(Node *N);
 void post_order_bitree(Node *N);
};

在.cpp中需要包含 #include “BinaryTree.h”,主要的代码是类中成员函数的实现,以前序遍历为例:

void BinaryTree::pre_order_bitree(Node *N)
{
 if(N!=NULL)
 {
  cout<< N->data;
  pre_order_bitree(N->l);
  pre_order_bitree(N->r);
 }
 
}

在主函数中只需要将头文件包含进去,就可以了,

#include “StdAfx.h”
#include”BinaryTree.h”
int main()
{
 BinaryTree biTree