vc多文件创建方法的学习笔记

            在大概一个月以前我写的程序大都是单文件的程序,而且在学校老师老师也是以单文件程序来交的,而我们在以后的工作中要面临的是多文件程序的创建,那么下面就一个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;
}

这里注意在头文件包含时,该段程序要用相应的头文件时才包含。好了,一个多文件的程序就建立成功了,每个文件编译,链接,执行就可以了。

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值