BIG C++
Cay Horstmann & Timothy Budd

Laboratory Notebook

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.

#### Lab Objectives

To gain experience with

• categories of operators and their uses

#### P1. Polynomial Representation

A simple polynomial is an important mathematical function. Consider a polynomial represented in the following manner: anxn + an-1xn-1+a0. This representation of a polynomial can be easily implemented using a C++ class assuming we place some constraints on the polynomial itself. Assume, for this lab, that n is always fixed at three and represents a third order polynomial. Construct a class called PolynomialThree that consists of the minimum necessary private data values required to represent this polynomial. Also, be sure to implement a default constructor that initializes a constant polynomial with value zero, a single parameter constructor to initialize a constant polynomial, and a four parameter constructor to initialize all parameters of the polynomial. Use double-precision numbers to represent the four polynomial coefficients.

Summary:

• Create a class called PolynomialThree to represent a third order polynomial using floating point numbers to represent the four coefficients
• Create three constructors
• Single parameter constructor to initialize a constant polynomial
• Default constructor to initialize a zero-valued constant polynomial
• Four parameter constructor to manually set all polynomial parameters

Submit all of the code for your answer in the space below.

R1. Operator Categories

Part of successfully employing overloaded operators is knowing which type should be used for your particular application. There are a number of different categories of operators. These include: simple arithmetic, comparison, input and output, increment and decrement, assignment, conversion, etc. In the space below, describe what operators might be useful for the polynomial class from Problem P1. Name as many as you think is necessary, without implementing any of them.

#### P2. Programming Polynomial Operations

You will now implement some operations in C++.  First, implement the addition and unary negation operations using public member functions. Then, add two nonmember overloading functions that utilize these member functions. Submit the four functions below using correct syntax. Write your code in such a way that the member functions take as input and return as output PolynomialThree objects only.

Implement a subtraction member function using the addition and negation functions just created. Implement an overloaded nonmember operator that utilizes this subtraction member function. Be sure to write the subtraction member function such that the inputs and returned output are objects of type PolynomialThree.

R2. Comparisons

When overloading comparison operators, it is often very useful to base all such operators on a single member function. Such structuring greatly simplifies the definitions of the overloaded functions and provides a member function in case the user of the code is not comfortable with using the operator syntax. To illustrate, write a single member function that compares the order of two polynomials.

Use this syntax:

int PolynomialThree::compare(const PolynomialThree right) const;

Return a negative number if the calling polynomial’s order is less than the argument polynomial’s order, zero if the argument polynomial’s order is equal to the calling polynomial’s order, and a positive number if the calling polynomial’s order is greater than the argument polynomial’s order. Note that a PolynomialThree object with a coefficient of zero for the x3 term is actually a second order polynomial and should test less than any polynomial with a non-zero x3 term. A coefficient will be considered zero if it is less that 1E-5.

Write the necessary function below.

Overload three nonmember comparison operators, greater than, less than, and equal to, using the newly created comparison method from Problem R3. Add the three overloaded functions in the space below.