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.

Recommended Answers

All 9 Replies

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:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int functionReturningInt() {
  cout << "In (global) functionReturningInt()\n";
  return 10;
}

namespace space1 {
  int functionReturningInt() {
    cout << "In space1::functionReturningInt()\n";
    return 20;
  }
}

int main() {
  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();
  return 0;
}

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.

>Well, unlike C, C++ provides a concept of headers(not header files)
The concept of headers is identical between C and C++.

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.

>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.

Ok. i get it. Thanks Narue.

Thanks again Fbody

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>

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.