Trending Now

Splitting a program code into multiple files

Multi-file programs are those programs where code is distributed into multiple files communicating with each other. This is a more practical and realistic approach towards advanced programming where we want loosely coupled code module that can communicate with each other. A question here arises that why should we prefer multi-file programs.

Advantages of using multiple files for a program are numerous. For instance, if you write code for a class in a separate document, you can use that class in multiple programs. It increases reusability of the code. Furthermore, if you want to change anything in a class, you will only have to change it in that particularly document and the change will be automatically reflected in all the documents referring to this document.

Furthermore, it is advisable to write large complex programs in multiple files. And last but not the least, in large organizations; several programmers are working on a project. In such scenarios, each programmer is responsible for designing designated modules; therefore separate documents for each programmer, are convenient to code and then subsequently integrate.

Dividing into multiple files
Generally, we divide the whole program into three file.
These three files are:  main.cpp (contains main function)
                                 fn.cpp (contains all functions)
                                 fn.h (contains the class with declaretion of all functions)

Note: The above program is in c++; for c, give .c extension instead of .cpp

Implementation
Let us consider an example which add two numbers in c++.
So, what we have are three files as follows.
  • main.cpp
  • mathematics.cpp
  • mathematics.h

Before, explanation of the code in above three files, follow these three steps.

1. Add main.cpp

This will be the file containing the main function as we have done previously. In all our programs we have this file because this is the point where compiler enters the code. Main.cpp will contain following code. If you do not previously have added this file to your project solution, right click on the project name or the folder named source file in the solution explorer and click on add new item. Add class from the list of items and name it main.cpp. The file will be added to solution explorer. Paste the following code in main.cpp.

Code:
#include <iostream>
#include <string>
using namespace std;
#include "Mathematics.h"
int main() {
 int num1, num2, result;
 Mathematics maths;
 cout <<"Enter the first number:";
 cin>>num1;
 cout<<"Enter the 2nd number:";
 cin>>num2;
 result = maths.add(num1, num2);
 cout <<"\nThe result of adding two numbers is: "<<result<<endl;
 result = maths.subtract(num1, num2);
 cout <<"The result of subtracting two numbers is: "<<result<<endl;
 result = maths.multiply(num1, num2);
 cout <<"The result of multipltying two numbers is: "<<result<<endl;
 result = maths.divide(num1, num2);
 cout <<"The result of dividing two numbers is: "<<result<<endl;
}

2. Add mathematics.cpp

This will be the class which contains the mathematical functionalities: Add, Subtract, Multiply and divide. Remember, in some of our previous tutorials we have defined all these functions in our main.cpp. Again, left click on the project name or the source files folder and add a class. Name this class mathematics.cpp and add following code in that file.

Code:
#include "Mathematics.h"
int Mathematics::add(int num1, int num2) {
 return Mathematics::result = num1 + num2;
}
int Mathematics::subtract(int num1, int num2) {
 return Mathematics::result = num1 - num2;
}
int Mathematics::multiply(int num1, int num2) {
 return Mathematics::result = num1 * num2;
}
int Mathematics::divide(int num1, int num2) {
 return Mathematics::result = num1 / num2;
}

3. Add mathematics.h

This is the header file we talked in some of our last sections. This file acts as a bridge between the main.cpp and mathematics.cpp. For now, just right click on Header files folder in solution explorer and add new item. This time choose header file. Name the file mathematics.h and add following code in the file.

Code:
#ifndef MATHEMATICS_H
#define MATHEMATICS_H
#include <iostream>
class Mathematics
{
 int result;
 public:
 int add(int num1,int num2);
 int subtract(int num1,int num2);
 int multiply(int num1,int num2);
 int divide(int num1,int num2);
};
#endif
Remember, function will not be defined in above file.

Compilation
$ gcc -o main.cpp mathematics.cpp maths

To run the program   $ ./maths

No comments:

Post a Comment