& Timothy Budd
Chapter 17 - Operator Overloading
Your email address:
Your student ID number:
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.
To gain experience with
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.
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.
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
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.
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
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.
P3. Comparison Operator Overload
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.
Overload the output stream operator in order to facilitate the printing of a
PolynomialThree class member to the screen. Try to
make the output look as neat as possible. Also, you should not print any terms
of the polynomial that have a zero coefficient. Use the definition of zero that
was given in Problem R2 above.
Enter your overload operator in the space below.
Do not forget to send your answers when you are finished.