Chapters 11, 14.1-3, 14.5, 18 and 19
The ability to define object data types (i.e., classes) provides a powerful mechanism for abstraction in problem solving. By tightly controlling the state of every instance through a class's public interface, the implementation remains truly opaque. The programmer using such a class need only understand how to call the publicly-available methods and understand what their behavioral guarantees are, the knowledge of which is often available from the header file alone. In this way, the individual elements of an object's state become irrelevant from outside the implementation, facilitating the understanding of the object as a single, coherent data value.
This level of abstraction offers two advantages to the programmer. On the one hand, she need not be responsible for maintaining the relationships among an object's component variables that are necessary for a valid state. The second advantage follows from this: by considering the defining class only as an abstract data type, it becomes easier to write sometimes complicated algorithms that manipulate object values of this type. This is important, since many important algorithms were developed independently of a particular class implementation, and since particular tasks we want to perform on an object (e.g. comparison) may be quite complicated.
For example, consider the problem of comparison between between values. As we discussed in class, comparison can mean a number of things (lexicographic, alphabetic, numeric, reverse numeric, etc.). For a class such as Time, even the usual sense of comparison can be difficult to get right:
int Time::compare(Time d) { /* returns -1, 0, or 1, according to whether this Time is less than, equal to, or greater than d */ int this_hour = hour, d_hour = d.getHour(); /* One subtlety: 12 pm is noon, and thus earlier than all other pm times. Similar for midnight. */ this_hour %= 12; d_hour %= 12; if (merid < d.getMer()) return -1; else if (merid > d.getMer()) return 1; else if (this_hour < d_hour) return -1; else if (this_hour > d_hour) return 1; else if (min < d.getMin()) return -1; else if (min > d.getMin()) return 1; else }
Details like this are best ignored when one simply needs to make a comparison of two Time objects as part of a more complicated activity such as sorting.
... is to complete the code in a test driver for the ApptBook class, which provides an implementation of a simple appointment book. The files provided for this project are the following:
All files can be found in the directory
http://www.cs.uoregon.edu/classes/cis122/code/hw6/You will make changes only to testApptBook.cpp. Although you will need to read the header files to learn about the functionality of the Date, Time, Appointment and ApptBook classes, you should not need to even look at the code in datetime.cpp or apptbook.cpp (although you may find it interesting). You will make no changes to the the header or data files.
For testApptBook, your task is twofold:
The ApptBook implementation in apptbook.cpp includes a private find method, which is used in several methods to help ensure that no duplicate Appointment values are stored. Currently, the body of this method implements a linear search.
However, we can do better than this. Because of the way that new Appointment values are added (using another private method, insert), the list of appointments is always stored in sorted order. Consequently, we could have implemented find to use a binary search.
For extra credit, make this modification.
Make sure that all six files are in the same directory. Then compile with the following command:
g++ -o testApptBook testApptBook.cpp datetime.cpp apptbook.cpp
Test your program by running it. If successful, you will have two files, "appts-2.dat" and "appts_sched.txt", each of which contains 18 appointments, sorted in chronological order.
Since you won't be making changes elsewhere, don't bother submitting any of the other files.
Submit your source code file as a plain text email attachment, sent to me at johnfl@cs.uoregon.edu. Please avoid paper copies. The full source of apptbook.cpp is particularly unwieldy.