2.2 Structure of Classes and Objects

To use objects of an existing class it is necessary to understand something of their structure and operation, just as when learning to drive a car it is useful to understand something about the car's structure (where the driver sits, where the ignition key goes, etc.) and the car's operation (what the steering wheel does, what the accelerator pedal does, etc.).

Public vs. Private Parts

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 {
         // encapsulated implementation goes here
         // 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.

Methods in the Public Interface

The public interface may (and usually does) contain three different kinds of methods: If no constructor or destructor is defined, a default one is provided. The default constructor and default destructor do nothing.

An Abstraction for a GUI Window

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
          // encapsulated implementation goes here
        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.

Next Stop


  1. Illustrate the difference between the public interface and the private implementation in the following ordinary objects:

    For each ordinary object identify several operations in its public interface and name one or more things that are probably in its private implementation.

  2. For one of the ordinary objects in the last question show how the public vs. private separation results in the advantages cited above (i.e, reuse, flexibility, debugging, design).

  3. Write a class definition (without the private section) for a "stack of integers" class.

  4. Write a class definition (without the private section) for a "file of characters" class.

  5. Write a variation of the Frame class (version 1) that moves the Frame by a relative amount (i.e., up/down a given number of units and left/right a given number of units).

  6. Write a variation of the Frame class (version 1) that changes the size of the Frame by a fractional amount (e.g., makes it 50% bigger or 50% smaller).

  7. Write a variation of the Frame class (version 1) that allows rectangles and triangles to be drawn in the Frame.

Last updated: July 15, 1996 / kafura@cs.vt.edu