C++ include files
Project files inC++
In software projects, not all the code, which may consist of millions of lines of code, is placed in one file because developing the code in the future or debugging the errors in it will be very difficult. Therefore, the code is written in an orderly manner and divided into a group of files, and then it becomes very easy to modify or develop it at any time, in addition to the possibility for each programmer in the project to work on developing part of it.
Previously, we used to write all the project code in one file with the aim of making you focus on the new concepts that we were explaining in each lesson.
In this lesson, you will learn how to distribute the project code to several files and then how to deal with them as if they are all in the same file.
How to include a file code in a file
If you want to include the code in one extension .cpp
file in another extension .cpp
, there are two ways to do that:
Include everything you want to use from the file separately and this method is not usually followed because it is tedious.
Include all the content of the file at once and use what you want from it directly and this is the best method for that.
You will learn later from the examples how to apply both methods, but remember that the second method is the best for that because it saves you a lot of time and repetition.
A note about embedding files in C++
We will explain how to create files with a programCodeBlocksBecause it is the program that we use in this course, but you have to know that the logic of creating files and including their content is exactly the same in any other program.
Include only what is needed
In the examples we've put in this lesson, you'll notice that we haven't written the command using namespace std;
like we used to, and that's because we'll start by trying to include only what we need to include in the code.
So when you find that we wrote std::cout
and std::endl
we use this cout
, endl
but without including everything that is in the librarystd
.
Specify the code to be included from the file inC++
In case you want to include a variable, function, class etc.. that is in one file inside another file, you must re-state the definition(Declaration)That thing in the file you intend to use it in because in this way the compiler knows that what you are using is something in the project, and therefore when running it searches for the place where this thing was defined.
In the following example, we created a new file in the project nameddemo.cpp
.
In case you don't know how to create the file yourself, follow the steps laid out here to create it.
How to create a cpp file in CodeBlocks
You should strictly follow these steps any time you want to create a .cpp
new file in your project.
In the following example we will create a new file named demo.cpp
in the project.
File creation steps
Click on the Create New File icon.
Click on the button
File...
as follows.
Click once on an icon
C/C++ Source
to select it.Click on the button
Go
as follows.
Click on the button
Next
as follows.
Click on the option
C++
to select the type of file you want to create.Click on the button
Next
as follows.
Click on the button
...
to specify where you want to create the file and the name of the file you want to create as follows.
Type the name you want to put for the file, and in our case, write
demo
until a file nameddemo.cpp
next to the file is createdmain.cpp
.Click on the button
save
as follows.
Click the button
All
until both optionsDebug
and . are checkedRelease
.Click on the button
Finish
as follows.
Here, you have
demo.cpp
successfully created the file, and you can return to the lesson to start working with this file.
The following image shows the general shape of the project folders and files.
Now we will define three simple functions in the filedemo.cpp
.
Then we will include these functions and call them in the filemain.cpp
.
Example
#include <iostream> // Here we have defined a function that prints a hello statement when called void greetings() { std::cout << "Welcome to harmash.com" << std::endl; } // Here we have defined a function that prints the numbers from 1 to the number we pass to it when called void printFromOneTo(int n) { for (int i=1; i<=n; i++) { std::cout << i << " "; } std::cout << std::endl; } // Here we have defined a function that we pass three numbers to when called, and it returns the product of their sum int sum(int a, int b, int c) { return a + b + c; }
#include <iostream> // so that they are included in this file, so we can call them demo.cpp for the three functions in the file (Header) here we mentioned the general layout void greetings(); void printFromOneTo(int n); int sum(int a, int b, int c); int main() { // greetings() here we called the function greetings(); // passing the value 10 to it so it prints all the numbers from 1 to 10 printFromOneTo() here we called the function printFromOneTo(10); // Passing three numbers to it and printing the sum that will return us sum() Here we called the function std::cout << "1 + 2 + 3 = " << sum(1, 2, 3); return 0; }
• We will get the following result when running.
Welcome to harmash.com 1 2 3 4 5 6 7 8 9 10 1 + 2 + 3 = 6
In the previous example, we assumed that the file whose content we intend to include has only three functions.
Now imagine if the file whose content we intend to include contains a lot of functions and we want to use 20 of them using the same method as before. Then it will be tiring and the code size will become larger since you will then have to re-state what each function you will use of them looks like in the file you included.
Include all the code in the file at once inC++
Usually, to make all the code in an extension file .cpp
can be included at once, a parallel file is created with the same name and extension .h
, and in it we just mention the general form(Declarations)It only contains the first file.
Then any file that wants to include all the code in the file whose extension .cpp
it includes .h
only the file parallel to it.
technical terms
Any file with an extension
.cpp
is called a source file(Source File)This means that it contains the same code.Any file with an extension
.h
is called a base file(Header File)This means that it contains only the general or basic form of the code.
The general shape of any base file(Header File)
A file with the extension .h
has two methods to define it: #ifndef
Old style or #pragma
Modern style.
The only difference between them is that style #ifndef
requires writing three lines, while style #pragma once
requires writing only one line.
The general format of the file extension is . .h
style#ifndef
.
#ifndef FILE_NAME // puts any name you haven't used before FILE_NAME in the place of the word #define FILE_NAME // put any name you haven't used before FILE_NAME in the place of the word // which you want to include in another cpp file, here you put the general appearance of whatever is in the file #endif
The general format of the file extension is . .h
style#pragma once
.
#pragma once // which you want to include in another cpp file, here you put the general appearance of whatever is in the file
Following one of these two methods when defining an extension file .h
is very important because it ensures that the same code is not included more than once in the same place.
Later we will try to include the same code in the same place more than once without adopting any method from them until you know how this may cause an error in the code that prevents it from running.
note
When creating an extension file .h
with a programCodeBlocksWe note that it adopts the old method, #ifndef
as you will find it automatically writes the three lines of this method for you as soon as the file is created. Of course you can erase it and write #pragma once
it yourself if you want or keep the style.
In the following example, we created a new file in the project named demo.h
and put in it the general shape of the contents of the file demo.cpp
that we created earlier.
In case you don't know how to create the file yourself, follow the steps laid out here to create it.
How to create h file in CodeBlocks
You should strictly follow these steps any time you want to create a .h
new file in your project.
In the following example we will create a new file named demo.h
in the project.
File creation steps
Click on the Create New File icon.
Click on the button
File...
as follows.
Click once on an icon
C/C++ Header
to select it.Click on the button
Go
as follows.
Click on the button
Next
as follows.
Click on the button
...
to specify where you want to create the file and the name of the file you want to create as follows.
Type the name you want to put for the file, and in our case, write
demo
until a file name is createddemo.h
next to the two filesdemo.cpp
andmain.cpp
.Click on the button
save
as follows.
Click the button
All
until both optionsDebug
and . are checkedRelease
.Click on the button
Finish
as follows.
Here, you have
demo.h
successfully created the file, and you can return to the lesson to start working with this file.
The following image shows the general shape of the project folders and files.
Now we will define the shape of the functions in the file demo.cpp
in the filedemo.h
.
Then we will include the file demo.h
in the file main.cpp
and then call the functions in the file demo.cpp
just as we did in the previous example.
Example
#include <iostream> // Here we have defined a function that prints a hello statement when called void greetings() { std::cout << "Welcome to harmash.com" << std::endl; } // Here we have defined a function that prints the numbers from 1 to the number we pass to it when called void printFromOneTo(int n) { for (int i=1; i<=n; i++) { std::cout << i << " "; } std::cout << std::endl; } // Here we have defined a function that we pass three numbers to when called, and it returns the product of their sum int sum(int a, int b, int c) { return a + b + c; }
#pragma once // just include all these functions demo.h, which will make the demo.cpp file include the three functions in the file (Header) here we mentioned the general layout void greetings(); void printFromOneTo(int n); int sum(int a, int b, int c);
#include <iostream> #include "demo.h" // so we can use anything we define in demo.h here we have included the file content int main() { // greetings() here we called the function greetings(); // passing the value 10 to it so it prints all the numbers from 1 to 10 printFromOneTo() here we called the function printFromOneTo(10); // Passing three numbers to it and printing the sum that will return us sum() Here we called the function std::cout << "1 + 2 + 3 = " << sum(1, 2, 3); return 0; }
• We will get the following result when running.
Welcome to harmash.com 1 2 3 4 5 6 7 8 9 10 1 + 2 + 3 = 6
The easiest way to write code
It is always better to start by stating the general form of the things you will define inside the extension file .h
and then include the file .h
inside the file .cpp
because this method will make the program you use to write the code to help you complete the code.
Now we will repeat the previous example, but we will start writing the code in the demo.h
next file without making any modification to the previous functions. Note: We will include in the file to make the compiler recognize the functions in it and thus to be able to help us complete the code.demo.cpp
main.cpp
demo.h
demo.cpp
Example
#pragma once // demo.cpp For the three functions that we will create in the file (Header) here we have mentioned the layout void greetings(); void printFromOneTo(int n); int sum(int a, int b, int c);
#include <iostream> #include "demo.h" // So the compiler is able to help us complete demo.h here we have included the content of the file // You will notice that the program you are using to code will help you to write the general form of greetings() while defining the function void greetings() { std::cout << "Welcome to harmash.com" << std::endl; } // You will notice that the program you are using to write the code will help you to write its general form printFromOneTo() while defining the function void printFromOneTo(int n) { for (int i=1; i<=n; i++) { std::cout << i << " "; } std::cout << std::endl; } // You will notice that the program you are using to write the code will help you to write its general form sum() while defining the function int sum(int a, int b, int c) { return a + b + c; }
#include <iostream> #include "demo.h" // so we can use anything we define in demo.h here we have included the file content int main() { // The compiler will help you write its name "demo.h" when calling any function in the file greetings(); printFromOneTo(10); std::cout << "1 + 2 + 3 = " << sum(1, 2, 3); return 0; }
• We will get the following result when running.
Welcome to harmash.com 1 2 3 4 5 6 7 8 9 10 1 + 2 + 3 = 6
The difference between #include " ... "
and #include < ... >
inC++
Extension files that .h
you find ready inC++Or you tell the compiler yourself where to find it - and this is something you'll learn in the next lesson - we include them in style #include < ... >
and here are some examples.
#include <iostream> #include <vector> #include <queue>
Files with the extension .h
that you or any other programmer created and the translator was not informed of their whereabouts, we include them in a way #include " ... "
because it allows us to determine the whereabouts of these files ourselves. Here are some examples.
// It is located directly inside the project and is not placed inside any student.h folder. This means that this file #include "student.h" // school is in the project in a folder called student.h This means that this file #include "school/student.h" // D://libraries/school is in student.h This means that this file #include "D://libraries/school/student.h"
How to delete files from the project inCodeBlocks
If you want to delete the two files demo.cpp
we demo.h
created in the previous examples, follow the steps below.
Click on the name of the project you want to delete or more.
Click on the button
Remove Files...
as follows.
Select the files you want to delete and in our case we will delete the two files
demo.cpp
anddemo.h
.Click on the button
OK
as follows.
Click on the button
Yes
as follows.
How to create a file .cpp
and a file.h
its file at once inCodeBlocks
In general, in projects, each class is created within a .cpp
special file and a file is created .h
for it so that its content is easily included within it.
If you want to create a new class with the two files created at once, follow the steps below .cpp
..h
Click on the Create New File icon.
Click on the button
Class...
as follows.
Put the name of the class you want to create in place of the text box
Class name:
to select it.Click on the button
Create
as follows.
Click on the button
Yes
as follows.
Click the button
Select All
until both optionsDebug
and . are checkedRelease
.Click on the button
OK
as follows.
If you follow the same steps that we followed, the file will be created
Person.cpp
in a folder with its namesrc
and the filePerson.h
in a folder namedinclude
as follows.
The code that you find ready in any file that is created in the project, you can delete it or modify it as you want.
You can even, as you saw before in the third step, specify the code that will be prepared for you in these files.