Consider the revised pizza buying program of Display 4.7. This program provides the following overloading for unitPrice functions for round and rectangular pizza:
double unitPrice(int diameter, double price);
double unitPrice(int length, int width, double price);
Suppose we are faced with the need for the unit price on a square pizza. The problem here is to devise in a ‘natural’ way to overload unitPrice to compute the price per square inch of a square as well as a round pizza?
There is no nice way to do this. A simple minded approach is to write
double unitPrice(int edge, double price);
This attempt involves having two overloaded unitPrice function versions both of which have argument lists with types int and double, which results in an ability.
A student might overload using two doubles for arguments:
double unitPrice(double edge, double price);
This leads to the problem of remembering always to use a double for the size (edge length) of a square pizza and an int for the size (diameter). This tends to be error prone.
We are faced with writing a function with a different name. (Of course, the easiest way to solve the problem of finding a unit price for a square pizza is to call the function for rectangular pizza, passing the edge size to the length and the width parameters, but that begs the question of this test question: how to overload in a natural way.)
You might also like to view...
If the value of testScore is ____, the condition below produces a value of false. testScore < 0 || testScore > 100
A. -100 B. -1 C. 0 D. 101
The purpose of a multivalued field is to replace the display of a number such as an ID (or other foreign key value) with something more meaningful, such as a name
Indicate whether the statement is true or false
A(n) ________ is a document on the World Wide Web that consists of an HTML/XHTML file and associated files that can contain multimedia
Fill in the blank(s) with correct word
Name 3 important operations that an iterator object can perform.
What will be an ideal response?