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.)

Computer Science & Information Technology

You might also like to view...

A "Range"

A) are the numbers that fall between the smallest and largest in the formula. B) refers to a group of adjacent or contiguous cells. C) cannot be selected with the mouse. D) is another word for the active worksheet.

Computer Science & Information Technology

Which of the Insert Table options allows you to select the number of rows and columns for a table by pointing to individual cells?

A) Insert Table B) Draw Table C) Import D) Table Grid

Computer Science & Information Technology