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?
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);
```
double unitPrice(int edge, double price);
double unitPrice(double edge, double price);
There is no nice way to do this. A simple minded approach is to write
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:
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...
A network host-based firewall is a software firewall installed on an existing machine that already has an operating system installed
Indicate whether the statement is true or false.
On an ATX motherboard, the 4-pin auxiliary power connector provides which of the following?
A. Additional power to onboard devices B. Additional power to PClx peripheral cards C. Additional power to the South Bridge D. Additional power to the CPU voltage regulator