CompSci 141 / CSE 141 / Informatics 101 Spring 2013
Project #4

Due date and time: Friday, May 24, 11:59pm


Introduction

This project will encourage you to develop your understanding of how the primary object-oriented features — classes, inheritance, and polymorphism — of languages like C++ and Java are implemented. It will also provide you with some exposure to C++ (which I know will be new to many of you) at two levels of abstraction: the object-oriented level of classes and objects, and the lower-level abstractions of structures and function pointers.

You will be given a program written in an object-oriented style in C++, and will be asked to reimplement it, without design changes, using the lower-level abstractions. The goal is not to write it to be simpler, clearer, or more efficient; the goal is to directly implement the provided object-oriented program in terms of underlying abstractions.


Understanding the original example

You'll be writing code that emulates the object-oriented features (classes, inheritance, and polymorphism) that are normally provided automatically by C++. Your program will be a re-implementation of an original C++ example (OOPorig) that's implemented using object-oriented features. Before you can write your program, you should spend some time reading and understanding the example, since that's the code you'll be emulating. I've heavily commented the code, since I'm well aware that many of you have little or no experience with C++. With this and plenty of help along the way, you should be able to adapt quickly. Naturally, I'll gladly answer further questions about it in person or via email, and we're also glad to help during discussion and lab. To keep things relatively straightforward, I haven't used any C++ features that don't have an analogue in Java.

Because of the way I've written and commented the code, it's wise to read and understand it in the following order. Comments in the later files in this list assume that you've read and understood the comments and code in the previous ones.

  1. main.cpp
  2. Shape.h
  3. Shape.cpp
  4. Circle.h
  5. Circle.cpp
  6. Rectangle.h
  7. Rectangle.cpp
  8. Square.h
  9. Square.cpp
  10. RightTriangle.h
  11. RightTriangle.cpp

I suggest adding some additional code to the main() function in this example, just to be sure that you understand how the provided example behaves. This will also help you to ensure that you know how to create objects and use the object-oriented features of C++, which may well prove to be handy knowledge to have.

Even if you're not going to start writing your code right away, take some time to read through this code and ask questions about it early, so that you can get the help you need to understand it, while still leaving yourself enough time to write your program.


Re-implementing the example without OOP features

Your main task for this assignment is to re-implement the OOPorig example in C++, without using classes, inheritance, or polymorphism. Instead, you'll be implementing lower-level code that handles the various details we discussed in lecture, such as virtual method tables. I've provided a starting point called OOPemu that you're required to use. Given to you is part of the main() function, as well as an emulation of the Shape class (heavily commented) and part of an emulation of the Circle class. Since the provided code for OOPemu is heavily commented, it should be clear what's been given and what you'll need to add. You'll need to complete the Circle class, as well as implement the Rectangle, Square, and RightTriangle classes. Additionally, you're required to add code to your main() function that creates various kinds of shapes and demonstrates that your emulation of inheritance and polymorphism are working correctly, at least by calling each method on each kind of object using dynamic binding (by looking up the method in the virtual method table).


The logistics of using C++ for this assignment

Background information

One of the nice things about using Java in an educational context is that the "Write once, run everywhere" mantra is almost entirely true. For the kind of work we do in most undergraduate courses, we rarely, if ever, expose any incompatibilities between virtual machines on different platforms. That means that we can tell you "Write your code in Java," allow you to work on any platform and with any development tools you want, and have a reasonably good expectation that everything will turn out fine in the end.

When we use C++, however, this advantage disappears. Since different implementations of C++ differ in some pretty fundamental ways — including what the width of an int variable is! — it's necessary for us to make an agreement up-front about what platform and what compiler we intend to use for the course.

Support for C++ in the ICS labs

The good news is that there's a very good (and reasonably standards-compliant) environment available for use — free! — in the ICS labs. It's called Visual Studio 2012. I don't expect most of you have used this tool before, so I've written a brief tutorial that explains how you can use it for the tasks that you'll need to do for this assignment.

Both chunks of provided code — OOPorig and OOPemu — are provided as Visual Studio 2012 solutions. All of the code that I've written is standard C++, so it should work just as well on other platforms and using other (standards-compliant) compilers as it does with Visual Studio 2012. Since Visual Studio 2012 is only available on Windows, you may prefer a different development environment; in general, that's fine. Still, it's important to note that C++ implementations can vary in some pretty fundamental ways, so we'll require that your code compiles and executes correctly using Visual Studio 2012. Even if you choose to use something else for your work, verify that your program works with Visual Studio 2012 before submitting it.


Deliverables

You need to submit all of the source and header files, including those provided, that comprise OOPemu. Please submit only your source code (.cpp and .h files) — do not submit compiled versions of your program, or other files generated by your development environment. Also, do not under any circumstances submit the code from OOPorig; that's provided to you for reference, but will confuse us in the grading process.

Follow this link for a discussion of how to submit your project. Remember that we do not accept paper submissions of your projects, nor do we accept them via email under any circumstances.