Transcript HW#5

Homework #5:
Pointers, Dynamic Arrays and
Inheritance
By J. H. Wang
May 31, 2011
Part I: Hand-Written Exercises
1. Give at least three uses of the * operator.
Name and describe each use.
2. You know that an overloaded assignment
operator and a copy constructor are not
inherited. Does this mean that if you do not
define an overloaded assignment operator or a
copy constructor for a derived class, then that
derived class will have no assignment operator
and no copy constructor? Please explain your
reasons.
Part II: Programming Exercises
3. Using dynamic arrays, implement a polynomial class with
polynomial addition, subtraction, and multiplication.
Discussion: A variable in a polynomial does nothing but act as a
placeholder for the coefficients. Hence, the only interesting thing
about polynomials is the array of coefficients and the
corresponding exponent. Think about the polynomial x*x*x + x+ 1.
Where is the term in x*x? One simple way to implement the
polynomial class is to use an array of doubles to store the
coefficients. The index of the array is the exponent of the
corresponding term. If a term is missing, then it simply has a zero
coefficient.
(There are techniques for representing polynomials of high
degree with many missing terms. These use so-called sparse
matrix techniques. Unless you already know these techniques, or
learn very quickly, don’t use these techniques.)
(To be continued on the next slide…)
(…Continued from the previous slide)
(1) Provide a default constructor, a copy constructor, and a
parameterized constructor that enables an arbitrary polynomial to be
constructed.
(2) Supply an overloaded operator = and a destructor.
(3) Provide these operations:
polynomial+polynomial, constant+polynomial, polynomial+constant,
polynomial-polynomial, constant-polynomial, polynomial-constant,
polynomial*polynomial, constant*polynomial, polynomial*constant,
(4) Supply functions to assign and extract coefficients, indexed by
exponent.
(5) Supply a function to evaluate the polynomial at a value of type
double.
You should decide whether to implement these functions as
members, friends, or standalone functions.
4. Define a class named Payment that contains a member
variable of type float which stores the amount of the
payment and appropriate accessor and mutator
functions. Also create a member function named
paymentDetails that outputs a sentence describing the
amount of the payment.
Next define a class named CashPayment that is
derived from Payment. This class should redefine the
paymentDetails function to indicate that the payment is
in cash. Include appropriate constructor(s).
(To be continued on the next slides…)
• (… continued from the previous slide)
Define a class named CreditCardPayment that is
derived from Payment. This class should contain
member variables for the name on the card,
expiration date, and credit card number. Include
appropriate constructor(s). Finally, redefine the
paymentDetails function to include all credit card
information in the printout.
Create a main function that creates at least two
CashPayment and two CreditCardPayment objects
with different values and calls to paymentDetails
for each.
Homework Submission
• Due: 2 weeks (June 14, 2011)
• Hand-written exercises
– Please write your names and answers on
papers.
• Programs
– Please submit to homework submission Web
site: http://140.124.183.39/oop/