Assignment 2

A Basic Templated Numeric Type Class

Due Date: Friday, Feb. 10, 2017

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 or some other electronic drafting tool 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:

  • templated classes

  • templated functions

  • overloading operators

  • the STL, in particular the vector class

  • the concept of implementing operations as classes

  • 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 numeric type. The aim is to hide many of the implementation details from the user of the type. A programmer doesn't need (or want) to know exactly what is happening when two integers or floats or complex numbers are added, multiplied, or disemboweled. They simply want the results of such actions. This assignment will have you "wrap" quaternions:

h = a + bi + cj + dk

Now, you may be wondering what in the world is a quaternion. You can learn much (enough) about them by using one of the links on this webpage. It is not this one

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 physiologically appealing class, you will need to overload operators so that operations such as

h1 += h2

make sense and indeed work. In addition, you want your quaternions to be able to be based in numeric types of varying kinds. That is, you want the ability to have the a's, b's, c's and d's be int or float or double or whatever. So, you will template the class.

Once you have your quaternion 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 of objects. It is a real-valued function of a set of things. The set of things you will be concerned with here is a set of quaternions. The norm you will implement is the L1 - norm (pr. "el 1 norm") and is defined to be the sum of the "magnitudes" of the elements in that set. For a quaternion, we will define its magnitude as the square root of the sum of the squares of the component coefficients (a, b, c, d in the above example) of that quaternion. Thus, 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 (in this case) and one (public) overloaded operator, the function evaluation operator, 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 (quaternions). Then you will be able to do this vector<quaternion<int> > vect;

and have a vector of quaternion values whose components are ints. Note: be careful to put that extra space between the two > symbols if you are not compiling with c++11.

Program Specifications: You will include in your submission source code for your templated quaternion class. You will include a default constructor (initializing to 0 + 0i + 0j + 0k) and another constructor taking 4 args to initialize the 4 parts of a quaternion. You will also have a copy constructor and a destructor. You will overload the following operators: (h is the name we'll use for an arbitrary quaternion)

  • + =, +, -, - =, = =, ! = to do the obvious

  • unary - so that - h is the negative of h

  • * for multiplying a quaternion by a quaternion

    • / for division of quaternions h1 / h2 = h1 * (h2)-1

  • = for assignment

  • [ ] to return the nth coefficient of the quaternion, with 0 returning the real part, 1 the i part, 2 the j part, and 3 the k part

  • stream operators

  • ! to return its conjugate

  • ~ to return the magnitude

You will also include a function called mag that returns the magnitude.

You will submit your implementation of the L1 - norm and 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:

10

1 2 3 4

0 -1 -2 -3

4 5 6 7

-2 0 0 3

1 -1 -1 1

8 8 8 8

0 0 0 0

.1 .2 .1 .1

2 0 0 0

0 1 0 1

(Note: the first value in this file is the number of quaternions that are to follow. Each quaternion is a set of ordered quadruplets. White space will delimit each value in the file. Have your program read data according to this file format. Your program may be tested on other data sets using this format.) Apply the L1 - norm to the entire set of quaternions and output the result. Also, output the first two quaternions and then output h1 + = h2, - h1, h1 * h3, ! h4, and (h5)-1 = 1 / h5. Include exception handling for division by zero, bad subscripting, and anything else clearly foolish. 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.

Submit your driver with classes templated on floats. Name your executable "driver".

Deliverables:

  1. Code: I will test that the code you submit does indeed compile and run. You are to submit all your source code along with a makefile in the usual way. 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.

†The term "basic" has different meanings for different people.