A stack program: Below is essentially the same stack implemented in C, C++, and Java. This is an array implementation. The code is arranged so that similar lines of each language are in a horizontal row with one another. To better illustrate the principles, this stack is ultimately stripped down, without even error checking. Each of the implementations takes place in a separate file, and each implementation is more-or-less how one might write code in the given language. Commentary about these approaches come after the code. Even the C implementation achieves a key goal of an abstract data type: the method of implementation of the stack could be completely changed, say to a linked list form, without altering or even recompiling files that contain code making use of the stack. The C++ and Java stacks have this property also.
Overview of the C Implemenation: This is the standard way to "fake" object-oriented programming in C: Use a separate file for the object, in this case stack.c. This allows for hidden data fields and hidden "member functions", as if they were declared private in the class of an object-oriented language. Data in the separate file is not accessible to the outside unless the data is declared extern. Functions can be protected from outside access by declaring them static, a very strange designator that should be called "private". The common connections between the stack implementation and its use occur in a header file, which is included below in each of the other files. This file is stack.h below. In this case the header file gives prototypes for the four functions that provide access to the stack. All variables related to the stack implementation are hidden in the implemenation file. The header file allows the stack implementation file and any file using the stack to be separately compiled. This C implementation gives a single instance of a stack of chars of size 10. If one wants a stack of a different size, or of a different type, one must hack the code and recompile. If one wants several instances of this stack (or similar ones), one must make another copy of the file with separate external function names.
Overview of the C++ Implemenation: The C++ implementation achieves the goals of inaccessible data and only the desired stack management functions publicly accessible. Even this stripped-down C++ implementation gives far more than the C version: Here a program that uses the stack can instantiate as many stacks of different sizes as desired, and use them all simultaneously. This same stack code could be written with C "templates" in such a way that a stacks of any type, including user-defined types, could be easily created. For a program that converts this stack code to a templated version, see C++ Stack Template The C++ implementation uses a separate "header" file as shown, similar to the C header file. This gives an "overview" of the stack without the complete implementation, and allows separate compilation of the file to implement the stack and any file that uses the stack. As in C, the header file allows the stack implementation file and any file using the stack to be separately compiled. This C++ code needs a separate destructor to recover the storage for the stack after it is no longer in use. This is because the one portion of storage is a dynamically allocated array which would not be deleted by the default destructor. Java uses automatic garbage collection, and so it doesn't need destructors.
Overview of the Java Implemenation: Many of the same comments given above for C++ apply to the Java implementation. Java doesn't have the C++ templates, but in Java one can create a stack of any type of object, and thus get the same features described above for C++, though the code will not be as efficient. The Java implementation does not have a separate header file. In order to compile a file that uses this stack, the compiler must have access to the stack implementation file. Java has the concept of an interface, which for a stack such as this would be a contract to implement the four given stack functions, by providing their prototypes in an interface declaration, and saying that the stack "implements" this interface. Revision date: 2013-02-16. (Please use ISO 8601, the International Standard Date and Time Notation.) |