I don't see any header file included into it. |
Oh? What then was this:
#include <iostream>
I know we use header files to make compiling faster |
No, they don't actually make things faster. (There are concepts of "precompiled headers" and "compilation firewalls", but they are details.)
we put functions in the header files |
No. Functions and types can be
declared in header, but
implementation can be in only one
translation unit. ODR, One Definition Rule: something can be defined (implemented) only once within a program.
A header can be included into many source files. If that header would contain implementation of a function, then all of those source files would have a copy and violate ODR during linking.
You cannot refer to a function or class in a source file unless you tell (
declare) the compiler that such thing exists. You have to declare the same thing in every source that needs it, but you don't want to copy-paste into every file. You rather store the declaration into a separate file (the header) and include that file where needed. Now you can update the declaration in one place, if needed.
If the previous program had been just:
1 2 3 4
|
int main {
std::cout << "hello\n";
return 0;
}
|
Then the compiler would have asked:
Error, error, wth is this
std::cout
?
Error, the only versions of
operator<<
that I know do not work with operators:
I-have-no-idea and
const char*
You have now two options:
1) write the declarations for std::cout and operator<< that allow linking with Standard C++ IO shared object.
OR
2) #include <iostream>, because file
iostream has those declarations.
Your really don't want to even consider the option 1.
Header files are used with language C too. C has no classes. Headers of Standard C library are "without classes".
Header files are not about classes.