Assignment 2

A Basic Templated Numeric Type Class

Due Date: September 23, 2009

Required for submitting:

    • Grade Sheet Print this sheet and bring it to class the day the program is due.

    • Bring your UML diagram to class. You may hand draw this first one, but please use dia or visio in the future. Also, be sure to include all the data and function attributes in your class diagrams.

Purpose:

  • This assignment is designed to refamiliarize you with writing OO code in C++, and in particular defining and implementing good class design. Some of the concepts addressed in this assignment may be review for you and some may not. But they are very basic to our OO design for numerical OOP. Thus, you are going to be using:

      1. templated classes

      2. templated functions

      3. overloading operators

      4. the STL, in particular the vector class

      5. the concept of possibly implementing algorithms as classes

      6. C++ to "wrap" a data type

Resources:

The Concept:

  • One of our goals this semester is to attain as much abstraction as possible in our data representation. To that end, we are first going to consider the concept of a "wrapper" for a fundamental numeric type. The aim is to hide many of the implementation details from the user of the type. A programmer doesn't need to know (or want to know) exactly what is happening when two integers or floats or complex numbers are added, multiplied, or discombobulated. They simply want the results of such actions. This assignment will have you "wrap" the ordered polar pairs:

    • p = (r, θ)

  • Your abstraction will be implemented with a class, of course. And, of course, your implementation should be transparent. This means that the functionality you build into your class should reflect the expected behavior of your type and that of the C++ primitives (int, float, etc.). So, in addition to the usual functions of a well defined and socially well adjusted class, you will need to overload operators so that operations such as

    • p1 = p2 / p3

  • make sense and indeed work. In addition, you want this type to be able to have data types of varying kinds. That is, you want the ability to have modulus (r) and argument (θ) parts that are int or float or double or etc. So, you will template the class.

    • Once you have your polar class, you will need to demonstrate how well it works. To do this, we want you to concurrently learn a little mathematics and to demonstrate your programming prowess by coding and applying a norm. Ok, so what is a norm? Loosely speaking, a norm is a way of measuring on a set. A norm is a real-valued function of a set of things. The set of things you will be concerned with is a set of polar pairs. The norm you will implement is the 1-norm and is defined to be the sum (over the set) of the magnitudes of the elements in that set. For your polars, the magnitude is the same as the absoluted value of the modulus, |r|. So, you must "pass" to your implementation of the norm a set of objects on which to operate. What, then, is the relationship between objects and operations here? This is your problem. You need to think carefully about your design. Use your UML. I will pass out in class some sample code and UML to help you get your head around this idea. So you need to think about what class depends on what other class.

    • It is possible to implement your norm operation either as a function or as a class. However, we will require that you implement it as a class for this assignment. The concept of representing an operation as a class may be foriegn to you. But the method has definite advantages. It boils down to this: define a class with no data and one (public) overloaded operator, ( ). Using the class object then appears to be the use of a function. I will speak about this in class.

    • We want you to use the STL vector class to contain your set of objects (polar pairs). Then you will be able to do this

    • vector<polarNum<int> > vect;

  • and have a vector of polar values whose components are ints. Note: be careful to put that extra space between the two > symbols.

Program Specifications:

  • You will include in your submission source code for your templated polar values class. You will include a default constructor and a constructor to initialize p (p will be the name we will give to a polar pair value) with passed values. You will also have a copy constructor and a destructor. You will overload the operators - (unary, for negation), *, /, ~ (the magnitude), ! (for conjugate), = (assignment), [ ] (with 0 for modulus, 1 for argument), <, >, = =, !=, stream operators. You will need get and set functions. Below I will elucidate on the mathematics you will need to know. You will submit your implementation of the 1-norm. You will submit a driver to test your constructs. The driver will read data values from a file that I will provide for you: DATA SET (Note: the first value in this file is the number of data pairs that are to follow. The first value in each pair is the modulus (r) and the second is the argument (θ). There is white space between each number. Have your program read data according to this file format. Your program will possibly be tested on other data sets using this format.) Output the quotient of the first polar value divided by the second, the product of the third and fourth values, the conjugate of the fifth value. Also apply the 1-norm to the entire data list and output the result. You will also submit during class a UML model of your code (hand-drawn if you like). Also submit during class the gradesheet. Be sure also to provide a makefile.

    • with p = (r, θ), we then have: -p = (-r, θ) p1*p2 = (r1*r21 + θ2) p1/p2 = (r1/r21 - θ2) ~p = |r| !p = (r, -θ)

    • For this submission, we also want you to run a script for this program:

      1. In the directory in which you have your source, type script output

      2. Run your make and execute your code

      3. Make clean

      4. Type exit

      5. Print the contents of the file named output using a2ps (a2ps filename | printer_name)

      6. Submit the printed output in class the day the assignment is due

    • One more thing. Make your driver accept a command-line argument for the name of the file from which to read data. This makes it easy for the grader.

Deliverables:

    1. Code: I will test that the code you submit does indeed compile and run. You are to copy all your source code along with a makefile into your cs328 directory. How to do this is explained in Submission procedure.

    2. makefile: Supply the makefile which compiles and builds your program as detailed above. I really suggest that you play around with and use the third of the makefiles I posted.

  1. Script printing