Class questions

Hi,

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.

Thank you:)
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?
As long as you link all the files, you can have as many files you want

In which of these 3 files does <iostream> and <string> needs inclusion?
If the class declaration needs some other headers, #include what you need in that your header or use forward declarations


Hey,

I don't really understand your answer on the second question.

Can you explain that a bit more?

Thanks
Obligatory link:

http://cplusplus.com/forum/articles/10627/

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.

/me points to above article link
Thank you Disch, I'll read that tutorial this evening :)
Topic archived. No new replies allowed.