C++模块化程序设计举例
#include "stdio.h" //使能printf()函数
#include <stdlib.h> //使能exit();
#include "Static_Variable.h"
//argc 是指命令行输入参数的个数;
//argv[]存储了所有的命令行参数;
//argv[0]通常指向程序中的可执行文件的文件名。在有些版本的编译器中还包括程序文件所在的路径。
//如:"d:\Production\Software\VC++_2005_Test\Win32控制台应用程序\Vc++_Test\debug\Vc++_Test.exe"
int main(int argc,char *argv[])
{
int i;
for(i=0;i<argc;i++)
{
printf("Argument %d is %s.\n", i, argv[i]);
}
Local_Static_Variable();
Globe_Static_Variable();
Local_Static_Variable();
Globe_Static_Variable();
printf("temp3=%d\n", temp3);
exit(0); //注意:return是退出當前函數exit是退出當前程序。
}
2、模块2
在“Static_Variable.cpp”文件里输入下面的程序:
#include <stdio.h> //使能printf()函数
int temp3;
static int temp1=0;
//局部静态变量只初始化一次;
//局部静态变量是在函数被调用后保留其值的局部变量;
//全局静态变量,它的作用仅限于当前定义的文件,不能够被其他文件使用extern关键字访问;
void Local_Static_Variable()
{
static int temp=10;
printf("temp=%d\n",temp);
temp++;
printf("temp1=%d\n",temp1);
temp1++;
}
void Globe_Static_Variable()
{
printf("temp1=%d\n",temp1);
temp1++;
}
3、模块3
在“Static_Variable.h”文件里输入下面的程序:
extern void Local_Static_Variable();
extern void Globe_Static_Variable();
extern int temp3;