Thread: Header file exercise questions

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Gunnison, Colorado, United States
    Posts
    10

    Header file exercise questions

    Okay, so I'm teaching myself C++, and I like to run with as many exercises as I can (I've found that nothing teaches better than actually coding it out, and conceptual knowledge helps much less than actual logic). In this book, despite being able to decipher and program the exercises I've done so far, I have an exercise that reads as follows: "Create a header file (with an extension of '.h'). In this file, declare a group of functions by varying the argument lists and return values from among the following: void, char, int, and float. Now create a .cpp file that includes your header file and creates definitions for all of these functions. Each definition should simply print out the function name, argument list, and return type so you know it's been called. Create a second .cpp file that includes your header file and defines int main(), containing calls to all of your functions. Compile and run your program."
    Now I have no idea what this is asking, and I'm particularly confused on how to create header files. Calling them is something I can obviously do by this time, but creating them I'm not sure how to do, and I don't know what he means by "declare a group of functions by varying the argument lists." I'm not asking for help on the program, just... I guess a cipher from programmer-speak to relative newbie speak. Can anyone help me out? If this violates the "do your own homework" policy, I'll just try to muddle it out, but I'd really like some help.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    A header file is included by the source code in order to not have a big bulk of code all in one file. You can create a header like any other file. You'll want to add a *.h, *.hpp or *.hh extension to it( though you don't need any particular extension as far as the compiler's concerned )
    About the second one, it says that you should only declare/prototype your functions in the header, and then you'll define them in the source file.
    Function prototype:
    Code:
    int foo(int, float, whatever);
    Function definition:
    Code:
    int foo(int a, float b, whatever c)
    {
        ....
    }
    That's the "formal" way, but my opinion is to always include parameter names, even in the prototype.
    Devoted my life to programming...

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by GReaper View Post
    That's the "formal" way, but my opinion is to always include parameter names, even in the prototype.
    I would go as far as to strongly recommend including the parameter names.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    The exercise is trying to teach you how to start using multiple source files and headers, this then makes your work change from everything being lumped into one large file with lots of code lines, to a 'project' where code is spread out amongst files and resources and as such makes for a much better way of managing your work, for example all of your error output can be moved into a file like 'error.cpp', all of your menu work in 'menu.cpp' etc etc.

    You always need a source file with 'int main()' in it, but when you start using multiple source files you will find that contrary to what you might be writing now your 'main()' file may only have a few lines in it, the rest of the action is diverted out to the supporting source files once you build your objects, start your algorithms or whatever.

    In the exercise you are basically required to add one source file and one header to a project.
    Often there will be one header and one source for each category, so you get pairs, like 'menu.cpp' goes with menu.h' etc.

    so in the header simply write the function prototype for now, with a comment to say what it does.
    like:
    Code:
    void ShowOutput();  //prints details of this function
    Then in the extra source file mysource.cpp, include your header (like when you write include<iostream>) dont use the <> though, use "myheader.h" for your own stuff.
    And in this file write the body of the function so that it prints out the details required.

    then in the normal source file containing main() also include myheader.h
    and call 'ShowOutput()'

    For further info a header file is included in the 'pre-processing' of the build.
    Thus the contents of a header let the compiler know 'ahead' of time about e.g functionality and datatypes that may be used in the actual source files.

    Regarding the recommendation to use parameter names in function declarations:

    a prototype like this:
    Code:
    void DrawLine(int, int, int, int); //draws a line
    is absolute crapola.
    It is left to the programmer that picks this up (and you..) to infer what the four ints may mean.

    much better is somethng like :
    Code:
    void DrawLine(int xStart, int yStart, int xEnd, int yEnd); //draws a line from the  x, y start positions to the x, y end positions
    And in your source file you may well call:
    Code:
    DrawLine(currXstart, currYstart, currXend, currYend );
    the reader of this file understands that the present relative coordinates are being passed away to the function.

    the reader of the prototype does not need to see the arguments as 'currentX' or whatever, they just see that the function operates on four well described parameters that make sense in relation to the function name and work it does.
    Last edited by rogster001; 02-19-2012 at 07:43 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Feb 2012
    Location
    Gunnison, Colorado, United States
    Posts
    10
    Wow! Thanks a lot, guys. I wasn't sure what to expect of this forum, but that really clarifies things perfectly! I'm sure I'll have more questions for you guys as time goes on.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Location
    Gunnison, Colorado, United States
    Posts
    10
    Does one have to put normal #include stuff into a header, or is it read directly into the main program?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What do you mean?
    Remember that the preprocessor just takes the contents of the file pointed to by the #include directive and replaces that line with the contents of the file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Dagda View Post
    Does one have to put normal #include stuff into a header, or is it read directly into the main program?
    If you use anything in the header that requires a standard include, you should put it.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    If you use anything in the header that requires a standard include, you should put it.
    However, if you use something in the source file that implements what is declared in the header, but which is not itself used in the header, then only #include those required headers in the source file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by laserlight View Post
    However, if you use something in the source file that implements what is declared in the header, but which is not itself used in the header, then only #include those required headers in the source file.
    Though I myself had always followed it, how much sense does it actually make, when the different translation units should ideally be compiled separately and linked together ?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    Though I myself had always followed it, how much sense does it actually make, when the different translation units should ideally be compiled separately and linked together ?
    Well, especially if you are not using pre-compiled headers, doing this means that only translation units that need those declarations and type definitions need to be parsed for them. Consequently, impact of a change to the header is reduced: only those source files that include the header need to be re-compiled.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  2. Including header file with in the header file
    By Arafat_211184 in forum C Programming
    Replies: 13
    Last Post: 12-19-2005, 10:03 AM
  3. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  4. Some really quick questions on header files
    By quentin in forum C++ Programming
    Replies: 8
    Last Post: 04-27-2002, 09:50 AM
  5. questions about djgpp and header-files
    By dune911 in forum C Programming
    Replies: 8
    Last Post: 10-20-2001, 11:31 AM