UMBC CMSC202, Computer Science II, Fall 1998,
Sections 0101, 0102, 0103, 0104
7. C++ I/O
Tuesday September 22, 1998
[Previous Lecture]
[Next Lecture]
Assigned Reading: 4.1-4.5
Handouts (available on-line):
Programs from this lecture.
Topics Covered:
- Continued the discussion on destructors:
- Last time we looked at a program that
returns a reference to a dynamically allocated object. Careful scrutiny of
the sample run will reveal that the memory
allocated for the pointer R in function foo() was never
freed and cannot be freed in the main program because the pointer to this
object is lost.
- One way to solve this problem is to declare P as a reference
and initialize it with the return value from foo(). Then we
can delete the address of P because we remembered that it was
initialized with dynamically allocated memory. This is not an elegant
solution. (See
program and
sample run.)
- One reason we are having trouble returning an object from a function
is that this is not object-oriented thinking. We want to call the function
foo() and stuff the return value into the object P. The
object-oriented approach would make the function foo() a member
function of the Record class. Then, we would request that P
"modify itself" using the member function.
- Still, even following OOP guidelines we might find ourselves in a
situation where we want to assign a value to an object. This is
properly accomplished by creating two member functions in the class:
a copy constructor and an overloaded assingment operator. In the new
declaration of our Record class in record3.h,
we have two new member functions:
Record(Record &) ; // copy constructor
Record& operator=(const Record&) ; // overload assignment
These member functions make allocate new memory for a Record when a
Record is assigned and when a Record is automatically copied (e.g., when
a Record is passed by value). See the implementation of the Record class.
-
With the copy constructor and overloaded assignment operator in place,
we can pass Records by value and return a Record object without
inadvertently delete any strings. (See
program and
sample run for return an object
and program and
sample run for passing an object
by value.) Do note that using a copy constructor and an overloaded
assignment operator can slow down your program because these member
functions may have to copy large data structures.
- Next topic: C++ I/O. The same issues that come up with console I/O
in C programming also come up in C++ programming.
- Example 0: we try to read in strings into fixed sized
character arrays. We encounter problems if the user types in really
long strings.
Program and
sample run.
- Example 1: we use the setw() function to keep long
strings from overflowing our arrays, but we still have the problem
that the unread portions of the long string remains in the input buffer.
Program and
sample run.
- Example 2: the member function ignore() can be used
to eat up characters still in the input buffer. This is similar to
fflush(stdin) from C I/O. Note that the constant
INT_MAX is defined in the header file limits.h.
Program and
sample run.
- Example 3: we try to read in a bunch of integers and store
them into an array. Here we encounter problems if the user enters
non-numeric input or when the user types in more than one number per
line.
Program and
sample run.
- Example 4: we use the good() member function to check if the
cin input stream successfully read in an integer. We use a while
loop to pester the user until the user enters the data correctly. Note the
use of the clear() function before calling ignore() if
the input was not an integer. Also note the use of the Boolean type
bool with constants true and false.
Program and
sample run.
- Example 5: if we want to read in an arbitrarily long string,
there is no shortcut except to read in one character at a time and
reallocate space for a longer string if we run out of space. We use
the get() member function to read in one character.
Program and
sample run.
[Previous Lecture]
[Next Lecture]
Last Modified:
22 Jul 2024 11:28:50 EDT
by
Richard Chang
Back up
to Fall 1998 CMSC 202 Section Homepage