C++ Custom Header Files (Moved)
Results 1 to 6 of 6

Thread: C++ Custom Header Files (Moved)

  1. #1
    Senior Member
    Join Date
    Nov 2003
    Posts
    247

    C++ Custom Header Files (Moved)

    *Note: Moved under my own volition. Go my volition.*

    Custom Header Files in C++

    Everything that is in this article will probably work/apply to C and C#, although I cannot be sure about this

    One of the most valuable things that can be done or used in any programming language is of course the code library, which saves people from having to re-write code again and again, an invaluable tool for anyone who aspires to write good code with any depth or speed.

    In C++ code libraries are implemented in two main ways:
    1.) Header files
    2.) Copying and pasting code

    Header files are often the easiest and most universal, having speed and simplicity on their side. Copying and pasting code will make it harder for people to look at and read, but is good in very specific instances.

    To use a header file in C++, you must very simply insert, at the beginning of a program:
    #include <headername>

    In some older compilers you may have to include the .h (for header) after the header name:
    #include <headername.h>

    You should be able to tell fairly quickly from the compiler results or the compiler's documentation. Modern GCC compiler's should allow you to drop off the .h

    Some of the most commonly used and useful header files in C++ are:
    iostream -- Data to and from keyboard
    ifstream -- Data to and from files
    cmath -- Advanced math functions, i.e. square-roots
    string -- Allows the string (text) variable
    etc...

    All well and good, right? Well, one of the great things about modern programming is that it is very easy to create new header files for yourself and anyone else you care to share them with.

    You would do this if you had a function that you used in a lot of programs, specific variable types and names, sequences of data, or even a routine help sequence throughout your program.

    So how do you create these wondrous little diddies? Quite simply, you would go ahead and make them in the same format that you would a regular C++ file, using the same syntax and the like.

    You can, however, leave out the basic header file and int main() that you'll often see or make at the beginning of any program. If you want to though, you can include them, which can save you several other statements.

    Here's an example of a decent, random header file:
    #include <string>
    #include <iostream>

    void sodainfo(int sodanum, string sodaname, bool sodabuy);

    int sodanum;
    string sodaname;
    bool sodabuy;


    void sodainfo(int sodanum, string sodaname, bool sodabuy)
    {
    cout << "The type of soda is " << sodaname << "." << endl;
    cout << "There are " << sodanum << " sodas to be bought." << endl;
    cout << "Would you like to buy a soda?" << endl;
    cin >> sodabuy;
    }

    Obviously this is a very singular function, with only one real use. But I think you'll get the point.

    You may be saying to yourself: That's just like a regular C++ program. Well, it is. Its written exactly the same, but it can save you immense amounts of time in the right circumstances.

    The only real difference is that you save it in the library directory (for the compiler) in the format "headername.h"

    Nice, simple, easy, and effective.

    Happy coding!
    www.ADigitalPimp.com
    There is a ghost in the machine, and he is my friend.

  2. #2
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    A good introduction to header files in C++, thanks.

    Just one small note (and this is being a bit pedantic), if you use #include<iostream> instead of #include <iostream.h>, you have to use the standard namespaces (or add 'using namespace std' to the top of your code). So cout would become std::cout etc.
    Paul Waring - Web site design and development.

  3. #3
    Senior Member
    Join Date
    Nov 2003
    Posts
    247
    I don't believe that's so. You could very easily put using namespace std; inside the main program. Calling the function inside of that, the cout should act normally.
    www.ADigitalPimp.com
    There is a ghost in the machine, and he is my friend.

  4. #4
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    Perhaps, but then the creator of the original function is relying on the end user including the line 'using namespace std' in their program. Now, this might not seem important if you write the functions yourself and don't intend to share them with anyone else, but if I were to include the header file you mentioned in your post for example, it wouldn't compile because I don't use 'using namespace std' in my programs.
    Paul Waring - Web site design and development.

  5. #5
    Senior Member
    Join Date
    May 2002
    Posts
    344
    Just one small note (and this is being a bit pedantic), if you use #include<iostream> instead of #include <iostream.h>, you have to use the standard namespaces (or add 'using namespace std' to the top of your code). So cout would become std::cout etc.
    pwarning is right...i told you the same thing in the thread you had previously deleted.
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  6. #6
    Senior Member
    Join Date
    Nov 2003
    Posts
    247
    Point.
    www.ADigitalPimp.com
    There is a ghost in the machine, and he is my friend.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides