Previous Table of Contents Next


6.2.4. C with Classes Feature Details

Clearly, the most important aspect of C with Classes—and later of C++—was the class concept. Many aspects of the C with Classes class concept can be observed by examining a simple example from Classes: An Abstract Data Type Facility for the C Language (Stroustrup, 1980a):

   class stack {
       char    s[SIZE];      /* array of characters */
       char *  min;          /* pointer to bottom of stack */
       char *  top;          /* pointer to top of stack */
       char *  max;          /* pointer to top of allocated space */
       void    new();        /* initialization function (constructor) */
   public:
       void push(char);
       char pop();
   };

A class is a user-defined data type. A class specifies the type of the class members that define the representation of a variable of the type (an object of the class), specifies the set of operations (functions) that manipulate such objects, and specifies the access users have to these members. Member functions are typically defined elsewhere:

   char stack.pop()
   {
       if (top <= min) error(“stack underflow”);
       return *(--top);
   }

Objects of class stack can now be defined and used:

   class stack s1, s2;             /* two variables of class stack */
   class stack * p1 = &s2;         /* p1 points to s2 */
   class stack * p2 = new stack;   /* p2 points to stack object allocated
                                      on free store */
   s1.push(‘h’);                   /* use object directly */
   p1->push(‘s’);                  /* use object through pointer */

Several key design decisions are reflected here:

  C with Classes follows Simula in letting the programmer specify types from which variables (objects) can be created, rather than, say, the Modula approach of specifying a module as a collection of objects and functions. In C with Classes (as in C++), a class is a type. This is a key notion in C++. When class means user-defined type in C++, why didn’t I call it type? I chose class primarily because I dislike inventing new terminology and found Simula’s quite adequate in most cases.
  The representation of objects of the user-defined type is part of the class declaration. This has far-reaching implications. For example, it means that true local variables can be implemented without the use of free store (heap store, dynamic store) or garbage collection. It also means that a function must be recompiled if the representation of an object it uses directly is changed. See section 6.4.3 for C++ facilities for expressing interfaces that avoid such recompilation.
  Compile-time access control is used to restrict access to the representation. By default, only the functions mentioned in the class declaration can use names of class members. Members (usually function members) specified in the public interface—the declarations after the public: label—can be used by other code.
  The full type (including both the return type and the argument types) of a function is specified for function members. Static (compile-time) type checking is based on this type specification. This differed from C at the time, where function argument types were neither specified in interfaces nor checked in calls.
  Function definitions are typically specified elsewhere to make a class more like an interface specification than a lexical mechanism for organizing source code. This implies that separate compilation for class member functions and their users is easy and the linker technology traditionally used for C is sufficient to support C++.
  The function new() a constructor, is a constructor, a function with a special meaning to the compiler. Such functions provided guarantees about classes. In this case, the guarantee is that the constructor—known somewhat confusingly as a new function at the time—is guaranteed to be called to initialize every object of its class before the first use of the object.
  Both pointers and non-pointer types are provided (as in both C and Simula).

Much of the further development of C with Classes and C++ can be seen as exploring the consequences of these design choices, exploiting their good sides, and compensating for the problems caused by their bad sides. Many, but by no means all, of the implications of these design choices were understood at the time Classes: An Abstract Data Type Facility for the C Language was written (dated April 3, 1980). This section tries to explain what was understood at the time and give pointers to sections explaining later consequences and realizations.


Previous Table of Contents Next