你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2014-02-24] 来源:Linux社区  作者:michael_kong_nju [字体:大 中 小]

在用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; //call the construct-function
 biTree.count_node();
 cout<<"先序遍历是:"<<endl;
 biTree.pre_order_bitree(biTree.root);
 return 1;
}

linux
相关资讯       C++开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款