In: Computer Science
Please provide step by step on how to create the header files for this project. I received code for it but I am getting an error and I know it has to do with my header files. Please provide instructions on how to create these files along with the full code in C++ for the following:
Write a C++ program to simulate a service desk. This service desk should be able to service customers that can have one of three different priorities (high, medium, and low). The duration for any customer is a random number (between 5 minutes and 8 minutes). You need to write a program that will do the following:
Please provide entire code and screenshot of output. Thank you!
As programs grow larger (and make use of more files), it becomes increasingly tedious to have to forward declare every function you want to use that is defined in a different file. Wouldn’t it be nice if you could put all your forward declarations in one place and then import them when you need them?
C++ code files (with a .cpp extension) are not the only files commonly seen in C++ programs. The other type of file is called a header file. Header files usually have a .h extension, but you will occasionally see them with a .hpp extension or no extension at all. The primary purpose of a header file is to propagate declarations to code files.
Key insight
Header files allow us to put declarations in one location and then import them wherever we need them. This can save a lot of typing in multi-file programs.
Using standard library header files
Consider the following program:
1 2 3 4 5 6 7 |
#include <iostream> int main() { std::cout << "Hello, world!"; return 0; } |
This program prints “Hello, world!” to the console using std::cout. However, this program never provided a definition or declaration for std::cout, so how does the compiler know what std::cout is?
The answer is that std::cout has been forward declared
in the “iostream” header file. When we #include
<iostream>
, we’re requesting that the preprocessor
copy all of the content (including forward declarations for
std::cout) from the file named “iostream” into the file doing the
#include.
Key insight
When you #include a file, the content of the included file is inserted at the point of inclusion. This provides a useful way to pull in declarations from another file.
Consider what would happen if the iostream header did
not exist. Wherever you used std::cout, you would have to
manually type or copy in all of the declarations related to
std::cout into the top of each file that used
std::cout! This would require a lot of knowledge about how
std::cout was implemented, and would be a ton of work.
Even worse, if a function prototype changed, we’d have to go
manually update all of the forward declarations. It’s much easier
to just #include iostream
!
When it comes to functions and variables, it’s worth keeping in mind that header files typically only contain function and variable declarations, not function and variable definitions (otherwise a violation of the one definition rule could result). std::cout is forward declared in the iostream header, but defined as part of the C++ standard library, which is automatically linked into your program during the linker phase.
Best practice
Header files should generally not contain function and variable definitions, so as not to violate the one definition rule. An exception is made for symbolic constants (which we cover in lesson 4.13 -- Const, constexpr, and symbolic constants).
Writing your own header files
Now let’s go back to the example we were discussing in a previous lesson. When we left off, we had two files, add.cpp and main.cpp, that looked like this:
add.cpp:
1 2 3 4 |
int add(int x, int y) { return x + y; } |
main.cpp:
1 2 3 4 5 6 7 8 9 |
#include <iostream> int add(int x, int y); // forward declaration using function prototype int main() { std::cout << "The sum of 3 and 4 is " << add(3, 4) << '\n'; return 0; } |
(If you’re recreating this example from scratch, don’t forget to add add.cpp to your project so it gets compiled in).
In this example, we used a forward declaration so that the compiler will know what identifier add is when compiling main.cpp. As previously mentioned, manually adding forward declarations for every function you want to use that lives in another file can get tedious quickly.
Let’s write a header file to relieve us of this burden. Writing a header file is surprisingly easy, as header files only consist of two parts:
Adding a header file to a project works analogously to adding a source file (covered in lesson 2.8 -- Programs with multiple code files). If using an IDE, go through the same steps and choose “Header” instead of “Source” when asked. If using the command line, just create a new file in your favorite editor.
Best practice
Use a .h suffix when naming your header files.
Header files are often paired with code files, with the header file providing forward declarations for the corresponding code file. Since our header file will contain a forward declaration for functions defined in add.cpp, we’ll call our new header file add.h.
Best practice
If a header file is paired with a code file (e.g. add.h with add.cpp), they should both have the same base name (add).
Here’s our completed header file:
add.h:
1 2 3 4 |
// 1) We really should have a header guard here, but will omit it for simplicity (we'll cover header guards in the next lesson) // 2) This is the content of the .h file, which is where the declarations go int add(int x, int y); // function prototype for add.h -- don't forget the semicolon! |
In order to use this header file in main.cpp, we have to #include it (using quotes, not angle brackets).
main.cpp:
1 2 3 4 5 6 7 8 |
#include <iostream> #include "add.h" // Insert contents of add.h at this point. Note use of double quotes here. int main() { std::cout << "The sum of 3 and 4 is " << add(3, 4) << '\n'; return 0; } |
add.cpp:
1 2 3 4 |
int add(int x, int y) { return x + y; } |
When the preprocessor processes the #include
"add.h"
line, it copies the contents of add.h into the
current file at that point. Because our add.h contains a
forward declaration for function add, that forward
declaration will be copied into main.cpp. The end result
is a program that is functionally the same as the one where we
manually added the forward declaration at the top of
main.cpp.
Consequently, our program will compile and link correctly.