UMBC CMSC202, Computer Science II, Fall 1998,
Sections 0101, 0102, 0103, 0104
17. More Class Derivation
Thursday October 29, 1998
[Previous Lecture]
[Next Lecture]
Assigned Reading: 7.6-7.15
Handouts (available on-line):
Programs from this lecture:
Topics Covered:
- Exam 2 topics
- In this lecture, we look at some more useful examples of public
derivation. For derivation to work properly, the base class must have been
designed with derivation in mind. For example, we want to extend the Array class used in Lecture 14. However, the data member
size was declared as a private member of the Array class. This
prevents us from defining constructors in a derived class that would modify
the size of an array. Clearly, an undesirable situation. As a result, we
are forced to redefine the Array class to make it more amenable to
derivation. In the new declaration,
the size data member is declared as a protected member. This
allows us to declare an FAarray class
that includes a constructor which makes an array from the contents of a
file.
- Because an FArray object is also an Array object, functions
written to take Array parameters can also take FArray parameters
(e.g., the Merge Sort function). If we simply copied the
declaration and implementation of the Array class and made a new
FArray class without using class derivation, then the compiler would
not know the relationship between Arrays and FArrays.
- We took a closer look at the GenQ class for generic queues
from Lecture 10 and discusses some implementation details.
- It is awkward to use the GenQ class. For example, to enqueue
an integer, we must first dynamically allocate memory for the
integer and pass a pointer to that location:
L.enqueue(new int(5)) ;
Dequeuing is also awkward. The dequeue() function returns
a pointer to an integer. This memory must be deallocated each time.
- We can eliminate the awkwardness of the GenQ class by deriving
easier-to-use, specialized classes from GenQ. However, we need to
modify GenQ somewhat to make it amenable to derivation. Again, a
bit of forethought would have made these changes unnecessary.
New class declaration and
implementation.
- Now it becomes relatively simple to derive an IntQ class for
integer queues from GenQ:
- Similarly, we can derive a class for a queue of strings using
the BString class from Lecture 9
and Lecture 10.
[Previous Lecture]
[Next Lecture]
Last Modified:
22 Jul 2024 11:28:49 EDT
by
Richard Chang
Back up
to Fall 1998 CMSC 202 Section Homepage