Where should I #include the header files

To understand my question have a look at the code first:

Foo.h

1
2
3
4
class Foo
{
	void bar();
};


Foo.cpp
1
2
3
4
void Foo::bar()
{
	std::cout << "Foo::bar()" << '\n';
}


Where should I #include <iostream> , in Foo.h or in Foo.cpp?

I think it that it makes sense to #include <iostream> only in Foo.cpp since it is used there and it is not required anywhere in the declaration of class Foo.

Thanks in advance to anyone who replies!
You're correct that the .cpp file would be the best place since the .h file never uses the class. IMO you should never include unnecessary files. But you should always include all necessary files, never rely on some other file to include a required file.

@jib

Thank you so much for the quick reply!
Topic archived. No new replies allowed.