Are header files usually made with classes? c++

I've been struggling with making a simple header file for 2 weeks now, but I finally made one today. I'm learning c++ from this site http://www.learncpp.com/cpp-tutorial/19-header-files/ and the teacher doesn't use a class with his header files, but when I looked on other sites they used classes. Is it a bad practice to use header files without classes?
Think about this:
Why is a header file used in the following program?
1
2
3
4
5
6
#include <iostream>

int main {
  std::cout << "hello\n";
  return 0;
}
I dont know. I don't see any header file included into it. I know we use header files to make compiling faster and we put functions in the header files. Everytime I search on youtube about how to use header files on multiple files in a project people always use classes, but in the link I show we dont even discuss classes yet and how a header file is made is different so I'm wondering if this is normal or not.
your question: Its fine to have header files that are not tied to a class. I have a header file that does nothing but include other header files, to keep them organized. I have another with nothing but conversion factors of common things, like radians to degrees and math constants like pi, e, and so on.

The reason most headers are for classes is that most modern programming is object oriented. Nothing more.



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.
Topic archived. No new replies allowed.