I know they're processor commands and I read through the definitions but I still don't really understand their purpose? Is there anyone who can explain it in simpler terms :S ?
Header guards are useful to avoid having the code called multiple times in a project. Doing that makes it so if you include classHeader.h in your project, lets say 5 times, the first time it encounters it the preprocessor will see that _CLASSHEADER_H wasn't defined, then define it, load all your code, and end the ifndef call, but when it encounters your header the other 4 times, it will have the _CLASSHEADER_H defined and skip over loading the code the other four times.
There are other uses for them, but that is the most common use for them.
I'd say that these pre-processor commands are used more often for writing portable code, as opposed to include guards, which can be implemented in other ways than macros.
If I want to write portable code (code which works on other platforms), I'll need to check if certain platform specific macros are defined. Depending on which macros are defined, the compiler will be able to build a working program.
[EDIT] @xismn, you replied while I was typing this up. Yeah, I realized
that and wanted to elaborate, but I was trying to think of examples that didn't
throw more preprocessors onto it as I feared it would add to his confusion and
make it more complicated to explain. Hence the terrible examples below and
explanations.
I wanted to build on what I meant by "there are other uses for them".
This code for example is the simplest I could think of.
Let's say I was building this from command line and I had calls to print out some data while it ran so I could test something. I could pass -D (define) to the command line
g++ -o exeFile sourceFile.cpp -DDEBUG
and it will make the ifdef true so that any code I have between #ifdef DEBUG #endif will print out, but because I didn't define _DEMO_ my code there will print out. To remove _DEMO_ from running during compilation I would do
g++ -o exeFile sourceFile.cpp -D_DEMO_
making _DEMO_ now defined and will skip over it.
The advantages of this is you can put multiple of them in your code.