![]() |
|
#1
|
|||
|
|||
Which Header Files to Use??I'm using Visual Studio 6 and I am preplexed over which header files to use: stdio or stdio.h, iostream or iostream.h, cstdlib or stdlib.h, etc..
I've read that .h files should be avoided, but iostrem (no .h) does not support cout on my computer (while iostream.h does). CPP / C++ / C Code:
I've also read that different header types shouldn't be mixed in the same code. So the following is not a good idea: CPP / C++ / C Code:
A little direction on this would be much appreciated. THANKS! |
|
#2
|
|||
|
|||
|
just add this line:
using namespace std; after all the headers, don't put .h in the end of the file name and you won't have problems |
|
#3
|
||||
|
||||
|
Quote:
C++ headers you should use the non-h version as mentioned. If you are writing C, there are no non-h headers. __________________
Age is unimportant -- except in cheese |
|
#4
|
|||
|
|||
|
can somebody explain why arent .h headers recommended in c++?? and what is the difference between using them or using its non .h version??
also what does the statement using namespace std; do?? |
|
#5
|
||||
|
||||
|
Okay, so first you need to understand what namespaces are. A namespace is basically what the word says... it's a bunch of space with a name. The trick is that the space usually has some code in it. Now, I'm sure you know about scope and variable lifetime. If so, you know that when you enter a function, it is said to be in scope, meaning... you can see the contents of the function, but you can't see anything else. When you exit the function, it's said to be out of scope, and wherever you're taken is then in scope. So, you can no longer see what's in the function, and you can't access its variables. With namespaces, you're basically taking all the code in a defined space, and giving the space a name so that it can't be accessed unless it's in scope. This is where the using directive comes in. The using directive allows you to bring the contents of a namespace into scope so that you can use it. Here's an example.
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
__________________
-Aaron |
|
#6
|
|||
|
|||
|
Thank you aaroncohn, what a good description, i didnt know about namespaces and the scope operator thanks for explaining them.
So i would guess using namespace std; would mean to put in global scope all the variables and functions in the std namespace of one of the header files wouldnt it?? My question now is.. which header files?? and what are the differences between .h headers and non .h headers?? and why arent recommended the first ones??? |
|
#7
|
||||
|
||||
|
Many standard C++ header files use the std namespace. That way you don't have to remember 50 different namespace identifiers! We don't use the .h extension for C++. C does not support namespaces, and it also does not support leaving out the .h extension. I guess the missing .h extension dignifies a file as C++ compliant and therefore supporting namespaces. Someone please correct me if I'm wrong. Occasionally, though, you will run into a header that has no C++ counterpart and will have to make the unsavory mixing of the .h and non-.h headers. I did this in my recent hangman game. There's no <time>, so I had to use <time.h> even though the program is almost strictly C++.
__________________
-Aaron |
|
#8
|
||||
|
||||
|
Quote:
Actually, C supports any file extension as a header, but only .h files come with a C compiler. You can still create your own header with or without an extension. For C++ headers, the old-style .h headers also don't support namespaces, but the extensionless headers do -- which is the current standard. I don't have a clue how they work, but that's what I've noticed. Quote:
__________________
Age is unimportant -- except in cheese |
|
#9
|
|||
|
|||
|
So the main difference between .h headers and non .h headers are that the first ones doesnt support namespaces isnt it?
Then what would happen if we include the non .h headers without using namespace std;?? we can also use their functions cant we?? if so the what is the help of that statement? And if so, then if we include and use those headers without using namespace std; is the same as using the .h headers isnt it?? then i wouldnt find a great difference between the use of both... |
Recent GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| this is from 6 files now i need fancy reports to write to the files | kilgortrout | C++ Forum | 1 | 21-May-2004 16:57 |
| Can't view pages from another machine on the Intranet | aevans | Apache Web Server Forum | 9 | 14-May-2004 02:26 |
| uploading files into server | prinzekay | MySQL / PHP Forum | 5 | 16-Mar-2004 00:00 |
| gxx linker accepts only 7 object files | danielxs66 | C++ Forum | 1 | 12-Dec-2003 09:27 |
| publishing Drirector/Shockwave files ...? | darrin | Web Design Forum | 1 | 04-Oct-2002 17:21 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The