Different includes

This is a discussion on Different includes within the C++ Programming forums, part of the General Programming Boards category; This might seem like a question I shouldn't be asking, but I've seen different variations of #include. Sometimes you include ...

Different includes

This might seem like a question I shouldn't be asking, but I've seen different variations of #include. Sometimes you include .h and sometimes you don't. I've read recently that in C++ you don't put .h and in C you do. But in source code I look at and when I first tried a bit of C++ a few years ago (I stopped for years, I was working with Java, so I didn't keep up to date with C++), you'd have .h in #include. For example:

Code:

#include <iostream.h>
// Now it's
#include <iostream>

But when I look at some source code written pretty recently it has .h in it, but when I try to compile with .h it gives me an error. Why don't you put .h and when would be a time you do?

Also, I've seen #include written with <> or "". Usually when I see it written with quotes it's a user-created header file. Is that when you use it? And <> for built in header files? Like:

Code:

#include <iostream>
#include "myHeaderFile"

Or do they distinguish something else?

And now you have to use namespaces like

Code:

#include <iostream>
using namespace std;
// or instead of the using namespace line,
// when calling a function like cout put the namespace name
// in front like a class
std::cout<<"Text";

I never had to do that. I'm guessing C++ has changed since the last time I used it? I know about namespaces, are they new, and C++ changed like that? Or where they always in the C++ language, you just never needed to define the namespace?

the .h #includes are usually depreciated headers
or your own personal header file, all of the now standard
header file do not require the .h , alot of the old c
header file that you may still choose to use in
c++ require the .h such as <ctime.h>

and as far as i know the namespace thing has been around for
a while al lit does is let you nto have to type std::
infront of alot of stuff.

i agree

I agree with the above comment.
i do not use .h any more for iostream, the only ones I use it for is
windows.h and conio.h. The use of .h is mainly surrounding the C langugage, when it was introduced, C++ did away with all the old C jargon like that, and having to put std:: in a cout and namespace function. you can still use these features, but I personally do the other! It does make programming easyer, and reduces errors such as syntax and parse errors on compilation

Yeah seems like they basicly just revamped their code when they did away with .h's, and put code in namespaces for the benefits of doing that: having different versions of code with the same variable/function/etc. names, makes it easy to have alternate data that almost does the same thing (and imho I wouldnt use 'using namespace std' just because I like the extra clarification of std::, makes my attention easier when spotting my personal code and not the iostream code.)

What exactly is a depreciated header? Also I understand the namespaces, I just never seen them used before. I guess a few years ago when I tried C++ it was a mixed coding of C and C++ and the compiler still compiled it fine.

"Deprecated" means that something is marked as "possibly being removed in the near future. Use is highly discouraged."
It is good to note that the C++ standard never contained the .h versions of the C++ headers in the first place, so it never deprecated them. As far as the standard is concerned, they simply don't exist. However, the standard does contain and deprecate the old style C headers (stdio.h) in favour of the new style (cstdio).
Compilers that originally contained the old C++ headers have deprecated them in their own right: GCC deprecated them in the 3.x series. MS VC++ did so in version 7 (.Net 2002), and actually removed them in 7.1 (.Net 2003).

You can call your own header files with any extension. It is popular in some places to have C++ headers end with .hpp. You can include anything you like (the preprocessor simply copies and pastes the contents of the file, presuming it can find it).

usually makes the compiler look in the include directory, and if it isn't there, creates an error. Double quoted includes, one the other hand, make the compiler look in the current directory first, and then the include directory. So for built-in headers you use angle brackets, and for your own header files you use double quotes.

For angle bracket includes the compiler looks on a defined set of paths (which can be altered easily) where as quoted includes the compiler looks based on the current directory (so you can have relative paths in the header include name).