I've just learnt some basics of OOP questions and to split .cpp and .h files. But the longer I'm doing this, the more questions I'm starting to have.
Let's assume I have two .h files and in these files, there is 1 class (so 2 classes in total).
Does all methods of these two classes needs to be written in one .cpp file or do I need to create a .cpp file for every .h file?
Also, I don't really understand when to include something.
Assume the following situation:
1 .h file
1 .cpp file with the methods in there
another .cpp file with the main function
The .h file doesn't use anything from <iostream>, but it does from <string>. The other two files uses both things from <iostream> AND <string>
In which of these 3 files does <iostream> and <string> needs inclusion? I use #ifndef for the .h file.
Typically, you put a class in a .h file, and have an accompanying .cpp for that class's implementation (ie, for MyClass you'd generally have "myclass.h" and "myclass.cpp"). This is not a fixed rule, however. Often times you'll have headers without a matching .cpp file, or you might also have several cpp files to handle the implementation for a single class. It's all about how you want to organize your program.
The .h file doesn't use anything from <iostream>
Then don't include it in the .h file
but it does from <string>
Then include it in the .h file.
In which of these 3 files does <iostream> and <string> needs inclusion
#include in files where the header is a dependency.