*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!