Best practice for include and using statements.

I've got a program that uses three files: main.cpp, date.h, and date.cpp. I've read that you shouldn't use the whole standard namespace if you don't have to. I also imagine that #include <iostream> being repeated again and again isn't necessary. Currently, main.cpp has
1
2
3
4
5
#include "date.h"

using std::cin;
using std::cout;
using std::string;

The date.h has
1
2
3
4
#include <iostream>

using std::istream;
using std::ostream;

And date.cpp has
#include "date.h"
Does this seem to be a decent way about doing things? I know that I could probably move all of the using statements to the header file, but if that header file were to be reused in a different program that doesn't need cin, cout, or string, it'd be a waste. Now that I'm thinking about it, maybe a repeated #include <iostream> in main.cpp wouldn't be so bad bad if it has the guard, which I'm guessing it does. Anyhow, you guys know better than I do. Thanks in advance. Take care!
> if that header file were to be reused in a different program that doesn't need cin, cout, or string, it'd be a waste.

Yes.

Ideally, the header file would not even contain a #include <iostream>
A #include <iosfwd> would suffice if the stream operations are not inline.

1
2
3
4
5
6
7
8
9
10
11
12
// header file date.h

// include guard

#include <iosfwd>

struct date
{
    // ...
    friend std::ostream& operator << ( std::ostream&, const date& ) ;
    friend std::istream& operator >> ( std::istream&, date& ) ;
};


With the #include <iostream> moved to the implementation file.
Topic archived. No new replies allowed.