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)
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).
cout << "This function call returns a 10 from (global) version.\n";
cout << functionReturningInt() << endl;
cout << "\nThis function call returns a 20 from space1 version.\n";
cout << space1::functionReturningInt() << endl;
cin.get();
return0;
}
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.
@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?
>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.
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.
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.