The syntax and and utility of default arguments is illustrated by the following example based on the Frame class.
class Frame { private: ... public: ... void Resize (int width = 100, int height = 150); ... };In this example, the width and height arguments are both given default values. To see the effect of these default values consider the following use of the Resize method:
Frame window (100, 100, 300, 400); ... window.Resize(200, 250); // case 1 window.Resize(200); // case 2 window.Resize(); // case 3In the first case the window is resized to be 200 X 250: since both parameters are given the default values are ignored. In the second case, the Frame object is resized to be 200 X 150: the default value of 150 is used for the absent second (height) argument while the supplied 200 is used for the first (width) argument. In the third case, the window is resized to be 100 X 200 since both default values are used.
Default values can also be used with constructors. For example the Frame class could be redefined as follows:
class Frame { private: ... public: Frame( char *name, int initXcoord = 50, int initYcoord = 50, int initWidth = 100, int initHeight = 100 ); ... };
This single constructor can be used in any of the following ways:
Frame display1("Fully Specified", 200, 200, 300, 350); Frame display2("No Height", 200, 200, 300); Frame display3("Position Only", 200, 200); Frame display4("No YCoordinate", 200) Frame display5("Name Only");
The Frame display1 is given a name and will be located at position (200,200) with a shape of 300 by 350. The Frame display2 is like display1 except that it has a default height of 100 as specified by the default value for initHeight. The Frame display3 is given a name and will be located at position (200,200) and it will have a shape of 100 by 100 as determined by the default values for initWidth and initHeight. The Frame display 4 is similar to display3 except that it has a different name and will be located at (200,50), the y coordinate of 50 being determined by the default value for the parameter initYcoord. Finally, display5 will have the name indicated and the default values will determine its position - (50,50) - and it shape - 100 by 100.
Notice that as with overloaded methods, the use of default parameters give the caller of a method added flexibility in how to invoke the method. The caller need only specify those parameters that are essential and allow the default values to apply for all other parameters. Callers who need more exact control of the methods arguments can override the default values by providing their own values.
Default values can be used when:
The rule for specifying default values is that a argument without a default value can not occur after (to the right of) an argument with a default value. The following example illustrates why this rule is needed:
class Cube { public: ... Resize( int height = 10, int width = 20, int depth); ... };Notice that this violates the rule for default arguments because the argument depth has no default and occurs after width which has a default value. Consider the following code that uses the Resize method:
Cube cube; ... cube.Resize(30, 50);This invocation of Resize could reasonably mean any one of three things:
cube.Resize(10, 30, 50); // uses height default cube.Resize(30, 20, 50); // uses width default cube.Resize(30, 50); // ERROR - no depth argumentThis ambiguous situation is avoided (both for the benefit of the compiler and the programmer) by the rule given above.
|
Exercises |