3.7 Self Referencing Class Definitions


The definition of a class may refer to itself. One situation in which this occurs is when a class's operation has an object as a parameter where that object is of that same class as the one containing the operation. Examples of where this occurs are the following:

  • A Location object is to decide if it has the same screen coordinates as another Location object
  • A Shape object is to decide if it has the same height and width as another Shape object
  • A File object is to copy itself to or from another File


In each case the operation needs as its parameter an object in the same class as the one containing the operation.

A second situation in which a class may refer to itself occurs when a class's method returns an instance of that class as a result. Some examples of methods that return objects in their own class are the following:

  • A Shape object returns a new Shape object whose dimensions are some percentage less or more than the size of the original Shape object

  • A Location object returns a new Location object that is horizontally or vertically offset from the original Location object

  • A File object returns a new File object that represents a temporary copy of itself

The File class is extended to add a method that will perform the copying operations described above. The extended definition is:

Extended File Class
 class File {                               // Version 2
    private:
                      // encapsulated implementation goes here
    public:

          File(char* fileName);               // represents file with given name
          File();                             // unknown, as yet, file
    char* Name();                             // reply name of file
    int   Exists();                           // does file Exist?
    void  View();                             // scrollable view window
    void  Edit(char* editor);                 // edit file using "editor"
    void  Delete();                           // delete file 
    void  CopyTo(File& other);                // copy me to other
    void  CopyFrom(File& other);              // copy other to me
         ~File();                             // free name
};

In this revised version of the File class, the CopyTo and CopyFrom methods take as their input arguments references to other File objects, which the called object copies itself to or from. This class can be used in the following way:

  FileNavigator nav;
   File sourceFile = nav.AskUser();
   File targetFile = nav.AskUser();

   sourceFile.CopyTo(targetFile);
   sourceFile.View();
   targetFile.View();

In this example the user is asked to select two existing files. The file first selected is copied to the second file. The two viewing windows that are created can be used to visually confirm that the files are identical.

As noted in the examples above, it is also useful for an operation of a class to return an object of that same class. This is illustrated by the following revisions to the Location and Shape classes:

Revised Location Class
  class Location {                     // Version 2
     private:  
        // encapsulated implementation goes here
     public:
              Location(int x, int y);   // specific location
              Location();               // default location
     int      Xcoord();                 // return x-axis coordinate
     int      Ycoord();                 // return y-axis coordinate
     Location Xmove(int amount);        // move right/left
     Location Ymove(int amount);        // move up/down
   };

Revised Shape Class
   class Shape {                        // Version 2
     private:  
        // encapsulated implementation goes here
     public:   
           Shape(int width, int height);// specific shape
           Shape();                     // default shape
     int   Height();                    // return height
     int   Width();                     // return width
     Shape Resize(float factor);        // return adjusted shape
   };

Using these revisions to the Shape and Location class we can operate on a window as follows:

  Frame window(nearTop, largeSquare);
   
   Shape currentShape       = largeSquare;
   Location currentLocation = nearTop;
   Shape    newShape        = currentShape.Resize(0.9);
   Location newLocation     = currentLocation.Xmove(50);
  
   window.MoveTo( newLocation );
   window.Resize( newShape    );


In this example a window is made smaller by 10 percent and moved to a location that is fifty units to the right of its starting location.

Tasks

  1. Redefine the Location class to include a method SameAs that decides if the called Location object has the same screen coordinates as another Location object.
  2. Redefine the Shape class to include a method SameAs that decides if the call Shape object has the same height and width as another Shape object.
  3. Redefine the Location class to include another useful method that has a Location object as its parameter. Explain briefly what your new method does.
  4. Redefine the Shape class to include another useful method that has a Shape object as its parameter. Explain briefly what your new method does.

 




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

Legal Statement

 

 

 

ÿ