• No results found

R EVISED P IZZA -B UYING P ROGRAM

In document C++ Basics (Page 156-160)

Overloading and Default Arguments

R EVISED P IZZA -B UYING P ROGRAM

The Pizza Consumers Union has been very successful with the program that we wrote for it in Dis-play 4.5. In fact, now everybody always buys the pizza that is the best buy. One disreputable pizza parlor used to make money by fooling consumers into buying the more expensive pizza, but our program has put an end to its evil practices. However, the owners wish to continue their des-picable behavior and have come up with a new way to fool consumers. They now offer both round pizzas and rectangular pizzas. They know that the program we wrote cannot deal with rectangu-larly shaped pizzas, so they hope they can again confuse consumers. Display 4.7 is another ver-sion of our program that compares a round pizza and a rectangular pizza. Note that the function name unitPrice has been overloaded so that it applies to both round and rectangular pizzas.

Display 4.7 Revised Pizza Program (part 1 of 3)

1 //Determines whether a round pizza or a rectangular pizza is the best buy.

2 #include <iostream>

3 using namespace std;

4 double unitPrice(int diameter, double price);

5 //Returns the price per square inch of a round pizza.

6 //The formal parameter named diameter is the diameter of the pizza 7 //in inches. The formal parameter named price is the price of the pizza.

8 double unitPrice(int length, int width, double price);

9 //Returns the price per square inch of a rectangular pizza 10 //with dimensions length by width inches.

11 //The formal parameter price is the price of the pizza.

12 int main() 13 {

14 int diameter, length, width;

15 double priceRound, unitPriceRound,

16 priceRectangular, unitPriceRectangular;

17 cout << "Welcome to the Pizza Consumers Union.\n";

18 cout << "Enter the diameter in inches"

19 << " of a round pizza: ";

20 cin >> diameter;

21 cout << "Enter the price of a round pizza: $";

158 Parameters and Overloading

Display 4.7 Revised Pizza Program (part 2 of 3) 22 cin >> priceRound;

23 cout << "Enter length and width in inches\n"

24 << "of a rectangular pizza: ";

25 cin >> length >> width;

26 cout << "Enter the price of a rectangular pizza: $";

27 cin >> priceRectangular;

28 unitPriceRectangular =

29 unitPrice(length, width, priceRectangular);

30 unitPriceRound = unitPrice(diameter, priceRound);

31 cout.setf(ios::fixed);

32 cout.setf(ios::showpoint);

33 cout.precision(2);

34 cout << endl

35 << "Round pizza: Diameter = "

36 << diameter << " inches\n"

37 << "Price = $" << priceRound

38 << " Per square inch = $" << unitPriceRound 39 << endl

40 << "Rectangular pizza: Length = "

41 << length << " inches\n"

42 << "Rectangular pizza: Width = "

43 << width << " inches\n"

44 << "Price = $" << priceRectangular

45 << " Per square inch = $" << unitPriceRectangular 46 << endl;

47 if (unitPriceRound < unitPriceRectangular) 48 cout << "The round one is the better buy.\n";

49 else

50 cout << "The rectangular one is the better buy.\n";

51 cout << "Buon Appetito!\n";

52 return 0;

53 }

54 double unitPrice(int diameter, double price) 55 {

56 const double PI = 3.14159;

57 double radius, area;

58

59 radius = diameter/double(2);

60 area = PI * radius * radius;

61 return (price/area);

62 }

04_CH04.fm Page 158 Wednesday, August 13, 2003 12:49 PM

Overloading and Default Arguments 159

DEFAULT ARGUMENTS

You can specify a default argument for one or more call-by-value parameters in a func-tion. If the corresponding argument is omitted, then it is replaced by the default argu-ment. For example, the function volume in Display 4.8 computes the volume of a box from its length, width, and height. If no height is given, the height is assumed to be 1. If neither a width nor a height is given, they are both assumed to be 1.

Note that in Display 4.8 the default arguments are given in the function declaration but not in the function definition. A default argument is given the first time the func-tion is declared (or defined, if that occurs first). Subsequent declarafunc-tions or a following definition should not give the default arguments again because some compilers will consider this an error even if the arguments given are consistent with the ones given previously.

You may have more than one default argument, but all the default argument posi-tions must be in the rightmost posiposi-tions. Thus, for the function volume in Display 4.8, we could have given default arguments for the last one, last two, or all three parameters, but any other combinations of default arguments are not allowed.

Display 4.7 Revised Pizza Program (part 3 of 3)

63 double unitPrice(int length, int width, double price) 64 {

65 double area = length * width;

66 return (price/area);

67 }

SAMPLE DIALOGUE

Welcome to the Pizza Consumers Union.

Enter the diameter in inches of a round pizza: 10 Enter the price of a round pizza: $8.50

Enter length and width in inches of a rectangular pizza: 6 4

Enter the price of a rectangular pizza: $7.55 Round pizza: Diameter = 10 inches

Price = $8.50 Per square inch = $0.11 Rectangular pizza: Length = 6 inches Rectangular pizza: Width = 4 inches Price = $7.55 Per square inch = $0.31 The round one is the better buy.

Buon Appetito!

default argument

160 Parameters and Overloading

If you have more than one default argument, then when the function is invoked, you must omit arguments starting from the right. For example, note that in Display 4.8 there are two default arguments. When only one argument is omitted, it is assumed to be the last argument. There is no way to omit the second argument in an invocation of volume without also omitting the third argument.

Display 4.8 Default Arguments 1

2 #include <iostream>

3 using namespace std;

4 void showVolume(int length, int width = 1, int height = 1);

5 //Returns the volume of a box.

6 //If no height is given, the height is assumed to be 1.

7 //If neither height nor width is given, both are assumed to be 1.

8 int main( ) 9 {

10 showVolume(4, 6, 2);

11 showVolume(4, 6);

12 showVolume(4);

13 return 0;

14 }

15 void showVolume(int length, int width, int height) 16 {

17 cout << "Volume of a box with \n"

18 << "Length = " << length << ", Width = " << width << endl 19 << "and Height = " << height

20 << " is " << length*width*height << endl;

21 }

SAMPLE DIALOGUE\ Volume of a box with Length = 4, Width = 6 and Height = 2 is 48 Volume of a box with Length = 4, Width = 6 and Height = 1 is 24 Volume of a box with Length = 4, Width = 1 and Height = 1 is 4

Default arguments

A default argument should not be given a second time.

04_CH04.fm Page 160 Wednesday, August 13, 2003 12:49 PM

In document C++ Basics (Page 156-160)