4.2 Defining the Implementation




Implementing a class consist of defining: The data is often referred to as the "state" variables or the "instance" variables of the class. The term "state" reflects the point of view that an object moves from one state to another in time as directed by the execution of its methods. For example, when the MoveTo method is applied to a Frame object, the object is moved from a state in which it is at one location to a state in which it is at a different location. The term "instance" reflects the fact that each object is an instance of the class: each instance of a given class being distinct from all other instances and possessing its own private data.

The encapsulated data in a class is accessible to the member functions of that class. Such access is allowed, of course, because the member functions and the data are parts of the same implementation: to be written, the writer of the code must know the details of the data. The data is, however, completely inaccessible to all non-member functions.

The Location class is used to illustrate the syntax and placement of the the data and the code for a class. The complete definition, including the implementation, for the Location class is given below.

   class Location {                         // Version 1
   private:

     int  currentX, currentY;

   public:

        Location(int x, int y);         // specific location
        Location();                     // default location
     int Xcoord();                      // return x-axis coordinate
     int Ycoord();                      // return y-axis coordinate
   };

   // the implementation follows

    Location::Location( int x, int y ) { currentX = x; currentY = y; }

    Location::Location () { currentX = -1; currentY = -1; }

int Location::Xcoord() { return currentX; }

int Location::Ycoord() { return currentY; }

Notice that the data is placed in the private portion of the class definition. It is natural to wonder why, if the data is hidden, it is textually visible in the class definition. Why not make the data hidden both conceptually and visually. The placement of the data in the class definition is a compromise in the design of the overall system software. This compromise simplifies the job of the compiler and linker at the expense of exposing to view (but not to access) the variables declared in the private section of the class definition.

The general syntax of the implementation of a member function is as follows:

   ReturnType  ClassName::memberFunctionName ( ArgumentList ) { Statements }
where
ReturnType
is the type of the value returned by the member function (e.d, int),

ClassName
is the name of the class of which this function is a member (e.g., Location),

memberFunctionName
is the name of the member function (e.g., Xcoord),

ArgumentList
is a list of the argument for this member function (e.g., x and y for the first constructor); the list is empty if there are not arguments.

Statements
the statements that define what the member function does when it is invoked.

The ClassName must be repeated on each member function. This is necessary because, with overloaded member functions, it is possible for two different classes to have member functions with the name name and arguments; only their class names distinguish these two different member functions. Notice also that the complete argument list must be given even though it may already have been given earlier in the class definition.


Next Stop


Last updated: July 24, 1995 / kafura@cs.vt.edu