Lab #2: CDT, ADT, Polymorphism, and Object


Introduction

This lab is split into four parts. The first part concentrates on getting familiarized with the construction of concrete data types (CDT). The second part examines a ADT (abstract data types). And the third part deals with polymorphic code. Finally, the fourth part delves into objects. Both the latter two parts are optional.

1. Concrete Data Types (CDT)

CDTs are data types that the concrete data representations are transparent. In this problem, we'd like to experience with CDT via one example: the complex data type. Download this program to start with.

Now compile the code we offered you, you may see a bunch of warning messages emitted by the compiler. And partly due to the fact some source files are incomplete. Open the file complex.c, you may see some function bodies are filled by todo.

Your job is to fill in the code that marked as todo in source file complex.c. Feel free to add any new functions to this file and the header file complex.h.

After finishing the task, try to modify the client code in file main.c to see what are the problems of CDT (as we discussed in the class).

2. Abstract Data Types (ADT)

ADTs are data types that the concrete data representations are opaque. In this problem, we'd like to experience with ADT by rethinking our previous example: the complex data type. Download this program to start with.

Your job is to fill in the code that marked as todo in source file complex.c. Feel free to add any new functions to this file and the header file complex.h.

What's the difference between CDT and ADT? Can we access data representations directly in ADTs? Make some demos to support your conclusions.

3. Polymorphism

In this problem, your job is to implement a polymorphic data type tuple. Download this program to start with.

The newly added files are tuple.h and tuple.c. Your job is to filled the code that absent in file tuple.c. Note that in this problem, we'll also make use of the complex ADT as we did in problem 2. So you should first copy your code in problem 2 here.

We have suppied, in file main.c, some test code. After finishing the problem, write more test cases to test your code.

Question #1: How can we generalize the tuple ADT to a nTuple ADT? Recall that nTuple is of the form: (x_1, x_2, ..., x_n) and each element x_i (1<=i<=n) may be of different type.

4. (Optional) Object

In this problem, we will do some object-oriented programming. To be specific, we will implement both the complex and tuple ADTs in an object-oriented style, that is, each data item will carry function pointers. Download this program to start with.

Problem #1: The object layout we offered you in the code is more or less primitive, in the sense that it only supports virtual functions and dynamic method dispatch. Investigate how to modify the object layout so that it can support more object-oriented language features, such as RTTI (runtime type indentification), reflection, etc.. Then try to implement your ideas.

Problem #2: Could we modify the object layout we offered you in the code, to support inheritance or mutiple inheritance? Why or Why not?