Reference articles on history, science, culture and more
Encyclopedia

C with Classes

Precursor language to C++

Image credit is listed at the end of this article.

C with Classes was the historical precursor language to what would later become C++, both of which were first created by Danish computer scientist Bjarne Stroustrup. As its name suggests, it aimed to extend the C language with object-oriented features, namely classes.

The term "C with classes" has also been used colloquially (sometimes disparagingly) to refer to C++ code written as if it were C.

01History

In 1979, Bjarne Stroustrup began work on C with Classes. The motivation for creating a new language originated from Stroustrup's experience in programming for his PhD thesis. Stroustrup found that Simula had features that were very helpful for large software development, but the language was too slow for practical use, while BCPL was fast but too low-level to be suitable for large software development. When Stroustrup started working in AT&T Bell Labs, he had the problem of analyzing the UNIX kernel with respect to distributed computing. Remembering his PhD experience, Stroustrup set out to enhance the C language with Simula-like features. C was chosen because it was general-purpose, fast, portable, and widely used. In addition to C and Simula's influences, other languages influenced this new language, including ALGOL 68, Ada, CLU, and ML.

Initially, Stroustrup's "C with Classes" added features to the C compiler, Cpre, including classes, derived classes, strong typing, inlining, and default arguments.

In 1982, Stroustrup began designing a cleaned-up and extended successor to C with Classes, using traditional compiler technology. The language was briefly called C84 before the name C++ was adopted; the name was suggested by Rick Mascitti and refers to C's ++ increment operator. The resulting language included virtual functions, function and operator overloading, references, const, improved type checking, user-controlled free-store memory allocation (new/delete), and BCPL-style single-line // comments. Stroustrup also designed and implemented Cfront, the C++ compiler front end, between spring 1982 and summer 1983.

Bjarne Stroustrup, the creator of C++, in his AT&T New Jersey office, c. 2000
Bjarne Stroustrup, the creator of C++, in his AT&T New Jersey office, c. 2000

02Language

The language's constructors and destructors, rather than using the class names (X() and ~X()), used the new and delete keywords themselves as methods returning void; these keywords served to replace the C memory allocation functions malloc() and free(). C with Classes lacked the private keyword, and had only a public keyword to declare a public region of the class, while everything before was private.

C with Classes, which lacked namespaces, did not use the namespace resolution operator :: for referring to class members; rather, it used the period ., much like C. Providing the definition of a class method inside the class itself inlined the function, avoiding the function call overhead. In early iterations of C with Classes, every class would also have void call() and void return() (implicitly defined if not explicitly written) methods which would be called before and after (respectively) every function call.

#define STACK_SIZE 128 #include <stream.h> class Stack { char* min; char* top; char* max; /* Constructor, with default parameter */ void new(int size = STACK_SIZE) { top = min = new char[size]; max = min + size - 1; } /* Destructor */ void delete(void) { delete min; } public: void push(char c); char pop(void); }; /* Notice it is Stack.push(), not Stack::push() */ void Stack.push(char c) { if (max <= top) { error("Stack overflow"); } *top++ = c; } char Stack.pop() { if (top <= min) { error("Stack underflow"); } return *(--top); } main() { /* Much like C, the class keyword must appear before its name */ class Stack s; int i; for (i = 0; i < 10; ++i) { s.push((char)('a' + i)); } for (i = 0; i < 10; ++i) { cout << s.pop() << '\n'; } }

C with Classes did not have virtual functions or function and operator overloading; these were not added until C++ in 1984.

Templates did not appear until Cfront 3.0 in 1991, and namespaces not until 1993; exceptions were first conceived for C++ around 1990 but only implemented in 1992.

03Library additions

Stroustrup wrote various library headers for C with Classes; the most notable of these were a task library (<task.h>) for Simula-style concurrency, as well as a stream-based I/O library (<stream.h>, which would later become the foundation for the <iostream> header.

task was a class with a private constructor, for types to inherit from (and should not be confused for std::execution::task<T, Env>, added in C++26), used to represent an independent activity (a coroutine, or lightweight thread). Inter-task communication was done through queue classes, with a producer task putting data into qhead and a consumer task pulling it out from qhead. Classes timer and clock were part of a time framework used for Simula-style event-driven simulations.

Watch videos about C with ClassesExplainers and documentaries on YouTube (opens in a new tab)

Sources and credits

This article is adapted from the Wikipedia article C with Classes, written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.

Images, from Wikimedia Commons:

Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.