2.4 Overloaded Operations




In some situations, frequently with constructors, a class may provide alternative ways of performing the same operation. These alternatives are defined as methods with the same name but with different argument lists. Here is the Frame class again with overload constructors and an overloaded method:
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.


Next Stop


Exercises

  1. Construct a window using the constructor with no arguments and see what name it chose for the name of the window. Create two such windows and compare their names.

  2. Shrinking Window 1: Write a program that places a window of size 500 by 500 near the center of the screen. Leaving its position fixed, resize the window 19 times, each time making it smaller by an additional 25 units. Use the version of Resize that takes two integer arguments.

  3. Shrinking Window 2: Write a program that places a window of size 500 by 500 near the center of the screen. Leaving its position fixed, resize the window 19 times, each time making it smaller by 10 percent of its current size. Use the version of Resize that takes a single float argument.

  4. Heart Beat 1: Write a program that first shrinks and then expands a window back to its original size using the approach in the Shrinking Window 1 problem.

  5. Heart Beat 2: Write a program that first shrinks and then expands a window back to its original size using the approach in the Shrinking Window 2 problem.

Last Updated: May 21, 1996 / kafura@cs.vt.edu