UMBC CMSC202, Computer Science II, Fall 1998,
Sections 0101, 0102, 0103, 0104
8. Designing Classes
Thursday September 24, 1998
[Previous Lecture]
[Next Lecture]
Assigned Reading: 4.6-4.13
Handouts (available on-line):
Project 2
Programs from this lecture.
Topics Covered:
- Discussed Project 2.
- Took attendance.
- Continued with C++ I/O:
- Example 6: file I/O. We use the open() and
close() member functions of an ifstream and
ofstream object to open and close a file for reading
and writing, respectively. Once open, we can use the <<
and >> operators with the stream objects as we've
done with cin and cout. Note the use of the
eof() member function to detect the end of file.
Program and
sample run.
- Example 7: we modify our GetLine() function
to work with file I/O as well as console I/O. The only tricky
part is that we need to test for the end-of-file condition. In order
to trigger an eof condition, we must attempt to read past
the end of the file. So, at the beginning of the function, we
read in one character, then test for eof. If we are not at the
end of the file, then we use nifty putback() function
to pretend that we didn't read that extra character. The result
is a GetLine() function that works with input files
and with cin.
Program and
sample run.
- The GetLine() function shown above is ugly. It was developed
without regard to object-oriented philosophy. We discussed how we might
develop an object-oriented string class that allows strings to shrink and
grow as we need more or less space. One possible many possible designs is
the BString class declared in the header file bstring.h. Sample programs show
that one of the design goals of this class is to allow the user to
use BString objects and char * strings somewhat interchangeably.
The implementation of the BString
class will be discussed in the next lecture.
[Previous Lecture]
[Next Lecture]
Last Modified:
22 Jul 2024 11:28:51 EDT
by
Richard Chang
Back up
to Fall 1998 CMSC 202 Section Homepage