3.6 Self Referencing Class Definitions




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

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 iself occurs when a method of a class returns an instance of that class as a result. Some examples of methods that return objects in their own class are the following:


In these examples the methods in the Shape, Location or File classes return another object in their same class.

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

  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 to 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:


   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
   };


   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    = window.WhatShape();
   Location currentLocation = window.WhereAt();
   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% and moved to a location that is 50 units to the right of its starting location.


Exercises


  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.


Last Updated: August 21, 1995 / kafura@cs.vt.edu