《(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.