2.4 Overloaded Operations

In some situations, frequently with constructors, a class may provide alternative ways to perform the same operation. These alternatives are defined as methods with the same name but with different argument lists. The revised Frame class with overload constructors and overloaded methods is shown in the following figure.

Frame Class - Version 2
									class Frame {                   // Version 2
          // encapsulated implementation goes here
        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 );
   void Clear();                                // clear entire frame
   void Clear(int x, int y, int w, int h);      // clear rectangular area
   void TextSize(char* text, int& w, int& h);   // convenience method
        ...     // 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.

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

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 overloads constructors gives the user of the class more flexibility in the amount of control needed over the construction of the object.

The Resize method shows how overloading is used for non-constructor methods. 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 while factors less than 1.0 cause the window to shrink in width and height.

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

The Clear method is also overloaded. The version of this method with no arguments clears the entire frame. The version of the Clear method with four arguments allows a rectangular area within the frame to be cleared without erasing anything outside of this rectangle. To make this overloaded Clear method usable for erasing text, a helpful method, named TextSize, is added to the Frame class that determines the width and height of the rectangular area occupied by a text string. For example, to find the rectangular dimensions of the text "Hello World," the TextSize method would be used as follows:

 int w, h;     // variables to hold the result
  exact.TextSize("Hello World", w, h);

Notice that the two integer arguments are output parameters; they are declared as references to integers (int&).

A more complete use of the overloaded Clear method is shown below. This segment of code erases from a Frame object one text string (the "Hello World" string), leaving a second text string visible (the string "This is Great!").

 Frame window("Clear Test", 100,100, 200,200);
  window.DrawText("Hello World", 20, 20);
  window.DrawText("This is Great!", 50,50);
  int w, h;
  window.TextSize("Hello World", w, h);
  window.Clear(20, 20, w, h);           // erase "Hello World"

Frame (Version 2) Simulation

This applet illustrates the concepts of object construction and object manipulation using version2 of the Frame class. The applet is divided by a vertical line into two areas. The left area, labelled "Class Space", contains an icon that represents version 2 of the Frame class. The Frame class icon is the only one which appears in this area in this version of the simulation; later versions will introduce additional classes. The right area, labelled "Object Space", contains icons that represent objects constructed from the Frame class. Many object icons may appear in the Object Space. The same icon is used to represent the Frame class and the Frame objects to visually indicate their relationship. This visual cue is important in later versions of the simulation where there are many classes and the relationship between an object and its class would not otherwise be clearly evident.

A Frame object may be constructed as follows. Move the cursor to the Frame icon in the Class Space and control-click (i.e., hold down the control key while clicking the mouse button). This will case a menu to appear that shows the signatures of the Frame's overloaded constructors. A second control-click will remove the constructor menu. Subsequent conrol-clicks will toggle the menu between its visible and hidden states. When the constructor menu is visible, click on one of the menu items and a dialogue box will appear. The dialogue box has one text box at the top to enter the name associated with the object to be constructed (similar to the variable that names the object in a program) and text boxes for each of the construtor arguments. Fill in each of the fields in the dialogue box and press the "Exec" button to construct the object. Two visible actions will occur when the object is constructed. First, an icon for the object will appear in the Object Space part of the applet. This icon will be labelled with the string given as the object's name in the constructor dialogue box. Second, a window will appear whose title, position, and dimensions correspond to those given in the constructor dialogue box. This part of the simulation emphasises that objects are created by using the constructor of a class and that objects correspond to real entities (in this case a visible window). The icon from a Frame object may be repositioned in the Object Space by dragging the icon (placing the cursor over the icon and holding the mouse button down while moving the mouse). The placement of the icon in the object space has no significance (i.e., moving the icon does not move the window corresponding to that icon).

A Frame object may be manipulated as follows. Move the cursor over the Frame icon in the Object Space and control-click to bring up a menu showing each method that can be applied to a Frame object. Clicking on one of the methods in the menu causes a dialogue box to appear. Enter the parameters of the method and click on the "Exec" button to execute the method on the selected object. The control-click toggels the visibility of the menu of methods. To see the mouse events that occur within the window corresponding to a Frame object, shift-click on the object's icon. This activates a small display that shows the current mouse coordinates when the cursor is within the window and the current mouse button state. A Frame object may be deleted by a control-shift-click (clicking while hold down both the control and shift keys) on the Frame object's icon.

The code to generate the objects visible in the Object Space can be seen by performing a control-click operation in the Object Space outside of an object's icon. This action displays a window in which the code is presented in the form required by the simple programming environment.



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

  2. Simple Button: Write a program that displays within a frame your first name toward the top and, near the middle, a circle around the text "Push Here". When the user clicks anywhere inside the circle, erase just your first name and replace it with your last name. Clicking again within the circle should cause your last name to be erased and your first name to be displayed. Subsequent clicks within the circle should alternate in this way showing either your first or last name.

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

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

  5. 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.

  6. 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.


©1998 Prentice-Hall, Inc.
A Simon & Schuster Company
Upper Saddle River, New Jersey 07458

Legal Statement