|
|
 |
|
|
|
|
42: {
43: printWidth = width; // use parameter values
44: printHeight = height;
45: }
46:
47:
48: for (int i = 0; i<printHeight; i++)
49: {
50: for (int j = 0; j< printWidth; j++)
51: {
52: cout << *;
53: }
54: cout << \n;
55: }
56: }
57:
58: // Driver program to demonstrate overloaded functions
59: int main()
60: {
61: // initialize a rectangle to 10,20
62: Rectangle theRect(30,5);
63: cout << DrawShape(0,0,true)\n;
64: theRect.DrawShape(0,0,true);
65: cout <<DrawShape(40,2)\n;
66: theRect.DrawShape(40,2);
67: return 0;
68: } |
|
|
|
|
|
|
|
|
DrawShape(0,0,true)
******************************
******************************
******************************
******************************
****************************** |
|
|
|
|
|
|
|
|
DrawShape(40,2)
************************************************************
************************************************************
|
|
|
|
|
|
|
|
|
Analysis: Listing 13.2 replaces the overloaded DrawShape() function with a single function with default parameters. The function is declared on lines 11 and 12 to take three parameters. The first two, aWidth and aHeight, are ints; the third, UseCurrentValue, is a bool (true or false) that defaults to false. |
|
|
|
|
|
|
|
|
Analysis: The implementation for this somewhat awkward function begins on line 27. The third parameter, UseCurrentValue, is evaluated. If it is true, the member variables itsWidth and itsHeight are used to set the local variables printWidth and printHeight, respectively. |
|
|
|
|
|