Notes for week 7

For this week's links click here.

Another Windows Example

One of the standard classes of programs that are implemented using object-oriented languages is the simulation. The first widely used OOPs language was in fact called Simula. Creating a simulation in Windows requires that you abandon the standard type of program that calls on the simulation function in the main. Here we will create a menu entry simulate. This will call the simulation function. For a discussion of this simulation click here.

TWindow use continued

Overview

In the Easy Windows version of the program, we automatically loaded a file at the start of the program and saved at the end of the run. Our windows program will allow us to have multiple lists (say one for Tuesday's shopping and one for Friday's shopping). In Windows, the class responsible for file operations must be determined before you create your operations. Normally the window that is responsible for operating on your data is the window that saves the data. On the other hand, if each of the application windows is responsible for a different list (here only one can be open at a time, but later we will have more than one open), then a class other than the application window must be used to open the window. This is the chicken and egg problem. In all of our applications there will be a parent class for the application class, and this parent will open files and create the application window. In this SDI application, the class is the application class.

Saving Files

Saving files in windows is a bit more complicated, but allows the user to more easily place a file where it is supposed to be. It also allows the user to see the other files in the directory, so they don't repeat a file name. In addition, you can restrict the suffix that will be used, so the user only has to type the file name and an appropriate suffix will be used.

All this is accomplished, by separating the file saving process into two parts. The first part is getting a file name using the TFileSaveDialog. This processes is done in the SaveAs command handler. Actual file saving takes place in the Save command handler.

Openning Files

When opening a file, the application class uses the TFileOpenDialog to establish the file name for the application TData. Then actually lets the window read the data. In some cases (you will see later) the class that finds the file also reads the data, and the creates the window to display the data. Here only one window is used to handle the data, so we simply clear the data and read the new in that window. Click here to see the function.

The Grocery List - Modified

What changes would you have to make to the grocery list problem if some of the items were taxable? Could we just mark some things as taxable? This would lead to a set of problems not the smallest of which is when do we compute the tax. If we compute the tax at the time when we add the cost to our total, we would have to use the amout of the sale twice. Remember, that each object is supposed to know how to "do its thing." That means each object should compute its own tax. This means that the function computeTax would have to be available for each object, but would return 0 for nontaxable items, while computing the tax on taxible items.

Clearly computeTax is work for a virtual function and we really have two types of GroceryItem,

If we have two types of GroceryItem, and we mix these items in the same list, we must use pointers to the items because of strict typing. Now we have an array of items, so we must produce an array of (counted) pointers to objects that are children of GroceryItem.

Class discussion: Construction of the classes and revisions of functions.

Creating Dialog Boxes

New GroceryItems are created by collecting information about the item and then calling the constructor for GroceryItem. Information is gathered by repeated calls to TInputDialogBox's Execute function. The Execute function is shared by all dialog boxes. The use of multiple calls frustrates the users and makes it more difficult to enter data in an error free manner. When you need to aquire more than one piece of information it's best to create your own dialog box. Over the course of the rest of the semester we will look at the types of information that you can aquire using dialog boxes.

When creating a dialog box, first decide on the data that you will aquire and then make a sketch of the dialog box with appropriate gadgets for each item you want the user to enter. Once you have done this, enter the resource workshop from the ClassExpert. The easiest way to do this is to select the "Edit Menu" choice from the right button menu. Once in the resource workshop you can create your dialog box. Return to the ClassExpert (without quiting the workshop) and create the class that corresponds to that dialog box.