|

Just
as non-class functions can have one or more default
values, so can each member function of a class.
The same rules apply for declaring the default values,
as illustrated in Listing 10.2.
Listing
10.2. Using default values.
1:
//Listing 10.2 Default values in member functions
2:
#include <iostream.h>
3:
4:
typedef unsigned short int USHORT;
5:
enum BOOL { FALSE, TRUE};
6:
7:
// Rectangle class declaration
8:
class Rectangle
9:
{
10:
public:
11:
// constructors
12:
Rectangle(USHORT width, USHORT height);
13:
~Rectangle(){}
14:
void DrawShape(USHORT aWidth, USHORT aHeight, BOOL
UseCurrentVals = Â FALSE) const;
15:
16:
private:
17:
USHORT itsWidth;
18:
USHORT itsHeight;
19:
};
20:
21:
//Constructor implementation
22:
Rectangle::Rectangle(USHORT width, USHORT height):
23:
itsWidth(width), // initializations
24:
itsHeight(height)
25:
{} // empty body
26:
27:
28:
// default values used for third parameter
29:
void Rectangle::DrawShape(
30:
USHORT width,
31:
USHORT height,
32:
BOOL UseCurrentValue
33:
) const
34:
{
35:
int printWidth;
36:
int printHeight;
37:
38:
if (UseCurrentValue == TRUE)
39:
{
40:
printWidth = itsWidth; // use current class values
41:
printHeight = itsHeight;
42:
}
43:
else
44:
{
45:
printWidth = width; // use parameter values
46:
printHeight = height;
47:
}
48:
49:
50:
for (int i = 0; i<printHeight; i++)
51:
{
52:
for (int j = 0; j< printWidth; j++)
53:
{
54:
cout << "*";
55:
}
56:
cout << "\n";
57:
}
58:
}
59:
60:
// Driver program to demonstrate overloaded functions
61:
int main()
62:
{
63:
// initialize a rectangle to 10,20
64:
Rectangle theRect(30,5);
65:
cout << "DrawShape(0,0,TRUE)...\n";
66:
theRect.DrawShape(0,0,TRUE);
67:
cout <<"DrawShape(40,2)...\n";
68:
theRect.DrawShape(40,2);
69:
return 0;
70:
}
OUTPUT:
DrawShape(0,0,TRUE)...
******************************
******************************
******************************
******************************
******************************
DrawShape(40,2)...
******************************************************
******************************************************
ANALYSIS: Listing 10.2 replaces the
overloaded DrawShape() function with a single function
with default parameters. The function is declared
on line 14 to take three parameters. The first two,
aWidth and aHeight, are USHORTs, and the third,
UseCurrentVals, is a BOOL (true or false) that defaults
to FALSE.
NOTE: Boolean values are those that
evaluate to TRUE or FALSE. C++ considers 0 to be
false and all other values to be true.
The
implementation for this somewhat awkward function
begins on line 29. 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.
If
UseCurrentValue is FALSE, either because it has
defaulted FALSE or was set by the user, the first
two parameters are used for setting printWidth and
printHeight.
Note
that if UseCurrentValue is TRUE, the values of the
other two parameters are completely ignored.
|