943,015 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 163
  • C++ RSS
Sep 2nd, 2010
0

C++ headers

Expand Post »
Hello.

Well, unlike C, C++ provides a concept of headers(not header files) and that these headers are something like a collection of names, which may mapped as header files by the compiler.

Can anyone please correct me if im wrong in my above understanding. Also please state the main difference between a namepace and a header.(since even a C++ header is a collection of names)

Thanks in advance.
Similar Threads
Reputation Points: 57
Solved Threads: 38
Posting Whiz
myk45 is offline Offline
311 posts
since Sep 2010
Sep 2nd, 2010
0
Re: C++ headers
The C++ Standard headers are a collection of named functionalities, not merely a collection of names, that all C++ compilers are required to provide at a minimum. I believe they are permitted to either be implemented as header files or implemented directly within the compiler itself, as long as it recognizes the header names defined within the Standard and provides the appropriate functionality, but I'm not positive.

A namespace is a mechanism that helps with object naming conflicts. In Standard C++, all default functionalities are declared in headers as members of the std namespace. This affords you the opportunity to re-use any of those names as long as you declare them within a different namespace (not that I would recommend it).

Example:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using std::cout;
  3. using std::cin;
  4. using std::endl;
  5.  
  6. int functionReturningInt() {
  7. cout << "In (global) functionReturningInt()\n";
  8. return 10;
  9. }
  10.  
  11. namespace space1 {
  12. int functionReturningInt() {
  13. cout << "In space1::functionReturningInt()\n";
  14. return 20;
  15. }
  16. }
  17.  
  18. int main() {
  19. cout << "This function call returns a 10 from (global) version.\n";
  20. cout << functionReturningInt() << endl;
  21.  
  22. cout << "\nThis function call returns a 20 from space1 version.\n";
  23. cout << space1::functionReturningInt() << endl;
  24.  
  25. cin.get();
  26. return 0;
  27. }
As you can see, I have implemented 2 different versions of functionReturningInt(). One is not within a namespace (it's "global") and the other is in the namespace "space1". I can call whichever one I choose by using the scope resolution operator '::'. I also use cout, cin, and endl which are declared within the std namespace.
Last edited by Fbody; Sep 2nd, 2010 at 5:19 pm.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Sep 2nd, 2010
0
Re: C++ headers
>Well, unlike C, C++ provides a concept of headers(not header files)
The concept of headers is identical between C and C++.
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 2nd, 2010
0
Re: C++ headers
Thanks a lot Fbody.

@Narue
Can you please elaborate a little more. Do you mean to say there is a concept of headers even in C? i mean are'nt the function prototypes, etc listed in the header files. So what does the header in C mean?

Thanks.
Reputation Points: 57
Solved Threads: 38
Posting Whiz
myk45 is offline Offline
311 posts
since Sep 2010
Sep 2nd, 2010
0
Re: C++ headers
>Can you please elaborate a little more.
It seems like you're making the proper distinction between headers and header files (ie. a header need not be a file). However, aside from that distinction, there's no difference: Include a header and a bunch of stuff gets textually pasted into your translation unit. No magic, just glorified copy/paste.
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 2nd, 2010
0
Re: C++ headers
A header file is a header. The terms are mostly synonymous. The modern versions of the header files just have different names.

The C++ headers <cstdlib>, <cstdio>, <ctime>, <cmath>, and many others, are modern C++ versions of the older C "header files" <stdlib.h>, <stdio.h>, <time.h>, <math.h>, etc. respectively. The newer versions are the same as the old, the names and functionalities within them are just declared as part of the std namespace instead of simply being global.
Last edited by Fbody; Sep 2nd, 2010 at 5:56 pm.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Sep 2nd, 2010
0
Re: C++ headers
Ok. i get it. Thanks Narue.
Reputation Points: 57
Solved Threads: 38
Posting Whiz
myk45 is offline Offline
311 posts
since Sep 2010
Sep 2nd, 2010
0
Re: C++ headers
Thanks again Fbody
Last edited by myk45; Sep 2nd, 2010 at 5:54 pm.
Reputation Points: 57
Solved Threads: 38
Posting Whiz
myk45 is offline Offline
311 posts
since Sep 2010
Sep 2nd, 2010
0
Re: C++ headers
And of course there is <windows.h> which I use frequently.

Remember, I am pretty sure that with headers that end in ".h," you can just add a "c" at the beginning to do the same thing

Example I think <cmath> = <math.h>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
koolman123 is offline Offline
8 posts
since Sep 2010
Sep 3rd, 2010
0
Re: C++ headers
That only works in C++ and only for Pre-Standard headers inherited from C. Using the c* versions pulls them in to the std namespace.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Serialization
Next Thread in C++ Forum Timeline: Hello Everyone





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC