06.08.2013 Views

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Strings and Streams 27<br />

Note:<br />

• Since the parameters are of type istream or ostream, the operator functions work for both<br />

file streams and string streams.<br />

• The functions are global “helper” functions. 7<br />

• Both operators return the stream object that is the first argument to the operator, enabling<br />

us to write, e.g., cout c;<br />

if (c == ’(’) {<br />

is >> re >> c;<br />

if (c == ’,’) {<br />

is >> im >> c;<br />

if (c == ’)’) {<br />

cplx = complex(re, im);<br />

}<br />

else {<br />

// format error, set the stream state to ’fail’<br />

is.setstate(ios_base::failbit);<br />

}<br />

}<br />

else if (c == ’)’) {<br />

cplx = complex(re, num_type(0));<br />

}<br />

else {<br />

// format error, set the stream state to ’fail’<br />

is.setstate(ios_base::failbit);<br />

}<br />

}<br />

else {<br />

is.putback(c);<br />

is >> re;<br />

cplx = complex(re, num_type(0));<br />

}<br />

return is;<br />

}<br />

}<br />

7 The alternative implementation, to write operator>> and operator

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!