1st - should you ever have a main.h?
Very rarely. main.cpp
implies it's compiling the translation unit that contains main()
, which is typically client code for other lower-level libraries that shouldn't need to know about the symbols in main()
. If somehow things got cyclic in your design, and there was good reason (massive time pressure?) not to split something you'd put out into a separate .cpp
, then you could end up with a main.h
. That should really only declare the symbols in main.cpp that other translation units might need access to. Specifically, it shouldn't include car.h and/or speed.h unless exposing main.h functions that need car.h or speed.h declarations - for example, a declaration of a function in main.cpp that took arguments of types from car.h or speed.h.
2nd - if main.h has #include car.h and #include speed.h then in car/speed.cpp do you just have to add #main.h (thus it would include car/speed.h)
As above, this is almost certainly a very broken design: main.cpp would be expected to include car.h and speed.h directly, and if it doesn't want to do that and a higher-level header is needed for car.h and speed.h, it should be named based on their overarching theme (e.g. transport.h), not named after the specific client that wants access to both. Remember, main.h should only exist if it's necessary to expose things from main.cpp.
3rd - should you ever go that route?
Probably not, given what I've explained above.