UMBC CMSC202, Computer Science II, Spring 1998,
Sections 0101, 0102, 0103, 0104 and Honors
Tuesday April 7, 1998
- A Book on C:
- Programming Abstractions in C:
Handouts (available on-line): none
- Discussion about differences in grade distribution between IFSM and
CMSC majors. The upshot: I don't see any. Conclusion: I expect the
same amount and quality of work from you whether you are a IFSM or CMSC
- We converted our singly-linked list
implementation of the list ADT to C++. I've added 3 new functions:
FirstPos(), NextPos() and ItemAt().
Otherwise, we have a one-to-one translation. The C++ main program which uses the List
class was modified from the previous C version. This was necessary
because the main program must use C++ syntax to call the member
functions. The sample run, on the
other hand, is the same as before, except for the part that exercised
the new functions.
- Instead of defining a type for node structures, we
define a class ListNode, which does the same thing. Similarly, the
class List replaces the list structure in the C version. (See header file and implementation.)
- The only member functions in the ListNode class are two
constructors and a destructor called ~ListNode(). This
destructor replaces the FreeNode() function in the C
version. A destructor is the opposite of a constructor. This
function is called whenever an object of type ListNode is no longer
needed. In our ListNode implementation, the destructor frees up
the memory used by the string data in the node. We will need to
discuss destructors in greater detail in the future. Until then,
note that the program here is very careful to only declare
variables that are pointers to ListNode and that all ListNode
objects are created using the new operator.
- Similarly, in the List class, we have a destructor called
~List(). This is analogous to the FreeList() function
in the C version.
- The members header, count and last in the
List class are in the protected section. This is similar to private and
will be explained later.
- We defined a type called position. Values returned by
Locate(), FirstPos() and NextPos() have
type position. We could have made these return pointers to ListNode, but
it is cleaner to have a separate type. (If we did this in the C
version, then our array implementation of the list ADT would have
a cleaner Locate() function.)
- One of the advantages of Object-Oriented Programming (OOP) is
the ability to reuse code.
- If we wanted to reuse our list implementation to work with
integer data instead of string data, we could look at the source
code and replace all references to functions like strdup()
and strcmp() with appropriate code for integers. However,
the process of editing and modifying code is tedious and error
prone. For example, you may very well have missed that the
Print() member function must be modified to print out
integers instead of strings.
- A better way to reuse the code for the List class is to make
a new class called ListItem. Instead of having ListNode hold a string
directly, each ListNode object will contain a pointer to a ListItem
object. For starters, a ListItem object will simply hold a string.
That makes this implementation functionally equivalent to the previous
one. However, since we have defined a ListItem class, we can make all the
functions we need to work with strings member functions of the ListItem
- We put the definition of the Listitem class in a file by
itself called stringitem.h.
The member functions for this class are: 2 constructors, the
destructor, copy(), compare() and
print(). The member function copy() returns a
copy of the data stored in the ListItem host object. In the
implementation of copy() (see stringitem.C), we simply call
the strdup() function. Similarly, the member function
compare() just calls strcmp and print()
calls printf(). The purpose of encapsulating the string
data this way is that we can substitute this ListItem class with
another ListItem class that holds integers instead of strings. And
as long as the member functions are implemented correctly, we can
use the code for the ListNode and List classes without
modification. To make this point explicit, we include a header file
called listitem.h in the header file for the List class. The
only purpose of the listitem.h file is to include the stringitem.h
file. When we reuse the code for linked-lists of integers, the only
line of code we will change is the include command in listitem.h
--- we will change it to include intitem.h instead of stringitem.h.
- In the meantime, we have modified the implementation of the ListNode and List
classes so it works properly with the ListItem class. A sample run of the main program shows that the
modifications were successful.
- Next, we implement a different ListItem class where the data
in each ListItem is an integer instead of a string. This
implementation is really easy, since we do not need to allocate
memory for integers. (See header
file and implementation.) A
sample run of the main program shows that the only change
we have to make is one line of code in the listitem.h header file.
Thus, we have quickly turned our linked-list code for strings into
linked-list code for integers.
- We can repeat this exercise for structures instead of
integers. In this example, each ListItem object holds a pointer to
a structure of type book_t taken from Project 1. Again, the header file and the implementation of the member
functions for ListItem are very simple. Thus, we have quickly
converted our linked-list code for strings into linked-list code
for book_t structures. In the main program, we use fread()
to read in a sequence of book_t structures and we store
each structure in a linked-list. The sample run prints out the list
of books in the SF_Adventure file.
9 Apr 1998 00:12:38 EDT
to Spring 1998 CMSC 202 Section Homepage