class Frame { // Version 2 private: // encapsulated implementation goes here public: Frame (char *name, int initXCoord, int initYCoord, int initWidth, int initHeight); Frame (char *name, int initXCoord, int initYCoord); Frame (char *name); Frame (); void Resize ( int newHeight, int newWidth ); void Resize ( float factor ); ... // other methods the same as in version 1 };The first constructor requires five arguments - one for the name, two for the placement and two for the size. The second constructor specifies only the name and the placement arguments. When this constructor is used, the two arguments determine where the Frame is placed on the screen, but the object selects, by an algorithm or by a simple default, the size of the Frame. In the third constructor, the user provides the name for the Frame but the constructor itself determines, by an algorithm or by simple defaults, both the placement and size of the Frame. Finally the last constructor, with no arguments, allows the object to select its own name, its own placement and its own shape.
The Resize method is also overloaded. The version with two integer parameters resizes the window to the specified width and height. The second version of Resize changes the window's dimensions by a given factor. Factors larger than 1.0 cause the window to expand in both width and height. Factors less than 1.0 cause the window to shrink in both width and height.
Examples of using the overloaded constructors are:
Frame exact ("First Window", 50, 50, 100, 200); // uses first constructor Frame here ("Second Window", 50, 50); // uses second constructor Frame simple ("Third Window"); // uses third constructor Frame any; // uses fourth constructor
The Resize method is used in the following ways:
exact.Resize(100, 100); // change to a 100 X 100 square exact.Resize(1.5); // enlarge by 50% exact.Resize(0.5); // shrink to 50% current size
Overloaded constructors are useful in cases where common default values or easily computed values are the common case. Thus, the user of the object is spared the burden of specifying information that is typical or that is not important. A set of overload constructors gives the user of the class more flexibility in the amount of control the user needs over the construction of the object.
|
Exercises |