|
|
|
|
|
|
|
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 13.2. |
|
|
|
|
|
|
|
|
LISTING 13.2 USING DEFAULT VALUES |
|
|
|
 |
|
|
|
|
1: //Listing 13.2 Default values in member functions
2: #include <iostream.h>
3:
4: // Rectangle class declaration
5: class Rectangle
6: {
7: public:
8: // constructors
9: Rectangle(int width, int height);
10: ~Rectangle(){}
11: void DrawShape(int aWidth, int aHeight,
12: bool UseCurrentVals = false) const;
13: private:
14: int itsWidth;
15: int itsHeight;
16: };
17:
18: //Constructor implementation
19: Rectangle::Rectangle(int width, int height)
20: {
21: itsWidth = width; // initializations
22: itsHeight = height;
23: }
24:
25:
26: // default values used for third parameter
27: void Rectangle::DrawShape(
28: int width,
29: int height,
30: bool UseCurrentValue
31: ) const
32: {
33: int printWidth;
34: int printHeight;
35:
36: if (UseCurrentValue == true)
37: {
38: printWidth = itsWidth; // use current class values
39: printHeight = itsHeight;
40: }
41: else |
|
|
|
 |
|
|
|
|
continues |
|
|
|
|
|