Exercises, Second Set

3.Rewrite the DrugStockItem class that you wrote earlier in this chapter as a derived class of DatedStockItem, adding the new class to the polymorphic object implementation based on the StockItem class. The Reorder member function of the DrugStockItem class will be inherited from DatedStockItem, and the member function DeductSaleFromInventory will have to be made a virtual function in StockItem so that the correct version will be called via the StockItem* in the StockItem class. The resulting set of classes will allow the effective type of a StockItem object to be any of UndatedStockItem, DatedStockItem, or DrugStockItem.
4.Rewrite the Employee, Exempt, and Hourly classes that you wrote earlier in this chapter as a set of classes implementing a polymorphic object type. The base class will be Employee, with an Exempt class and an Hourly class derived from the base class. The resulting set of classes will allow the effective type of an Employee object to be either Exempt or Hourly, with the CalculatePay function producing the correct result for either type. To distinguish the effective types, you will need to write two different versions of the constructor for the Employee class. Do this by adding an additional argument of type double in the constructor that creates an Hourly worker object, specifying the multiplier used to calculate overtime pay. For example, a value of 1.5 specifies the standard “time-and-a-half for overtime” multiplier. You can declare a variable of type double just as you can a variable of any other type, e.g. double x; declares a variable named x of type double. Note that you must maintain the same interface for the CalculatePay function in these classes because you are using a base class pointer to access derived class objects.
5.What would happen if we tried to assign a StockItem object to itself using the operator = implementation in Figure 10.40 on page 727?
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset