Why are included files passed down

This is a very basic thing that I am just wondering about. When I #inlcude "string.h" in my header file for a class, why can I also use strings in my .cpp file for the class. Why/How are included files (i think they are files?) passed down to the .cpp file for the class?

Employee header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <iostream>
#include "string.h"

using namespace std;

class Employee
{
    public:
        Employee(string empName, int id, string dept, string pos);
    protected:
    private:
        string name;
        int idNumber;
        string department;
        string position;
};

#endif // EMPLOYEE_H 


Employee .cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Employee.h"

//#include "string.h"

using namespace std;

Employee::Employee(string empName, int id, string dept, string pos)
{
    name = empName;
    idNumber = id;
    department = dept;
    position = pos;
}


Also if I use ANY incorrect terminology, pls let me know. TY for any help!
Because you're including your header file, which in turn includes the string header.

If you put a coin in your wallet then put your wallet in a rucksack then the coin is still in the rucksack. :-)
Preprocessor reads files recursively, i.e. it does not only process the #include directives of Employee.cpp, but of every included files as well.
and #include "string.h" should be #include <string>
The #include directive has the effect of reading in the included file and replacing the #include directive with the contents of that file.

So, after preprocessing,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <iostream>
#include "string.h"

using namespace std; // *** this is bad

class Employee
{
    public:
        Employee(string empName, int id, string dept, string pos);
    protected:
    private:
        string name;
        int idNumber;
        string department;
        string position;
};

#endif // EMPLOYEE_H  


would look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
///// contents of the file iostream are preprocessed and inserted here ////////////////

///// contents of file string.h are preprocessed and inserted here ////////////////

using namespace std;

class Employee
{
    public:
        Employee(string empName, int id, string dept, string pos);
    protected:
    private:
        string name;
        int idNumber;
        string department;
        string position;
};


If we now have #include "Employee.h" , the effect is as if the directive was replaced by the preprocessed contents of employee.h
(Into which the preprocessed contents of "string.h" were already inserted.)
Last edited on
Thanks for the replies, that makes sense, I am used to having to copy over import or using statements in Java or C#, so this is something I will have to get used to.
There is nothing like include in Java. import in Java is like using in C++. Java "includes" the needed class files automatically via the compiler.

1
2
3
4
5
6
// Java

java.io.BufferedReader br = new java.io.BufferedReader(...);
// --- vs. ---
import java.io.BufferedReader;
BufferedReadder br = new BufferedReader(...);


1
2
3
4
5
6
//C++

std::fstream f(...);
// --- vs. ---
using std::fstream;
fstream f(...);
Last edited on
Topic archived. No new replies allowed.