Computer Science 3675
Summer 2002
Programming Assignment 6

Due: Wed. July 24, 11:59pm.

Procedural and functional programming both encourage you to organize programs so that similar functions working on different kinds of data are grouped together. For example, when you write the derivative function, all of the different kinds of derivative formulas are put into a single function that computes all different kinds of derivatives. Similarly, a single simplification function has to deal with any kind of expression, and all simplifications are grouped together.

Object-oriented programming encourages you to organize your program so that different operations that work on the same kind of data are grouped together. For example, all functions (such as computing the derivative and simplification) that work on expressions that are sums are put in one place, and those that work on products are put someplace else. This organization tends to result in software that is more flexible and easier to work with.

The organization method that object-oriented programming encourages makes some special requirements on a programming language. It must be able to perform some run-time type checking, so that it can check what kind of thing a function is acting on. The language implementation must also be able to find appropriate functions, even though they are spread among different modules.

The assignment

For this assignment, rework your derivative program into a more object-oriented form. You will not go all the way to object-oriented programming here, but only part of the way. Build a separate file for each kind of expression. All of the operations on a given kind of expression should be put together.

Get files Expression.ast, XExpr.ast, ConstantExpr.ast and SumExpr.ast, implementing the concepts of an expression (a general expression), a variable, a constant and a sum, respectively. Look at them as examples of how to write your new files. Also get TestExpr.ast, which contains some tests of these.

Add new files to cover difference, products and constant powers, as in the previous assignment. Use one file for each new concept.

You should not have to modify files XExpr.ast, ConstantExpr.ast, SumExpr.ast or Expression.ast at all in order to write your new files. Just add new files. The tester will need to be modified, to make it do more tests. Do not turn in untested programs.

Virtual functions and union types

Your first solution to the derivative problem uses a union type to capture all of the different kinds of expressions. Each time a new kind of expression is added, a new line must be added to the union type, and functions that operate on that type need to be modified. This assignment uses the idea of virtual functions to create unions in a different way. A general type, EXPRESSION, is created that is, in a sense, the union type. Each kind of expression is a separate type that lies beneath EXPRESSION in the hierarchy. Shared operations are defined for type Expression, although some of the implementations of those functions can only be done in the specific types. The deriv function is written in the EXPRESSION module. It calls rawDeriv to do the basic derivative computations. But the way to compute a derivative depends on the kind of expression that you have. Function rawDeriv is virtual in the EXPRESSION module. Its implementation is found in other modules. Each time a new type is added beneath EXPRESSION in the hierarchy, an implementation of rawDeriv for that type must be defined.

This style of programming tends to lead to somewhat larger programs, but the programs are much more flexible. You can add new kinds of expressions without modifying any of the other expression modules. You do not even need to recompile the old expression modules. Imagine that type Expression and a few different kinds of expressions are provided in a library. By only using the library (and not modifying it in any way) you can use not only the kinds of expressions that are provided to you, but others as well. You only implement the new kinds. What you get is an extensible union type, where you can add anything that you want to use.

Turning in your assignment

Use the handin program with program number 6. Submit only the new files that you have added, not the ones that you got above. Also submit your modified tester. Be sure that it does a reasonable test suite. It should, at the very minimum, execute every line that you wrote.