Assignment 2

A Basic Templated Numeric Type Class

Due Date: Friday, February 13, 2015

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 implementing operations 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 basic 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" cylindrical coordinate triples:

p = (r, θ, z)

If you don't know what cylindrical coordinates are, just imagine polar coordinates (you learned them in calc II) with a z-axis added for a 3-dimensional effect. This is something you missed by not taking calc III. Thus, if you need more help understanding cylindricals, just read your calc book.

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

make sense and indeed work. In addition, you want your cylindrical coordinates to be able to have numeric types of varying kinds. That is, you want the ability to have r, θ, and z parts that are int or float or double or whatever. So, you will template the class.

Once you have your cylindrical 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 cylindrical coordinates. 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 cylindricals, the magnitude is the distance from the origin to the point represented. 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 passed 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 foreign 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. If this concept gives you headaches or sweaty palms, I can talk about it in class if you ask.

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

vector<cylindricalNum<int> > vect;

and have a vector of cylindrical 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 cylindrical values class. You will include a default constructor and a constructor to initialize p (p will be the name we will give to a cylindrical coord value) with passed values. You will also have a copy constructor and a destructor. You will overload the operators - (so that -p is the reflection thru the origin or pole), ! (so that !p is the reflection thru the z-axis), ~ (where ~p is the magnitude of p), = (copy assignment), [ ] (with 0 for r, 1 for θ, 2 for z), <, >, = =, !=, stream operators. Define the < and > operators so that p1 < p2 if p1 is closer to the origin than p2. The == operator returns true if the two operands describe the same point in space. You are also to include a function which returns the coordinates converted to cartesian values (x, y, z) as a string type. 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. Here is a data set you may use for testing. It is in the format you are to assume: DATA SET (Note: the first value in this file is the number of data pairs that are to follow. The first value in each ordered triple is the r-value, the second is the θ-value, and the third is the z-value. There is white space between each number. Have your program read data according to this file format. Your program will be tested on other data sets using this format.) Output the polar reflection of the first point, the z-axis reflection of the second, and comparison of the third and fourth points using the < or > operator, and the conversion to cartesians of the fifth point. 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.

Deliverables:

    1. Code: I will test that the code you submit does indeed compile and run. You are to use cssubmit to submit your code and makefile. How to do this is explained in Submission procedure.

    2. makefile: Supply the makefile which compiles and builds your program as detailed above