Computer Science 197c - Programming in C++
Winter Session 2002
Brent Heeringa
heeringa@cs.umass.edu
Work Phone: 413.545.3616
Home Phone: 413.549.7321

Assignment 0

Assigned: 12.00 2 January 2002
Due: 17.00 9 January 2002

The best mechanism for learning a new language is to write code using that language. So, it seems reasonable that the first exercise should be a straight-forward programming assignment.

I've implemented a Rational number class that highlights a number of constructs available in the C++ programming language. I've also implemented a Complex number class in Java. This class contains the common algebraic operations addition, subtraction, multiplication, and division as well as exponentiation, equivalence, and formatting methods.

The assignment is to implement the Complex number class in C++ using a style similar to that given in the Rational class I've written. By style, I don't mean you must use the same conventions for bracket placement but rather, use the natural C++ conventions for implementing functions and operations. For example, use the operator overloading capabilities of C++ to implement the algebraic operations so that plus appears like

      const Complex operator+(const Complex& c) const {
         // logic goes here
      }
      
instead of
      const Complex Complex::plus(const Complex& c) {
        // logic goes here
      }
      

Make sure to overload the output stream, equality, and assignment operators. Also implement a copy constructor and any other supporting routines you deem necessary. You do not need to overload the <, >, ≤, and ≥ operators. Reuse the DivideByZeroException class I provide in the Rational example. I realize this is a pretty hefty assignment, but because the Rational number class is heavily documented and the syntaxes between the Rational and Complex implmentations are almost identifical, it should decrease the harshness and be a profitable assignment.

You can download all the necessary files standalone, in a zip file, or in a gziped tar file below.

Use the MainComplex.cc file to test your code. Turn in your Complex.h and Complex.cc files to me on 09 January 2001 by 17.00. This gives us Friday and Wednesday to talk about crazy stuff that happens. Feel free to write me email or drop by during office hours.

To compile the Rational number class (and it's necessary supporting files) on a unix variant, download the files to some directory. if you download a zip file use

      % unzip assignment0.zip
      
If a tgz file use
      % gzip -d assignment0.tgz
      % tar -xvf assignment0.tar
      
The C++ Rational number class will use all the files except for MainComplex.cc. However, we only need to compile the implementation (.cc) files. So do a
      % g++ -fhandle-exceptions Rational.cc DivideByZeroException.cc 
                                Cmpsci197cUtil.cc Main.cc -o main
      
This will compile all the files into an executable called main. Because oit does not have a recent version of C++, we must explicitly tell it to use exceptions. The best way to compile C++ is probably using a makefile. If you download the makefile into the same directory as the source and type
      % make
      
at the command line, this will compile the Rational number class into an executable called main. You can alter the makefile so that it can be used for the Complex number class as well. Feel free to use whatever compiler you desire if you don't like the g++ on oit.