vc多文件创建方法的学习笔记
2010-09-19 17:35
489 查看
在大概一个月以前我写的程序大都是单文件的程序,而且在学校老师老师也是以单文件程序来交的,而我们在以后的工作中要面临的是多文件程序的创建,那么下面就一个C++程序,在vc6.0环境下为例来说如何创建一个多文件的程序。
现有一个程序代码如下:
class animal
{
public:
animal()
{
cout<<"ainmal construct"<<endl;
}
void eat();
void sleep();
virtual void breathe();
};
void animal::eat()
{
cout<<"animal eat!"<<endl;
}
void animal::sleep()
{
cout<<"animal sleep"<<endl;
}
void animal::breathe()
{
cout<<"animal breathe!"<<endl;
}
class fish : public animal
{
public:
fish()
{
cout<<"fish construct"<<endl;
}
void breathe();
void sleep();
void eat();
};
void fish::eat()
{
cout<<"fish eat"<<endl;
}
void fish::sleep()
{
cout<<"fish sleep"<<endl;
}
void fish::breathe()
{
cout<<"fish breathe"<<endl;
}
void fn(animal *pan)
{
pan->breathe();
}
int main()
{
fish fh;
animal *pan;
pan=&fh;
pan->breathe();
return 0;
}
在这个程序代码中,我们现在相应的项目文件的目录下创建四个文件,分别命名为animal.h,animal.cpp,fish.h,fish.cpp,然后在vc中将这些文件添加大工程中。在animal.h文件中的代码为:
#ifndef AMIMAL_H_H
#define AMIMAL_H_H
class animal
{
public:
animal();
void eat();
void sleep();
virtual void breathe();
};
#endif
即头文件中只写程序的声明,这里的预编译命令是为了解决在主函数调用时,头文件重复被调用出错。在animal.cpp文件中的代码:
#include"animal.h"
#include<iostream.h>
animal::animal()
{
cout<<"ainmal construct"<<endl;
}
void animal::eat()
{
cout<<"animal eat!"<<endl;
}
void animal::sleep()
{
cout<<"animal sleep"<<endl;
}
void animal::breathe()
{
cout<<"animal breathe!"<<endl;
}
相应的fish.h和fish.cpp文件的代码分别为:
#include"animal.h"
#ifndef FISH_H_H
#define FISH_H_H
class fish : public animal
{
public:
fish();
void breathe();
void sleep();
void eat();
};
#endif
和
#include"animal.h"
#include"fish.h"
#include<iostream.h>
fish::fish():animal()
{
cout<<"fish construct"<<endl;
}
void fish::eat()
{
cout<<"fish eat"<<endl;
}
void fish::sleep()
{
cout<<"fish sleep"<<endl;
}
void fish::breathe()
{
cout<<"fish breathe"<<endl;
}
这里注意在头文件包含时,该段程序要用相应的头文件时才包含。好了,一个多文件的程序就建立成功了,每个文件编译,链接,执行就可以了。
现有一个程序代码如下:
class animal
{
public:
animal()
{
cout<<"ainmal construct"<<endl;
}
void eat();
void sleep();
virtual void breathe();
};
void animal::eat()
{
cout<<"animal eat!"<<endl;
}
void animal::sleep()
{
cout<<"animal sleep"<<endl;
}
void animal::breathe()
{
cout<<"animal breathe!"<<endl;
}
class fish : public animal
{
public:
fish()
{
cout<<"fish construct"<<endl;
}
void breathe();
void sleep();
void eat();
};
void fish::eat()
{
cout<<"fish eat"<<endl;
}
void fish::sleep()
{
cout<<"fish sleep"<<endl;
}
void fish::breathe()
{
cout<<"fish breathe"<<endl;
}
void fn(animal *pan)
{
pan->breathe();
}
int main()
{
fish fh;
animal *pan;
pan=&fh;
pan->breathe();
return 0;
}
在这个程序代码中,我们现在相应的项目文件的目录下创建四个文件,分别命名为animal.h,animal.cpp,fish.h,fish.cpp,然后在vc中将这些文件添加大工程中。在animal.h文件中的代码为:
#ifndef AMIMAL_H_H
#define AMIMAL_H_H
class animal
{
public:
animal();
void eat();
void sleep();
virtual void breathe();
};
#endif
即头文件中只写程序的声明,这里的预编译命令是为了解决在主函数调用时,头文件重复被调用出错。在animal.cpp文件中的代码:
#include"animal.h"
#include<iostream.h>
animal::animal()
{
cout<<"ainmal construct"<<endl;
}
void animal::eat()
{
cout<<"animal eat!"<<endl;
}
void animal::sleep()
{
cout<<"animal sleep"<<endl;
}
void animal::breathe()
{
cout<<"animal breathe!"<<endl;
}
相应的fish.h和fish.cpp文件的代码分别为:
#include"animal.h"
#ifndef FISH_H_H
#define FISH_H_H
class fish : public animal
{
public:
fish();
void breathe();
void sleep();
void eat();
};
#endif
和
#include"animal.h"
#include"fish.h"
#include<iostream.h>
fish::fish():animal()
{
cout<<"fish construct"<<endl;
}
void fish::eat()
{
cout<<"fish eat"<<endl;
}
void fish::sleep()
{
cout<<"fish sleep"<<endl;
}
void fish::breathe()
{
cout<<"fish breathe"<<endl;
}
这里注意在头文件包含时,该段程序要用相应的头文件时才包含。好了,一个多文件的程序就建立成功了,每个文件编译,链接,执行就可以了。
相关文章推荐
- Linux下创建文件和文件夹的方法-老男孩Linux运维学习笔记2
- 孙鑫VC学习笔记:第十一讲 图形重绘方法二 利用元文件
- 【JavaEE学习笔记】Servlet_02_创建Servlet方法,获取文件,响应对象
- jar文件的创建--学习笔记
- Linux内核学习笔记之seq_file接口创建可读写proc文件
- PHP学习笔记,curl,file_get_content,include和fopen四种方法获取远程文件速度测试.
- Java6学习笔记55——多线程编程——线程的创建方法1
- 学习笔记:病毒感染PE文件的基本方法
- Mysql DBA 高级运维学习笔记-创建Mysql用户及授权的多种方法实战
- JavaWeb学习笔记-spring-13-AOP-创建切面(静态普通方法名匹配切面)
- linux 可执行文件创建 学习笔记
- IOS学习 collectionView 创建应用管理 涉及单独用类方法从plist文件中取数据
- VC中判断目录,文件是否存在,创建目录,求目录或文件大小的方法
- 学习笔记--类创建和方法的创建
- Python学习笔记九:列表(1)--列表元素介绍及创建方法
- 创建组件核心文件——学习笔记
- Java6学习笔记56——多线程编程——线程的创建方法2
- Unix学习笔记-------目录遍历实例----Linux下判断是否是一个目录还是一个文件的方法
- Java学习笔记67. 创建多线程的方法 -- 通过继承thread类创建多线程
- 微软企业库4.1学习笔记(五)对象创建和依赖注入方法