| Previous | Table of Contents | Next |
Clearly, the most important aspect of C with Classesand 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:
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 |