I have found some threads that explain why C++ separates .cpp and .h files (e.g. here). I’d be interested to know if it causes any problem if I don’t separate them. I don’t want to share the object files, so what’s the benefit of the separation on a small project? If it just slows down the compilation time, it’s not a big deal in my opinion. I want to re-implement a Java program in C++, so to me it seems much easier to keep a class in one file only. Example:
// Hello.cpp
#ifndef HELLO_20091218
#define HELLO_20091218
#include <iostream>
#include "Utils.cpp"
class Hello
{
public:
void start()
{
std::cout << Utils::nrand(100) << "\n";
// Utils and all other classes are written in a similar way
}
};
#endif
There is a thing that troubles me. “Defining a member function inside the class asks the implementation to expand calls to it inline.” So if I do like this, everything is marked inline implicitly. Will it cause a larger executable or any other disadvantages?
In C and C++ the smallest unit of compilation is the file. If you just don’t use header files and include everything in your “main” file, everytime you change something your whole program would have to be recompilled. For larger applications this can be a very good argument for separation of header and implementation. Also if another part of your application would live in another binary executable and you wan’t to reuse classes you are safe with header files while you will get alot of overhead without them.
If you don’t care about those things (You’ll regret that.) there is no need in separate header files for you.
About the inlining: The compiler will inline a lot of functions (sometimes even whole classes, so to speak) anyway, even if you don’t ask it to do that. Inlining is generally a benefit for performance. There are corner cases (large size of the executable can resolut in slower execution) but those are fairly unusual.