Avatar ·

Why are header files (.h) and implementation files (.cpp) needed in C++?

Hi everyone! I just started learning C++ and I can't understand why this separation into header files and .cpp files is needed. In Java or Python, I just wrote all the code in one file and everything worked. But here there's some kind of hell with #include, #pragma once and other directives. For example, I write a simple program: ```cpp // main.cpp #include void sayHello() { std::cout << "Hello!" << std::endl; } int main() { sayHello(); return 0; } ``` And it works. Why do I need to create a separate sayHello.h and sayHello.cpp? After all, you can write everything in one file. I've heard that this is needed for code organization and to avoid linker errors, but I don't understand exactly how. I'm especially scared when I see that function declarations (prototypes) are written in the .h file, and the implementation is in the .cpp file.