1
h14
CS32 S18
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h14: Polymorphism

ready? assigned due points
true Tue 05/15 08:00AM Tue 05/22 12:30PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores).


Reading: Polymorphism, PS 15.3

  1. (10 pts) Fill in the information in the header. The following are required to get the 10 "participation" points.
    • Filling in your name and umail address.

    Also: For paper submission PLEASE submit on ONE SHEET OF PAPER, double-sided if at all possible. If you must submit on two printed sheets write name on BOTH sheets and no staples, paperclips, or folded corners.

  2. On p. 868 in PS, in Display 15.12, line 17, there is a use of the overloaded operator < on two objects, one of type Sale and another of type DiscountSale. The definition of that operator appears on lines 25 - 28 of Display 15.10 on p. 866. On line 27, there is an invocation of first.bill() and an invocation of second.bill().

    1. (4 pts) For first.bill() in the case of the invocation in Display 15.12 line 17, where is the definition of the member function bill() that is invoked? Give the Display number, line number(s) and page number(s) of the textbook where it appears.
       
    2. (4 pts) For second.bill() in the case of the invocation in Display 15.12 line 17, where is the definition of the member function bill() that is invoked? Give the Display number, line number(s) and page number(s) of the textbook where it appears.
       
    3. (3 pts) The bill() member function is "special", meaning that the exact definition of the function used depends on what type of object it is invoked on - if it is an instance of Sale or DiscountSale, for example, which may not be known until run-time. What is the C++ keyword that is used in the source code on the definition of bill() that signals this so called run-time dispatch of the member function?
       
    4. (3 pts) Is bill() an example of a function with early binding or late binding?
       
  3. (6 pts) Assume we have a base class (e.g. Person) and derived class (e.g. Student), and there is some function such as toString() that is defined in both the base class and the dervied class. For example, suppose that:

    • for Person, toString returns the person's name, e.g. Chris Gaucho
    • for Student, toString returns the person's name and their perm number in parentheses. e.g. Chris Gaucho (1234567).

    We say that toString() is overridding in the derived class. However, in PS (15.3), Savitch makes a distinction between the two cases, one that is properly called overriding and another that should really be called redefinition. Most of the cases we've seen so far are really just redefinition. What is different, according to Savitch, in the case where this should be called overriding?
     
  4. For this question, review the concept of abstract classes discussed in lecture. Let's say I created a program that kept a calendar for a dog grooming business. Currently, my system has a list of every breed of dog and a calendar class for keeping track of all appointments. For this calendar class, the makeAppointment() function takes an abstract Dog class. Obviously each dog may not have the same exact grooming needs (for example, a maltese and a great dane will probably need different tanks).

    1. (5 pts) How does using this abstract dog class reduce potential code duplication?
       
    2. (5 pts) How would you handle customized information knowing that your calendar only has access to an abstract Dog class?
       
    3. (5 pts) Let's say through some horrific abomination of genetic experimentation, I create a brand new dog breed. What modifications would I need to make to the makeAppointment() function to account for this new breed?
       
    4. (5 pts) If I happened to have a client that is of subclass PoliceDog, and a PoliceDog had specific public methods search() and attack(), would the makeAppointment() function be able to use these functions to its advantage?