Assignment 2

A (fairly) Basic Templated Numeric Type Class

Due Date: February 12, 2016

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 (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" polynomials:

    • p(x) = anxn + an-1xn-1 + ... + a2x2 + a1x + a0

  • 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, socially well adjusted, politically correct, and psychologically stable class, you will need to overload operators so that operations such as

    • p1 += p2

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

    • Once you have your polynomial 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 polynomials. The norm you will implement is the ∞ - norm (pr. "infinity norm") and is defined to be the maximum of the "magnitudes" of the elements in that set. For a polynomial, we will define its "magnitude" as the squareroot of the sum of the squares of the coefficients of that polynomial. 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 (polynomials). Then you will be able to do this

    • vector<polynomial_fnct<int> > vect;

  • and have a vector of polynomial values whose components are ints. Note: be careful to put that extra space between the two > symbols. Note: if you use c++11 in your compiler call, this isn't necessary.

Program Specifications:

  • You will include in your submission source code for your templated polynomial class. For that class, include a nested struct to represent a term of a polynomial. You may have other nested classes and/or structs. You will need to use dynamic memory allocation to store data for your polynomial. Also, you will include a default constructor and a constructor to initialize a polynomial as a monomial with passed values for coefficient and exponent. You will also have a copy constructor and a destructor. You will overload the following operators: (p is the name we'll use for an arbitrary polynomial)

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

      • unary - so that - p returns the negative of p

      • * for multiplying a polynomial by a constant

      • = for assignment

      • [ ] to return the monomial so indexed into p

      • ( ) the function evaluation operator so that you can evaluate a polynomial at a specific value of x

      • <<

      • ~ to return its polynomial argument with all coefficients made negative

  • You will submit your implementation of the ∞ - 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:

    • 7 2 -3 2 6.2 4 2 4.5 0 -5 3 2 -3 2 1 101 4 8 0 -31 3 15 5 -20 6 2 -10 0 102 4 3 4 2 4 7 4 9 9 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 22

  • (Note: the first value in this file is the number of polynomials that are to follow. Each polynomial is a set of ordered pairs (coefficient and power) preceded by the number of terms in that polynomial. White space will delimit each value in the file. Have your program read data according to this file format. Your program will be tested on other data sets using this format.) Output the first two polynomials and then output p1 + = p2, - p1, p1(-1), 4 * p3, ~p4(2). Also apply the ∞ - 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 grade sheet. Be sure also to provide a makefile.

    • Submit your driver with classes templated on floats. Name your executable "driver". Also, the driver should take one argument from the command line (using argv/argc), the name of the file to read in data.

    • Optional: If you wish to expand this assignment w/o reward, do the following. Add a member function that calculates and returns the roots of any polynomial of degree 2 or less. You will need to include in your poly class nested classes for complex_roots and real_roots , exception handling for attempting to find roots of polynomials of degree greater than 2 and for any polynomial that has no roots, and a complex class for expressing any complex roots.

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 using cssubmit (or git). 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.