Question

In: Computer Science

Please provide step by step on how to create the header files for this project. I...

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:

  1. Generate random 100 service requests.
  2. Each service request can be either high, medium, or low priority. (Your program should randomly allocate this priority to each service.)
  3. Each service request may need any time to be serviced between 5 and 8 minutes. (Your program should randomly allocate this time to each service.)
  4. Your program should simulate the case when you have one service station for all customers.
  5. Your program should simulate the case when you have two service stations for these 100 customers.
  6. For each case, output the following statistics:
    1. The number of requests for each priority along with the service time for each request
    2. The waiting time for each service request
    3. The average waiting time for service requests within each priority

Please provide entire code and screenshot of output. Thank you!

Solutions

Expert Solution

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:

  1. A header guard, which we’ll discuss in more detail in the next lesson (2.12 -- Header guards).
  2. The actual content of the header file, which should be the forward declarations for all of the identifiers we want other files to be able to see.

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.


Related Solutions

Please provide step by step detailing on how i should do this assignment. In Angel, you...
Please provide step by step detailing on how i should do this assignment. In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary...
How do you use header files on a program? I need to separate my program into...
How do you use header files on a program? I need to separate my program into header files/need to use header files for this program.Their needs to be 2-3 files one of which is the menu. thanks! #include #include #include using namespace std; const int maxrecs = 5; struct Teletype { string name; string phoneNo; Teletype *nextaddr; }; void display(Teletype *); void populate(Teletype *); void modify(Teletype *head, string name); void insertAtMid(Teletype *, string, string); void deleteAtMid(Teletype *, string); int find(Teletype...
Please provide a step by step solution create a Vacation Budget worksheet Add at least 6...
Please provide a step by step solution create a Vacation Budget worksheet Add at least 6 other expenses related to your planned vacation (car rental etc.) enter an estimate of the cost of for each item Use a function to calculate the total estimated costs. g) Enter a function to calculate the average cost per day. h) Create a chart (you choose the type) in this same worksheet based on this trip to display the estimated costs. Add a title...
Please provide a step by step solution. Quad Enterprises is considering a new three-year expansion project...
Please provide a step by step solution. Quad Enterprises is considering a new three-year expansion project that requires an initial fixed asset investment of $3.09 million. The fixed asset will be depreciated straight-line to zero over its three-year tax life, after which time it will be worthless. The project is estimated to generate $2132664 in annual sales, with costs of $805848. If the tax rate is 34 percent and the required return on the project is 11 percent, what is...
Please provide step by step solution and i will give a positive rating. Many thanks 1)Suppose...
Please provide step by step solution and i will give a positive rating. Many thanks 1)Suppose we have a steady fluid flow through a section of a cylindrical pipe with varying area, with control volume.  At the entrance the radius of the pipe r1 = 4m, the flow velocity is U1 = 5 i m/s and at the exit the radius of the pipe is r2 = 2.5 m and the velocity of the flow is U2 = 5 i m/s....
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class Definition ShoppingCartPrinter.java – Contains main() method Build the ItemToPurchase class with the following specifications: Specifications Description ItemToPurchase(itemName) itemName – The name will be a String datatype and Initialized in default constructor to “none”. ItemToPurchase(itemPrice) itemPrice – The price will be integer datatype and Initialized in default constructor to 0. ItemToPurchase(itemQuantity) itemQuantity – The quantity will be integer datatype Initialized in default constructor to 0....
I don't know Fishers Exact test.. Please provide me Concept , Step wise Step wise and...
I don't know Fishers Exact test.. Please provide me Concept , Step wise Step wise and a suitable example... Will upvote 100%
And please show me the step by step process of how you solved the question. I...
And please show me the step by step process of how you solved the question. I have a learning disability and I need to know the steps to solve the question If it takes 17.5 mL of 0.085 M NaOH to titrate a 15 mL sample of sauerkraut juice, what is the acidity of that juice, expressed as % lactic acid (wt/vol)?
How can I solve this problem? Can you please show step by step how to solve...
How can I solve this problem? Can you please show step by step how to solve this? Qd = 2,000 − 10P MC = 0.1Q
(Typed work preferable. Show step-by-step please! Answer fully.) I used SPSS to create a bivariate regression...
(Typed work preferable. Show step-by-step please! Answer fully.) I used SPSS to create a bivariate regression equation where “MaleLifeExp” is the dependent variable and “Literacy” is the independent variable. The variable “Literacy” measures the percentage of people in each country who are able to read. Model Summary Model R R Square Adjusted R Square Std. Error of the Estimate 1 .680 a 0.463 0.458 6.592 a. Predictors: (Constant), Literacy: Percentage of Adults Who Can Read Coefficients a Model Understated Coefficients...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT