The key element of an object's structure is its public interface. As with everything else about an object, the object's public interface is defined in the class from which the object was created. The public interface contains a set of methods (also called procedures, member functions, or operations) that can be used to manipulate the object. The only way to manipulate an object is by using the methods defined in the object's public interface.
An object also contains a private (hidden, internal, encapsulated) implementation. The user of an object has no concern with how the object is implemented, just as the driver of a car has no concern with how the engine is constructed. The internal details of an object's implementation can be ignored by the user of the object.
In C++ the public and private portions of a class are denoted by keywords as shown in the following:
class Frame { private: // encapsulated implementation goes here public: // public interface goes here };
The separation of the public interface from the private implementation is a major aspect of object-oriented programming. The public interface defines a contract between the object's implementor and the object's user. The contract specifies what service the implementor has agreed to provide. The contract limits the ways in which the user is allowed to manipulate the object.
The separation of public interface from private implementation has several advantages:
These benefits will be understood more deeply as the ideas of object oriented programming are developed.
The Frame class is the first of several abstractions of a graphical user interface window. A GUI window has numerous capabilities as shown in the figure below. A real-world GUI window has operation that can be performed on it to affects it placement and size, it is capable of displaying a wide range of information bearing elements (graphical, iconic, textual), and it affords a means of interacting with the user through control elements (buttons, menus, etc.). To avoid confronting all of these powerful capabilities in the first class being studied, a series of simpler abstractions will be shown. The figure below shows the first of these simpler abstractions.
Abstraction of a GUI Window |
---|
Consistent with the ideas of mapping an abstraction to a class, the first version of the Frame class is defined as follows:
class Frame { // Version 1 private: // encapsulated implementation goes here public: Frame (char *name, int initXCoord, int initYCoord, int initWidth, int initHeight); void Resize (int newHeight, int newWidth ); void MoveTo (int newXCoord, int newYCoord ); int IsNamed (char *name); void Clear (); void Write (char *text, int x, int y); void DrawLine (int x1, int y1, int x2, int y2); void DrawCircle (int x, int y, int radius); ~Frame (); };As shown in this example, the constructor method has the same name as the name of the class (Frame, in this case). This constructor requires five arguments giving the name to be associated with the Frame, the initial (x,y) coordinates of the Frame's upper left corner and the Frame's initial size (width and height). The last method, named ~Frame, is the destructor; placed here are any actions that the must be taken when the Frame object is destroyed. The IsNamed method is an accessor method that allows the program to determine if the name of the Frame matches the method's input argument. All other methods are manipulator methods. The two methods Resize and MoveTo provide operations to relocate the Frame on the screen and to change its size. The DrawLine and DrawCircle methods allow simple graphical shapes to be drawn in the Frame. The method Write allows text to be written into the Frame at the position within the Frame specified by the (x,y) coordinates. The Clear method erases all text and graphics from the Frame. As noted, a real Frame class would have more methods, but these suffice for a number of interesting examples.
Notice in the above definition of the Frame class, no executable code is given for any of the methods. This is in keeping with the desire to hide all of the implementation details of an object - the user of the class cares only what operations are defined and not how these operations are implemented. The creator of the class must, of course, write the code for these methods and provide the other private data as well. We will see later how this is done.
|
Exercises |
Last updated: July 15, 1996 / kafura@cs.vt.edu