(6.6)--6.6Programswithmultiplefilestruc.pdf

上传人:刘静 文档编号:57972272 上传时间:2022-11-06 格式:PDF 页数:15 大小:646.80KB
返回 下载 相关 举报
(6.6)--6.6Programswithmultiplefilestruc.pdf_第1页
第1页 / 共15页
(6.6)--6.6Programswithmultiplefilestruc.pdf_第2页
第2页 / 共15页
点击查看更多>>
资源描述

《(6.6)--6.6Programswithmultiplefilestruc.pdf》由会员分享,可在线阅读,更多相关《(6.6)--6.6Programswithmultiplefilestruc.pdf(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Programs with multiple file structuresPrograms with multiple file structures6.6When developing a software with complex functions,the thinking of modularization is often adopted to divide the software into multiple function modules,and then assign different modules to different people in charge,so th

2、at realize the cooperative development of a complex software by multiple people.Module1Module2Module nSoftwarePrograms with multiple file structures6.6When writing complex programs,it is often necessary to adopt the modularprogramming method with multiple file structure,which means,the functions and

3、variable definitions of the same functionality are placed in the same file,and thefunctions and variable definitions of different functionalities are placed in multiplefiles.Programs with multiple file structures6.6Advantages:1.Make the program structure clear,easy to manage and search;2.Theimplemen

4、tationofdifferentfunctionmodulesisdistributed in different files,which could avoid the problem thataccess conflict may occur when multiple people are modifying afile at the same time in the collaborative developing of aprogram.Programs with multiple file structures6.6 Definition of data type,such as

5、 structure,enumeration,etc.The declaration of user-defined function,usually we put the declaration of all functions defined in a source file in one corresponding header file.Symbolic constant definition and macro definition.In the multiple file structure of C+,users can customize their header file(w

6、ith file extension h),which usually contains the following parts:Header filePrograms with multiple file structures6.6If you want to use these user-defined data types,global variables,symbolic constants,inline functions and user-defined functions in a file,the only thing you need to do is to include

7、the header file in which they are included,without repeated definition and declaration at every time.In general,the header file only contains the declaration of the user-defined functions,not the definition.External declaration of global variables.Definition of inline functions.Include other necessa

8、ry header files.Programs with multiple file structures6.6Source file The definition of a user-defined function should be given in the source file(.cpp).Before calling these functions in other source files,we only need to include the header files in which the declaration of these functions are contai

9、ned.The definition of global variables should also be contained in the source file,and the external declaration of global variables should be contained in the header file.To use global variables in other source files,the only thing we need to do is to include the header file in which the external de

10、claration of global variables is contained.Programs with multiple file structures6.6EgAssume that a student record includes name,student ID and score.Write a program to input theinformation of N students in a class,and calculate the total and average scores of all students,then finally output the in

11、formation of each student and the calculation results./The first file global.hconst int N=3;/Define symbolic constant N to represent the number of studentsstruct Student;/Define Student struct to represent the information of studentschar name10;char num10;int score;extern Student g_stuN;/External de

12、claration of the Student struct array g_stuPrograms with multiple file structures6.6/The second file global.cpp#include global.h/Include the user-defined header file global.hStudent g_stuN;/Define the Student struct array g_stu/The third file studentInfo.h#include global.h/Include the user-defined h

13、eader file global.hvoid StudentInfoInput(Student&);/External declaration of functionvoid StudentInfoOutput(Student&);/External declaration of functionint StudentScoreSum();/External declaration of functiondouble StudentScoreAvg();/External declaration of functionPrograms with multiple file structure

14、s6.6/The fourth file studentInfo.cpp#include using namespace std;#include studentInfo.h/Include the user-defined header filevoid StudentInfoInput(Student&stu)/Definition of functioncinstu.namestu.numstu.score;void StudentInfoOutput(Student&stu)/Definition of functioncoutstu.name,stu.num,stu.scoreend

15、l;Programs with multiple file structures6.6int StudentScoreSum()/Definition of functionint i,sum=0;for(i=0;iN;i+)sum+=g_stui.score;return sum;double StudentScoreAvg()/Definition of functiondouble avg;avg=StudentScoreSum();avg/=N;return avg;Programs with multiple file structures6.6/The fifth file stu

16、dent.cpp,the master program#include using namespace std;#include studentInfo.hint main()int scoreSum;double scoreAvg;int i;for(i=0;iN;i+)/Input the information of the N-th studentcoutPlease enter the name,student ID,and score of the i+1th student:;StudentInfoInput(g_stui);Programs with multiple file

17、 structures6.6scoreSum=StudentScoreSum();/Calculate the total scorescoreAvg=StudentScoreAvg();/Calculate the average scorefor(i=0;iN;i+)/Output the information of the N-th studentcoutThe name,student ID and score of thei+1th student is;StudentInfoOutput(g_stui);/Output the total score and average sc

18、orecoutThe total score and average score is:scoreSum,scoreAvg,respectivelyendl;return 0;Programs with multiple file structures6.6EgTo avoid global.h being included repeatedly,we could use the following commands:#ifndef GLOBAL#define GLOBAL#include global.h#endifQuestion:the header file may be included repeatedly.How to solve this problem?Answer:the combination of macro definition and conditional compilation can solve the problem of including header files repeatedly.Programs with multiple file structures6.6Next,we will learn how to develop programs with multiple file structures in the IDE.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 大学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁