// B3.cpp - initializing constant and reference members // // constructors must initialize reference and constant members using // initialization sequences. This is so because references and const // data members cannot be reset once they are constructed, even in the // constructor body for class X. // #include #include using namespace std; class X { public: X(istream &input, int size) : in(input), _size(size) { } X(const X &x) : in(x.in), _size(x._size) { } void getInput() { char byte; int count = 0; while(count++ < _size && in.good()) { in.get(byte); cout << byte; } cout << "\n\n"; } private: istream ∈ const int _size; }; void main() { istringstream input( "\n programmers are like playwrights and all computers are bad actors" ); X x(input,35); cout << "\n x: "; x.getInput(); X y = x; cout << "\n y: "; y.getInput(); }