Class Definition, Implementation, and #include

In summary: When you create a program, you should create a single main file that contains all the code that makes the program work. This file should include all the data and function definitions for the program, and it should be the only file in the project. You can use the #include preprocessor directive to include other files in this file, but the linker is responsible for actually locating and combining these files into an executable program. Ah, I see now! Thanks chiro and rcgldr!
  • #1
Dschumanji
153
1
My computer science course is currently going over defining classes in C++. We do three things:

1) Create a header file that declares the class and save it as the class name appended with .h
2) Create a file that includes the header file in (1) and implements the class functions and save it as the class name appended with .cpp
3) Create a file that includes the header file in (1) and implements a main function to test the class.

My question is this: Why does the file in (3) only need to include the header file in (1) when the important things like the implementation of class functions in file (2) does not need to be included?
 
Technology news on Phys.org
  • #2
Dschumanji said:
My computer science course is currently going over defining classes in C++. We do three things:

1) Create a header file that declares the class and save it as the class name appended with .h
2) Create a file that includes the header file in (1) and implements the class functions and save it as the class name appended with .cpp
3) Create a file that includes the header file in (1) and implements a main function to test the class.

My question is this: Why does the file in (3) only need to include the header file in (1) when the important things like the implementation of class functions in file (2) does not need to be included?

Hello Dschumanji and welcome to the forums.

The reason this is the case is that the main function only needs to know the data and function definitions when it either uses a data structure (i.e. creates it, or uses it from the argument list), or calls some function that uses the structure (a function defined within the class, or some other function that uses an instance of the class).

When you write code and you need to use a class, its member functions, or something related, you need to tell the compiler the data format of the class variables and the arguments for the function.

What happens is that when you compile different objects, each object gets compiled separately from every other object. Each object needs to know the data structures for every data structure and function definition that it uses.

However, when you call a function, you do not need to know the actual function itself, you just need to know the format of the function like its arguments and the data type of the arguments.

What happens is every object is compiled independently, and then a process called a linker resolves everything and creates the final binary whether its an executable, binary library (think DLL) or some other object.

The linker has the job of making sure that the functions are actually defined in the right way and actually exist, not the compiler per se.

Also in terms of writing code, you could theoretically but every single data definition in one header file, and all of your code in one giant CPP file if you wanted to, but it's a good idea for various reasons to create different header files and object files.

You could even put your main class in the object CPP file if you wanted to as well, it wouldn't make a difference to the compiler, it's just not good form to jumble up stuff like that, especially when you have large projects.
 
  • #3
Dschumanji said:
My question is this: Why does the file in (3) only need to include the header file in (1) when the important things like the implementation of class functions in file (2) does not need to be included?
Those functions do need to be included in the program, but a program can be created from multiple source files. The compiler would compile the functions in file (2) to make one object file, and compile the main function in file (3) to make another object file. The linker would then combine these objects files into a single executable program, while filling in the address fields of the instructions in each object file used to call functions or access data in other object files. It would also include the object files from library files for any library functions you referenced in either file (2) or file (3).
 
  • #4
Ah, I think I see now! Thanks chiro and rcgldr!

I have another related question, though. Suppose I create a program with a large number of classes. To keep the project clean I decide to put my header files into one folder and all my files that implement the class functions into a separate folder. Outside of these two folders I write my main executable. I can easily use the #include preprocessor directive to indicate where my header files are to the compiler, but how do I go about telling the linker where to find the files that actually implement my class functions?
 
  • #5
That depends on your development environment.
 
  • #6
The linker doesn't need to know where the ".h" or ".cpp" files are. All it needs to know is the location of the object files that are produced by the compiler. As jtbell says, precisely how you tell the linker where to look for the files will depend on which product you are using. Most modern IDEs will handle this automatically, because the IDE tells the compiler where to put each object file and tells the linker where each object file is.
 
  • #7
Dschumanji said:
I can easily use the #include preprocessor directive to indicate where my header files are to the compiler, but how do I go about telling the linker where to find the files that actually implement my class functions?
That works, but isn't necessary. Most compilers and linkers will use environment variables that contain a list of directories when searching for files. As long as you don't have any duplicate names, there won't be an issue.

If you did want to explicitly specify locations of your object files, the command line parameters for most linkers let you specify the relative or the full path to each object or library file. The parameter may also be the path to a text file that will specify all the paths, if the parameter list is too large for the command line.
 
  • #8
Dschumanji said:
Ah, I think I see now! Thanks chiro and rcgldr!

I have another related question, though. Suppose I create a program with a large number of classes. To keep the project clean I decide to put my header files into one folder and all my files that implement the class functions into a separate folder. Outside of these two folders I write my main executable. I can easily use the #include preprocessor directive to indicate where my header files are to the compiler, but how do I go about telling the linker where to find the files that actually implement my class functions?

Usually what happens is that you have a project with lots of object files and the IDE calls the compiler to compile them and all the object files are generated in the same directory.

After this happens the linker takes in all the files, does the linking and creates your binary object.

With an IDE like Visual Studio, you just pretty much just add the file to your project and it takes care of the rest.

If you want to do it manually, you have to change your config files to 1) Compile your new source file and 2) Add it to the linker programs argument list so it knows to link it in with the other compiled object files.
 

What is a class?

A class is a blueprint or template for creating objects in object-oriented programming. It contains data members (variables) and member functions (methods) that define the behavior and attributes of the objects created from it.

What is a class definition?

A class definition is a declaration of a class, which specifies the name of the class and the data members and member functions it contains. It serves as a guide for creating objects of that class.

What is class implementation?

Class implementation is the process of writing the code for the member functions defined in the class. It involves defining the functionality of each method and how it interacts with the data members of the class.

What is the purpose of #include in a class?

The #include directive is used to include external files in a class. This allows the class to access code or data from other files, such as header files or libraries, that are needed for the proper functioning of the class.

How are classes related to object-oriented programming?

Classes are a fundamental concept in object-oriented programming, as they allow for the creation of objects with defined behavior and attributes. They promote code reusability, encapsulation, and abstraction, making it easier to manage and maintain large and complex programs.

Similar threads

  • Programming and Computer Science
Replies
2
Views
353
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
6
Views
901
  • Programming and Computer Science
Replies
2
Views
605
  • Programming and Computer Science
Replies
5
Views
806
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
Back
Top