免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2035 | 回复: 3
打印 上一主题 下一主题

为什么包含了头文件就能使用对应的.cpp文件? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-13 16:49 |只看该作者 |倒序浏览
有3个文件,student.h,student.cpp,main.cpp.其中student.cpp include了student.h,为什么main.cpp只需要include student.h就可以了?
我反而觉得main.cpp只需要include student.c就可以了,因为student.c包含了student.h,这样所有的关系都联系起来了.

/*********************************************************************************
student.h
*********************************************************************************/
class Student
{
     Student();
     int f();
     //................
};

/*********************************************************************************
student.cpp
*********************************************************************************/
#incude "student.h"
Student::Student()
{
     //................
}

int Student::f()
{
    //.............
}


/*********************************************************************************
main.cpp
*********************************************************************************/
#include"student.h"     //个人觉得#include"student.c" 才对
void main()
{
      Student s;
      //.....................
}

论坛徽章:
0
2 [报告]
发表于 2008-03-13 20:00 |只看该作者
头文件起声明作用,就是为了不让main.cpp看见student.cpp文件的内容,所以才有了student.h这个头文件
否则你不要student.cpp文件,把student.cpp内容全部拷贝到student.h里边去,再main.cpp中include "student.h"
一样能通过

论坛徽章:
0
3 [报告]
发表于 2008-03-13 21:13 |只看该作者
原帖由 vestige 于 2008-3-13 20:00 发表
头文件起声明作用,就是为了不让main.cpp看见student.cpp文件的内容,所以才有了student.h这个头文件
否则你不要student.cpp文件,把student.cpp内容全部拷贝到student.h里边去,再main.cpp中include "student ...

为什么不能让main.cpp看见student.cpp文件的内容?
我将student.h中的内容拷到student.c里去,再main.cpp中include student.c可以吗?

[ 本帖最后由 slowlykiss 于 2008-3-13 21:14 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-03-13 22:15 |只看该作者
呵呵,我推荐你了解一下一些基本的概念,比如头文件,源文件,以及两者之间的关系
.h文件一般用来声明变量、函数之类
.c文件是.h文件的实现
接口与实现分开是很重要的编程模式
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP