If you put an #include where appropriate in a header file, should you duplicate it in the file where you have the function definitions as well as in the file with main()?
Here's an example:
I'm using strings and hence put in the file account.h the statement #include <string>
Now in the file account.cpp, the following will compile:
The last function I even threw in extra just to see if it would work without the #include.Code:#include <iostream> #include "account.h" using namespace std; void Account::showAcct() const { cout << "Name: " << name << endl; cout << "Account number: " << acctNum << endl; cout << "Balance: $ " << balance << endl; } void Account::deposit(const double depAmt) { balance += depAmt; } void Account::withdraw(const double wdAmt) { balance -= wdAmt; } std::string Account::test() { return "tested"; }
I also don't have a #include <string> in the file with main(), but it also doesn't make explicit use of strings and does have #include "account.h" of course.
Is it good programming practice to put the #include in explicitly in all these spots even though, using this case as example, <string> gets included because it's included in the account.h file?
Or, asking my question a bit differently, should you explicitly #include the various libraries you need even if you've indirectly #included them already through reference in a header file?