Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Class Definition, Implementation, and #include

  1. Oct 5, 2011 #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?
     
  2. jcsd
  3. Oct 6, 2011 #2

    chiro

    User Avatar
    Science Advisor

    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.
     
  4. Oct 6, 2011 #3

    rcgldr

    User Avatar
    Homework Helper

    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).
     
  5. Oct 6, 2011 #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?
     
  6. Oct 6, 2011 #5

    jtbell

    User Avatar

    Staff: Mentor

    That depends on your development environment.
     
  7. Oct 6, 2011 #6

    DrGreg

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  8. Oct 6, 2011 #7

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  9. Oct 6, 2011 #8

    chiro

    User Avatar
    Science Advisor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Class Definition, Implementation, and #include
  1. Program Implementation (Replies: 21)

Loading...