Exercises

1.Suppose that the store using our inventory control program adds a new pharmacy department. Most of their items are nonprescription medications that can be handled with the DatedStockItem class we already created, but their prescription drug items need to be handled more carefully. This means that the member function DeductSaleFromInventory has to ask for a password before allowing the sale to take place. Create a new DrugStockItem class that enforces this new rule without using inheritance.
2.The store also needs some way to keep track of its employees' hours so it can calculate their pay. We'll assume that the employees are paid their gross wages, ignoring taxes. These wages are calculated as follows:
  1. Managers are paid a flat amount per week, calculated as their hourly rate multiplied by 40 hours.

  2. Hourly employees are paid a certain amount per hour no matter how many hours they work (i.e., overtime is not paid at a higher rate).

Write an Employee class that allows the creation of Employee objects with a specified hourly wage level and either “manager” or “hourly” salary rules. The pay for each object is to be calculated via a CalculatePay member function that uses the “manager” or “hourly” category specified when the object was created. Use the short data type to keep track of the pay rate and the total pay for each employee for the week, assuming that only a whole number of hours can be specified and that wage rates are always expressed in dollars rather than dollars and cents.[28]

[28] We'll see how we could handle more realistic values for the pay and number of hours using another numeric data type in a later chapter.

3.Rewrite the DrugStockItem class from Exercise 1 using inheritance from the DatedStockItem class.
4.Rewrite the Employee class from Exercise 2 as two classes: the base Exempt class and an Hourly class derived from the base class. The CalculatePay member function for each of these classes should use the appropriate method of calculating the pay for each class. In particular, this member function doesn't need an argument specifying the number of hours worked for the Exempt class, while the corresponding member function in the Hourly class does need such an argument.
5.Rewrite the Employee class that you wrote in Exercise 2 as two classes: the base Exempt class and an Hourly class derived from the base class. To maintain the same interface for these two classes, the CalculatePay member function in both classes should have an argument specifying the number of hours worked. The implementation of the Exempt class will ignore this argument, while the Hourly implementation will use it.
6.Write an essay comparing the advantages and disadvantages of the two approaches to inheritance in the previous two exercises.[29]

[29] If you'll e-mail this essay to me, I might put it on my WWW page!

7.Reimplement the DatedStockItem class to use a long variable of the form YYYYMMDD, rather than a string, to store the date.[30] Is this a better approach than using a string? Why or why not?

[30] A long is just like a short, except that it can hold larger numbers. See the Glossary for details on this type.

8.Rewrite the operator << functions for DatedStockItem and StockItem so that the former function uses the latter to display the common data items for the two classes, rather than display all the data items itself.
..................Content has been hidden....................

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